Class: Manufactory::DSL

Inherits:
Object show all
Defined in:
lib/manufactory/dsl.rb

Overview

DSL is used to execute the blueprint and construct an object. The blueprint is instance_eval’d against the Lathe.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, object, blueprint) ⇒ DSL

Returns a new instance of DSL.



8
9
10
11
12
# File 'lib/manufactory/dsl.rb', line 8

def initialize(adapter, object, blueprint)
  @adapter   = adapter
  @blueprint = blueprint
  @object    = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/manufactory/dsl.rb', line 24

def method_missing(symbol, *args, &block)
  if attribute_assigned?(symbol)
    # If we've already assigned the attribute, return that.
    self.object.send(symbol)
  elsif @adapter.has_association?(self.object, symbol) && !nil_or_empty?(self.object.send(symbol))
    # If the attribute is an association and is already assigned, return that.
    self.object.send(symbol)
  else
    # Otherwise generate a value and assign it.
    assigned_attributes[symbol] = generate_attribute_value(symbol, *args, &block)
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/manufactory/dsl.rb', line 7

def adapter
  @adapter
end

#blueprintObject (readonly)

Returns the value of attribute blueprint.



7
8
9
# File 'lib/manufactory/dsl.rb', line 7

def blueprint
  @blueprint
end

#object {|@object| ... } ⇒ Object (readonly)

Returns the value of attribute object.

Yields:



7
8
9
# File 'lib/manufactory/dsl.rb', line 7

def object
  @object
end

Instance Method Details

#assigned_attributesObject



37
38
39
# File 'lib/manufactory/dsl.rb', line 37

def assigned_attributes
  @assigned_attributes ||= {}
end

#runObject



14
15
16
17
# File 'lib/manufactory/dsl.rb', line 14

def run
  self.instance_eval(&self.blueprint)
  assigned_attributes
end