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.
10 11 12 13 14 |
# File 'lib/machinist/blueprint.rb', line 10 def initialize(klass, = {}, &block) @klass = klass @parent = [:parent] @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
16 17 18 |
# File 'lib/machinist/blueprint.rb', line 16 def block @block end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
16 17 18 |
# File 'lib/machinist/blueprint.rb', line 16 def klass @klass end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
16 17 18 |
# File 'lib/machinist/blueprint.rb', line 16 def parent @parent end |
Instance Method Details
#each_ancestor ⇒ Object
Yields the parent blueprint, its parent blueprint, etc.
52 53 54 55 56 57 58 |
# File 'lib/machinist/blueprint.rb', line 52 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.
33 34 35 |
# File 'lib/machinist/blueprint.rb', line 33 def lathe_class Lathe end |
#make(attributes = {}) ⇒ Object
Generate an object from this blueprint.
Pass in attributes to override values defined in the blueprint.
21 22 23 24 25 26 27 28 |
# File 'lib/machinist/blueprint.rb', line 21 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.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/machinist/blueprint.rb', line 38 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 |