Class: Faker::UniqueGenerator
- Inherits:
-
Object
- Object
- Faker::UniqueGenerator
- Defined in:
- lib/helpers/unique_generator.rb
Constant Summary collapse
- RetryLimitExceeded =
Class.new(StandardError)
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(generator, max_retries) ⇒ UniqueGenerator
constructor
A new instance of UniqueGenerator.
-
#method_missing(name, *arguments) ⇒ Object
rubocop:disable Style/MethodMissingSuper.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
rubocop:enable Style/MethodMissingSuper.
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
rubocop:disable Style/MethodMissingSuper
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/helpers/unique_generator.rb', line 10 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, "Retry limit exceeded for #{name}" end |
Class Method Details
.clear ⇒ Object
34 35 36 |
# File 'lib/helpers/unique_generator.rb', line 34 def self.clear ObjectSpace.each_object(self, &:clear) end |
Instance Method Details
#clear ⇒ Object
30 31 32 |
# File 'lib/helpers/unique_generator.rb', line 30 def clear @previous_results.clear end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
rubocop:enable Style/MethodMissingSuper
24 25 26 |
# File 'lib/helpers/unique_generator.rb', line 24 def respond_to_missing?(method_name, include_private = false) method_name.to_s.start_with?('faker_') || super end |