Class: Sham

Inherits:
Object show all
Defined in:
lib/dm-machinist/sham.rb

Constant Summary collapse

@@shams =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Sham

Returns a new instance of Sham.



25
26
27
28
29
30
31
# File 'lib/dm-machinist/sham.rb', line 25

def initialize(name, options = {}, &block)
  @name      = name
  @generator = block
  @offset    = 0
  @unique    = options.has_key?(:unique) ? options[:unique] : true
  generate_values(12)
end

Class Method Details

.method_missing(symbol, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/dm-machinist/sham.rb', line 11

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.



7
8
9
# File 'lib/dm-machinist/sham.rb', line 7

def self.name(*args, &block)
  method_missing(:name, *args, &block)
end

.resetObject



21
22
23
# File 'lib/dm-machinist/sham.rb', line 21

def self.reset
  @@shams.values.each{ |v| v.reset }
end

Instance Method Details

#fetch_valueObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/dm-machinist/sham.rb', line 37

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

#resetObject



33
34
35
# File 'lib/dm-machinist/sham.rb', line 33

def reset
  @offset = 0
end