Class: Blueprints::Blueprint
- Defined in:
- lib/blueprints/blueprint.rb
Overview
Class for actual blueprints. Allows building itself by executing block passed against current context.
Constant Summary
Constants inherited from Buildable
Blueprints::Buildable::BUILDING_MESSAGE
Instance Attribute Summary collapse
-
#uses ⇒ Object
readonly
Holds how many times this particular blueprint was built.
Attributes inherited from Buildable
Instance Method Summary collapse
-
#blueprint(name, &block) ⇒ Blueprints::Blueprint
Defines strategy for this blueprint.
- #demolish(environment = nil, current_name = nil, &block) ⇒ Object
-
#extends(parent, options = {}) ⇒ Object
Changes blueprint block to build another blueprint by passing additional options to it.
-
#initialize(name, context, &block) ⇒ Blueprint
constructor
Initializes blueprint by name, context and block.
-
#normalized_attributes(environment, options = {}) ⇒ Hash
Returns normalized attributes for this blueprint.
-
#update(&block) ⇒ Object
Allows customizing what happens when blueprint is already built and it’s being built again.
Methods inherited from Buildable
#attributes, #build, #build_parents, #built?, #depends_on, #full_name, infer_name, #inspect, #path, #undo!
Constructor Details
#initialize(name, context, &block) ⇒ Blueprint
Initializes blueprint by name, context and block. Also sets default demolish and update blocks.
10 11 12 13 14 15 16 17 18 |
# File 'lib/blueprints/blueprint.rb', line 10 def initialize(name, context, &block) super(name, context) @strategies = {} @strategies[:default] = block || Proc.new { dependencies.collect { |dep| instance_variable_get(:"@#{dep}") } } @strategies[:demolish] = Proc.new { instance_variable_get(variable_name).destroy } @strategies[:update] = Proc.new { instance_variable_get(variable_name).blueprint() } @uses = 0 end |
Instance Attribute Details
#uses ⇒ Object (readonly)
Holds how many times this particular blueprint was built
5 6 7 |
# File 'lib/blueprints/blueprint.rb', line 5 def uses @uses end |
Instance Method Details
#blueprint(name, &block) ⇒ Blueprints::Blueprint
Defines strategy for this blueprint. Blueprint can later be built using this strategy by passing :strategy option to Buildable#build method.
58 59 60 61 |
# File 'lib/blueprints/blueprint.rb', line 58 def blueprint(name, &block) @strategies[name.to_sym] = block self end |
#demolish(&block) ⇒ Object #demolish(environment) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/blueprints/blueprint.rb', line 38 def demolish(environment = nil, current_name = nil, &block) if block blueprint(:demolish, &block) elsif environment and built? eval_block(environment, {}, current_name, &@strategies[:demolish]) undo! else raise DemolishError, @name end end |
#extends(parent, options = {}) ⇒ Object
Changes blueprint block to build another blueprint by passing additional options to it. Usually used to dry up blueprints that are often built with some options.
27 28 29 |
# File 'lib/blueprints/blueprint.rb', line 27 def extends(parent, = {}) attributes().blueprint(:default) { build parent => attributes } end |
#normalized_attributes(environment, options = {}) ⇒ Hash
Returns normalized attributes for this blueprint. Normalized means that all dependencies are replaced by real instances and all procs evaluated.
68 69 70 |
# File 'lib/blueprints/blueprint.rb', line 68 def normalized_attributes(environment, = {}) normalize_hash(environment, @context.attributes.merge()) end |
#update(&block) ⇒ Object
Allows customizing what happens when blueprint is already built and it’s being built again.
50 51 52 |
# File 'lib/blueprints/blueprint.rb', line 50 def update(&block) blueprint(:update, &block) end |