Class: Manufactory::Sham
Constant Summary collapse
- @@shams =
Hash.new
Class Method Summary collapse
- .clear ⇒ Object
- .define(&block) ⇒ Object
- .method_missing(symbol, *args, &block) ⇒ Object
-
.name(*args, &block) ⇒ Object
Over-ride module’s built-in name method, so we can re-use it for generating names.
- .reset(scope = :before_all) ⇒ Object
Instance Method Summary collapse
- #fetch_value ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Sham
constructor
A new instance of Sham.
- #reset(scope) ⇒ Object
Constructor Details
#initialize(name, options = {}, &block) ⇒ Sham
Returns a new instance of Sham.
36 37 38 39 40 41 42 |
# File 'lib/manufactory/sham.rb', line 36 def initialize(name, = {}, &block) @name = name @generator = block @offset = 0 @unique = .has_key?(:unique) ? [:unique] : true generate_values(12) end |
Class Method Details
.clear ⇒ Object
24 25 26 |
# File 'lib/manufactory/sham.rb', line 24 def self.clear @@shams = {} end |
.define(&block) ⇒ Object
32 33 34 |
# File 'lib/manufactory/sham.rb', line 32 def self.define(&block) Sham.instance_eval(&block) end |
.method_missing(symbol, *args, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/manufactory/sham.rb', line 14 def self.method_missing(symbol, *args, &block) if block_given? @@shams[symbol] = Sham.new(symbol, args.pop || {}, &block) else sham = @@shams[symbol] raise "No sham defined for #{symbol}" if sham.nil? sham.fetch_value end end |
.name(*args, &block) ⇒ Object
Over-ride module’s built-in name method, so we can re-use it for generating names. This is a bit of a no-no, but we get away with it in this context.
10 11 12 |
# File 'lib/manufactory/sham.rb', line 10 def self.name(*args, &block) method_missing(:name, *args, &block) end |
.reset(scope = :before_all) ⇒ Object
28 29 30 |
# File 'lib/manufactory/sham.rb', line 28 def self.reset(scope = :before_all) @@shams.values.each { |sham| sham.reset(scope) } end |
Instance Method Details
#fetch_value ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/manufactory/sham.rb', line 54 def fetch_value # Generate more values if we need them. if @offset >= @values.length generate_values(2 * @values.length) raise "Can't generate more unique values for Sham.#{@name}" if @offset >= @values.length end result = @values[@offset] @offset += 1 result end |
#reset(scope) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/manufactory/sham.rb', line 44 def reset(scope) if scope == :before_all @offset, @before_offset = 0, nil elsif @before_offset @offset = @before_offset else @before_offset = @offset end end |