Class: Machinist::Blueprint
- Inherits:
-
Object
- Object
- Machinist::Blueprint
- Defined in:
- lib/machinist/blueprint.rb
Overview
A Blueprint defines a method of constructing objects of a particular class.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#each_ancestor ⇒ Object
Yields the parent blueprint, its parent blueprint, etc.
-
#initialize(klass, options = {}, &block) ⇒ Blueprint
constructor
Construct a blueprint for the given
klass. -
#lathe_class ⇒ Object
Returns the Lathe class used to make objects for this blueprint.
-
#make(attributes = {}) ⇒ Object
Generate an object from this blueprint.
-
#parent_blueprint ⇒ Object
Returns the parent blueprint for this blueprint.
Constructor Details
#initialize(klass, options = {}, &block) ⇒ Blueprint
Construct a blueprint for the given klass.
Pass in the :parent option to define a parent blueprint to apply after this one. You can supply another blueprint, or a class in which to look for a blueprint. In the latter case, make will walk up the superclass chain looking for blueprints to apply.
12 13 14 15 16 |
# File 'lib/machinist/blueprint.rb', line 12 def initialize(klass, = {}, &block) @klass = klass @parent = [:parent] @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
18 19 20 |
# File 'lib/machinist/blueprint.rb', line 18 def block @block end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
18 19 20 |
# File 'lib/machinist/blueprint.rb', line 18 def klass @klass end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
18 19 20 |
# File 'lib/machinist/blueprint.rb', line 18 def parent @parent end |
Instance Method Details
#each_ancestor ⇒ Object
Yields the parent blueprint, its parent blueprint, etc.
54 55 56 57 58 59 60 |
# File 'lib/machinist/blueprint.rb', line 54 def each_ancestor ancestor = parent_blueprint while ancestor yield ancestor ancestor = ancestor.parent_blueprint end end |
#lathe_class ⇒ Object
Returns the Lathe class used to make objects for this blueprint.
Subclasses can override this to substitute a custom lathe class.
35 36 37 |
# File 'lib/machinist/blueprint.rb', line 35 def lathe_class Lathe end |
#make(attributes = {}) ⇒ Object
Generate an object from this blueprint.
Pass in attributes to override values defined in the blueprint.
23 24 25 26 27 28 29 30 |
# File 'lib/machinist/blueprint.rb', line 23 def make(attributes = {}) lathe = lathe_class.new(@klass, new_serial_number, attributes) lathe.instance_eval(&@block) each_ancestor {|blueprint| lathe.instance_eval(&blueprint.block) } lathe.object end |
#parent_blueprint ⇒ Object
Returns the parent blueprint for this blueprint.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/machinist/blueprint.rb', line 40 def parent_blueprint case @parent when nil nil when Blueprint # @parent references the parent blueprint directly. @parent else # @parent is a class in which we should look for a blueprint. find_blueprint_in_superclass_chain(@parent) end end |