Class: KathyLee::Fakes

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kathy_lee/fakes.rb

Overview

A registry for procs that can be called to generate random, or fake, data.

Defined Under Namespace

Classes: Alias

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFakes

:nodoc:



9
10
11
# File 'lib/kathy_lee/fakes.rb', line 9

def initialize # :nodoc:
  self.reset!
end

Instance Attribute Details

#listObject

List of all the fake procs in the system.



7
8
9
# File 'lib/kathy_lee/fakes.rb', line 7

def list
  @list
end

Class Method Details

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

:nodoc:



51
52
53
# File 'lib/kathy_lee/fakes.rb', line 51

def method_missing(sym, *args, &block) # :nodoc:
  KathyLee::Fakes.instance.send(sym, *args, &block)
end

Instance Method Details

#add(name, &block) ⇒ Object

Add a new proc to the system.

Example:

KathyLee::Fakes.add(:birth_date) {(rand(80) + 13).years.ago}


21
22
23
# File 'lib/kathy_lee/fakes.rb', line 21

def add(name, &block)
  self.list[name.to_sym] = block
end

#alias(from, to) ⇒ Object

Create an alias from one fake proc to another.

Example:

KathyLee::Fakes.add(:birth_date) {(rand(80) + 13).years.ago}
KathyLee::Fakes.alias(:birthday, :birth_date)


30
31
32
# File 'lib/kathy_lee/fakes.rb', line 30

def alias(from, to)
  self.list[from.to_sym] = KathyLee::Fakes::Alias.new(to)
end

#execute(name, *args) ⇒ Object

Executes the specified fake proc. Raise KathyLee::Errors::NoFakeRegistered if the fake proc is not registered with the system.

Example:

KathyLee::Fakes.execute(:email) # => '[email protected]'


39
40
41
42
43
44
45
46
47
48
# File 'lib/kathy_lee/fakes.rb', line 39

def execute(name, *args)
  block = self.list[name.to_sym]
  if block.is_a?(KathyLee::Fakes::Alias)
    return execute(block.to, *args)
  end
  if block
    return block.call(*args)
  end
  raise "No fake has been registered for '#{name}'!"
end

#reset!Object

:nodoc:



13
14
15
# File 'lib/kathy_lee/fakes.rb', line 13

def reset! # :nodoc:
  self.list = {}
end