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

Raises:



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

Class Method Details

.clearObject



28
29
30
# File 'lib/helpers/unique_generator.rb', line 28

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

Instance Method Details

#clearObject



24
25
26
# File 'lib/helpers/unique_generator.rb', line 24

def clear
  @previous_results.clear
end