Class: KathyLee::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/kathy_lee/definition.rb

Overview

Houses the definition of a factory.

Examples:

KathyLee.define(:user) do
  user = User.new(options)
  ... # do some more work
  user
end

KathyLee.define(:user) do
  has_one :blog # will call the :blog factory and assign it to the
                # user object when it's returned from the block.

  user = User.new(options)
  ... # do some more work
  user
end

Defined Under Namespace

Classes: Binding, HasMany, HasOne, Relationship

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory_name, attributes = {}, &block) ⇒ Definition

Returns a new instance of Definition.



24
25
26
27
28
# File 'lib/kathy_lee/definition.rb', line 24

def initialize(factory_name, attributes = {}, &block)
  self.attributes = attributes
  self.factory_name = factory_name
  self.code_block = block
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#code_blockObject

Returns the value of attribute code_block.



22
23
24
# File 'lib/kathy_lee/definition.rb', line 22

def code_block
  @code_block
end

#factory_nameObject

Returns the value of attribute factory_name.



20
21
22
# File 'lib/kathy_lee/definition.rb', line 20

def factory_name
  @factory_name
end

Instance Method Details

#build(attribs = {}) ⇒ Object

Execute the code block in it’s own building, with it’s own attributes and return the result.



32
33
34
35
36
# File 'lib/kathy_lee/definition.rb', line 32

def build(attribs = {})
  b = KathyLee::Definition::Binding.new(self.factory_name, self.attributes.merge(attribs), &self.code_block)
  b.process!
  return b.result
end