Class: Faker::UniqueGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/unique_generator.rb

Constant Summary collapse

RetryLimitExceeded =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

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

Raises:



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

.clearObject



34
35
36
# File 'lib/helpers/unique_generator.rb', line 34

def self.clear
  ObjectSpace.each_object(self, &:clear)
end

Instance Method Details

#clearObject



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

Returns:



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