Class: Faker::UniqueGenerator
- Inherits:
-
Object
- Object
- Faker::UniqueGenerator
- Defined in:
- lib/helpers/unique_generator.rb
Constant Summary collapse
- RetryLimitExceeded =
Class.new(StandardError)
Instance Method Summary collapse
-
#initialize(generator, max_retries) ⇒ UniqueGenerator
constructor
A new instance of UniqueGenerator.
- #method_missing(name, *arguments) ⇒ Object
Constructor Details
#initialize(generator, max_retries) ⇒ UniqueGenerator
Returns a new instance of UniqueGenerator.
3 4 5 6 7 |
# File 'lib/helpers/unique_generator.rb', line 3 def initialize(generator, max_retries) @generator = generator @max_retries = max_retries @previous_results = Hash.new { |hash, key| hash[key] = Set.new } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/helpers/unique_generator.rb', line 9 def method_missing(name, *arguments) @max_retries.times do result = @generator.public_send(name, *arguments) next if @previous_results[[name, arguments]].include?(result) @previous_results[[name, arguments]] << result return result end raise RetryLimitExceeded end |