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
-
#uses ⇒ Object
readonly
Holds how many times this particular blueprint was built.
Attributes inherited from Buildable
Instance Method Summary collapse
-
#backtrace(trace) ⇒ Array<String>
Changes backtrace to include what blueprint was being built.
-
#build_self(eval_context, build_once, options) ⇒ Object
Builds blueprint and adds it to executed blueprint array.
- #demolish(eval_context = 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.
-
#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, #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) ivname = variable_name @block = block @demolish_block = Proc.new { instance_variable_get(ivname).destroy } @update_block = Proc.new { instance_variable_get(ivname).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
#backtrace(trace) ⇒ Array<String>
Changes backtrace to include what blueprint was being built.
51 52 53 |
# File 'lib/blueprints/blueprint.rb', line 51 def backtrace(trace) trace.collect! { |line| line.sub(/^#{@context.file}:(\d+).*/, "#{@context.file}:\\1:in blueprint '#{@name}'") } end |
#build_self(eval_context, build_once, options) ⇒ Object
Builds blueprint and adds it to executed blueprint array. Setups instance variable with same name as blueprint if it is not defined yet. Marks blueprint as used.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/blueprints/blueprint.rb', line 25 def build_self(eval_context, build_once, ) @uses += 1 unless built? surface_errors do if built? and build_once eval_context.instance_eval(@context, , &@update_block) if .present? elsif @block result(eval_context) { eval_context.instance_eval(@context, , &@block) } end end end |
#demolish(&block) ⇒ Object #demolish(eval_context) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/blueprints/blueprint.rb', line 61 def demolish(eval_context = nil, &block) if block @demolish_block = block elsif eval_context and built? eval_context.instance_eval(@context, {}, &@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.
43 44 45 46 |
# File 'lib/blueprints/blueprint.rb', line 43 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.
73 74 75 |
# File 'lib/blueprints/blueprint.rb', line 73 def update(&block) @update_block = block end |