faker-ruby / faker

A library for generating fake data such as names, addresses, and phone numbers.
MIT License
11.22k stars 3.18k forks source link

Add Composite generator class #2764

Closed ozydingo closed 1 year ago

ozydingo commented 1 year ago

Is your feature request related to a problem? Please describe it.

I have a use case where a user might upload various media files that might be movies, tv shows, music, and so on. I would love to increase the diversity of names using a simple composite generators that included, e.g. Faker::Movies, Faker::Music, and Faker::TvShows

If you're adding new objects, please describe how you would use them

generator = Faker::Composite(Faker::Movies, Faker::Music)
generator.title

The methods supported by the composite could use introspection to determine which methods are supported by all composed elements, or simply route methods to only those composed elements that respond_to? the method (and I say this as an opponent of Duck Typing as a general rule :-))

Describe alternatives you've considered

Additional context

Happy to work on this feature and submit a pull request.

thdaraujo commented 1 year ago

I think this would be an interesting addition to the library, and it makes a lot of sense. It would be nice to mix and match generators for similar things like you're proposing.

Can you add a couple more usage examples in the issue description?

One problem I see is for sampled generators that respond to the given method, but not with the same arguments. As long as we raise a helpful error that identifies the generator class that failed, I think it should be fine.

The other thing that comes to mind is that a user might want to call a different method depending on the sampled generator. But that might make things more complicated, so this could be a future addition.

sjjbirch commented 1 year ago

I'm writing an extension gem for this at the moment. The implicitly required features are not just compositing generators, but also weighting them or making them conditional.

The immediate solution for the argument problem is simply culling methods with an arity outside -1..0, the reason being that the compositing process should allow the compositing of namespace superclasses as well (for example, compositing animals with music, or book methods with french opera singers) such that a user couldn't reasonably know or fill the arity requirements ahead of time.

thdaraujo commented 1 year ago

@ozydingo @sjjbirch please take a look at #2794

stefannibrasil commented 1 year ago

Got here after reviewing #2794

I have a use case where a user might upload various media files that might be movies, tv shows, music, and so on. I would love to increase the diversity of names using a simple composite generators that included, e.g. Faker::Movies, Faker::Music, and Faker::TvShows

Thank you for your proposal! In my opinion, if the need is to get a wide range of names using a simple way, the simplest way I see is using Ruby to call multiple faker generators for your use case.

I think an extension gem outside of faker-ruby would be an appropriate solution for this as the library already has lots of generators and this would be a hard task to add to the maintainers duties.

thdaraujo commented 1 year ago

Got here after reviewing #2794

I have a use case where a user might upload various media files that might be movies, tv shows, music, and so on. I would love to increase the diversity of names using a simple composite generators that included, e.g. Faker::Movies, Faker::Music, and Faker::TvShows

Thank you for your proposal! In my opinion, if the need is to get a wide range of names using a simple way, the simplest way I see is using Ruby to call multiple faker generators for your use case.

I think an extension gem outside of faker-ruby would be an appropriate solution for this as the library already has lots of generators and this would be a hard task to add to the maintainers duties.

I agree. Let's close this for now. I'd be happy to reopen this issue if we see more interest in this type of feature in the future.