Class: Blueprints::Blueprint
- Defined in:
- lib/blueprints/blueprint.rb
Overview
Class for actual blueprints. Allows building itself by executing block passed against current context.
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Attributes inherited from Buildable
Instance Method Summary collapse
- #backtrace(trace) ⇒ Object
-
#build_self(build_once = true) ⇒ Object
Builds blueprint and adds it to executed blueprint hash.
-
#demolish(&block) ⇒ Object
If block is passed then sets custom demolish block for this blueprint.
-
#extends(parent, options) ⇒ Object
Changes blueprint block to build another blueprint by passing additional options to it.
-
#initialize(name, file, &block) ⇒ Blueprint
constructor
Initializes blueprint by name and block.
-
#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, normalize_attributes, #normalized_attributes, #result, #result=, #undo!
Constructor Details
#initialize(name, file, &block) ⇒ Blueprint
Initializes blueprint by name and block
6 7 8 9 10 11 12 13 14 |
# File 'lib/blueprints/blueprint.rb', line 6 def initialize(name, file, &block) @file = file super(name) ivname = variable_name @block = block @demolish_block = lambda { instance_variable_get(ivname).destroy } @update_block = lambda { instance_variable_get(ivname).blueprint() } end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
4 5 6 |
# File 'lib/blueprints/blueprint.rb', line 4 def file @file end |
Instance Method Details
#backtrace(trace) ⇒ Object
34 35 36 |
# File 'lib/blueprints/blueprint.rb', line 34 def backtrace(trace) trace.collect { |line| line.sub(/^\(eval\):(\d+).*/, "#{@file}:\\1:in blueprint '#{@name}'") } end |
#build_self(build_once = true) ⇒ Object
Builds blueprint and adds it to executed blueprint hash. Setups instance variable with same name as blueprint if it is not defined yet.
17 18 19 20 21 22 23 24 25 |
# File 'lib/blueprints/blueprint.rb', line 17 def build_self(build_once = true) surface_errors do if built? and build_once Namespace.root.context.instance_eval(&@update_block) if RootNamespace.root.context..present? elsif @block self.result = Namespace.root.context.instance_eval(&@block) end end end |
#demolish(&block) ⇒ Object
If block is passed then sets custom demolish block for this blueprint. If no block is passed then calls demolish block and marks blueprint as not built. Raises DemolishError if blueprints has not been built.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/blueprints/blueprint.rb', line 41 def demolish(&block) if block @demolish_block = block elsif built? Namespace.root.context.instance_eval(&@demolish_block) 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.
29 30 31 32 |
# File 'lib/blueprints/blueprint.rb', line 29 def extends(parent, ) attributes() @block = Proc.new { build parent => attributes } end |
#update(&block) ⇒ Object
Allows customizing what happens when blueprint is already built and it’s being built again.
53 54 55 |
# File 'lib/blueprints/blueprint.rb', line 53 def update(&block) @update_block = block end |