Class: FFaker::UniqueUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/ffaker/utils/unique_utils.rb

Constant Summary collapse

RetryLimitExceeded =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, max_retries) ⇒ UniqueUtils

Returns a new instance of UniqueUtils.



24
25
26
27
# File 'lib/ffaker/utils/unique_utils.rb', line 24

def initialize(generator, max_retries)
  @generator = generator
  @max_retries = max_retries
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs) ⇒ Object (private)

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ffaker/utils/unique_utils.rb', line 35

def method_missing(name, *args, **kwargs)
  @max_retries.times do
    result = @generator.public_send(name, *args, **kwargs)

    next if previous_results[[name, args, kwargs]].include?(result)

    previous_results[[name, args, kwargs]] << result
    return result
  end

  raise RetryLimitExceeded, "Retry limit exceeded for #{name}"
end

Class Method Details

.add_instance(generator, max_retries) ⇒ Object



10
11
12
# File 'lib/ffaker/utils/unique_utils.rb', line 10

def add_instance(generator, max_retries)
  instances[generator] ||= FFaker::UniqueUtils.new(generator, max_retries)
end

.clearObject



18
19
20
21
# File 'lib/ffaker/utils/unique_utils.rb', line 18

def clear
  instances.each_value(&:clear)
  instances.clear
end

.instancesObject



14
15
16
# File 'lib/ffaker/utils/unique_utils.rb', line 14

def instances
  Thread.current[:ffaker_unique_utils] ||= {}
end

Instance Method Details

#clearObject



29
30
31
# File 'lib/ffaker/utils/unique_utils.rb', line 29

def clear
  previous_results.clear
end