Class: Faker::UniqueGenerator
- Inherits:
-
Object
- Object
- Faker::UniqueGenerator
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.
5
6
7
8
|
# File 'lib/helpers/unique_generator.rb', line 5
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, *arguments) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/helpers/unique_generator.rb', line 10
def method_missing(name, *arguments)
self.class.marked_unique.add(self)
@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
48
49
50
51
|
# File 'lib/helpers/unique_generator.rb', line 48
def self.clear
marked_unique.each(&:clear)
marked_unique.clear
end
|
.marked_unique ⇒ Object
44
45
46
|
# File 'lib/helpers/unique_generator.rb', line 44
def self.marked_unique
Thread.current[:faker_unique_generator_marked_unique] ||= Set.new
end
|
Instance Method Details
#clear ⇒ Object
40
41
42
|
# File 'lib/helpers/unique_generator.rb', line 40
def clear
previous_results.clear
end
|
#exclude(name, arguments, values) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/helpers/unique_generator.rb', line 53
def exclude(name, arguments, values)
values ||= []
values.each do |value|
previous_results[[name, arguments]] << value
end
end
|
#previous_results ⇒ Object
35
36
37
38
|
# File 'lib/helpers/unique_generator.rb', line 35
def previous_results
Thread.current[:faker_unique_generator_previous_results] ||= {}
Thread.current[:faker_unique_generator_previous_results][@generator] ||= Hash.new { |hash, key| hash[key] = Set.new }
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
29
30
31
|
# File 'lib/helpers/unique_generator.rb', line 29
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('faker_') || super
end
|