Class: Sham
Constant Summary collapse
- @@shams =
{}
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.
35 36 37 38 39 40 41 |
# File 'lib/sham.rb', line 35 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
23 24 25 |
# File 'lib/sham.rb', line 23 def self.clear @@shams = {} end |
.define(&block) ⇒ Object
31 32 33 |
# File 'lib/sham.rb', line 31 def self.define(&block) Sham.instance_eval(&block) end |
.method_missing(symbol, *args, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/sham.rb', line 13 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.
9 10 11 |
# File 'lib/sham.rb', line 9 def self.name(*args, &block) method_missing(:name, *args, &block) end |
.reset(scope = :before_all) ⇒ Object
27 28 29 |
# File 'lib/sham.rb', line 27 def self.reset(scope = :before_all) @@shams.values.each { |sham| sham.reset(scope) } end |
Instance Method Details
#fetch_value ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sham.rb', line 53 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 returning @values[@offset] do @offset += 1 end end |
#reset(scope) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/sham.rb', line 43 def reset(scope) if scope == :before_all @offset, @before_offset = 0, nil elsif @before_offset @offset = @before_offset else @before_offset = @offset end end |