Class: FakerUnique::UniqueProxy
- Inherits:
-
Object
- Object
- FakerUnique::UniqueProxy
- Defined in:
- lib/faker_unique.rb
Instance Attribute Summary collapse
-
#max_retries ⇒ Object
writeonly
Sets the attribute max_retries.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
-
#initialize(klass) ⇒ UniqueProxy
constructor
A new instance of UniqueProxy.
- #method_missing(m, *args, &block) ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize(klass) ⇒ UniqueProxy
Returns a new instance of UniqueProxy.
15 16 17 18 |
# File 'lib/faker_unique.rb', line 15 def initialize(klass) @klass = klass reset! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/faker_unique.rb', line 24 def method_missing(m, *args, &block) counter = 0 begin if counter > @max_retries raise "Maximum retries of %d reached without finding a unique value" % @max_retries end res = @klass.send(m, *args, &block) counter += 1 end while @pool[m][res] @pool[m][res] = true res end |
Instance Attribute Details
#max_retries=(value) ⇒ Object (writeonly)
Sets the attribute max_retries
13 14 15 |
# File 'lib/faker_unique.rb', line 13 def max_retries=(value) @max_retries = value end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
12 13 14 |
# File 'lib/faker_unique.rb', line 12 def pool @pool end |
Instance Method Details
#reset! ⇒ Object
20 21 22 |
# File 'lib/faker_unique.rb', line 20 def reset! @pool = Hash.new {|h,k| h[k] = {}} end |