Filter Generated Values #1836
Open
Filter Generated Values #1836
Conversation
…esponds_to? We expect `UniqueGenerator#method_missing` to result in a unique, generated value whenever the `@generator.public_send(name, arguments)` can be evaluated. As such, `UniqueGenerator#respond_to_missing?(method)` should return true whenever `@generator.respond_to?(method)` evaluates to true.
|
|
||
| def respond_to_missing?(method_name, include_private = false) | ||
| method_name.to_s.start_with?('faker_') || super | ||
| @generator.respond_to?(method_name) || super |
tmfnll
Nov 18, 2019
•
Author
The existing implementation behaves slightly inconsistently:
2.6.3 :005 > Faker::Base.unique.letterify('?')
=> "E"
2.6.3 :006 > Faker::Base.unique.respond_to? :letterify
=> false The change should ensure the above doesn't occur, as well as permit things like following:
2.6.3 :005 > Faker::Base.unique.method :letterify
=> #<Method: Faker::UniqueGenerator#letterify>(See https://thoughtbot.com/blog/always-define-respond-to-missing-when-overriding)
This works in a similar way to the existing Faker::UniqueGenerator class in that it keeps trying to generate data up until the point at which a condition is satisfied. In this case this condition is provided to the constructor via a block.
This allows for generators to offer the ability to filter results via `Faker::Filter` objects. Applying this too the `Faker::Base`, `Faker::UniqueGenerator` and `Faker::Filter` class means we can mix and match filters with unique constraints.
Include `Faker::Filtering` in `Faker::Filter`, `Faker::UniqueGenerator` and the extend `Faker::Base`. This makes `select` and `reject` methods available when generating values.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
No-StoryDescription:
I thought it might be nice to be able to constrain values generated by
Fakerto some subset of those defined for a given generator.This PR allows one to invoke
selecton a generator, passing a block, where the generator will then only output values for which the block evaluates to true.A contrived example:
Outputs only even integers.
The same result can be achieved using the equivalent
rejectmethod.I.e. The following is equivalent to the above:
Furthermore, these filters can be a chained after unique constraints:
So
Returns unique positive integers.
Further details about the implementation can be found in the commit messages.