Class: Blueprints::Blueprint

Inherits:
Buildable show all
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

Attributes inherited from Buildable

#name

Instance Method Summary collapse

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.

Parameters:

  • name (#to_sym, Hash)

    Name of buildable. If hash is passed then first key is assumed name, and value(s) of that key are assumed as dependencies.

  • context (Blueprints::Context)

    Context of buildable that later might get updated.



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(options) }
  @uses = 0
end

Instance Attribute Details

#usesObject (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.

Parameters:

  • trace (Array<String>)

    Current trace

Returns:

  • (Array<String>)

    Changed trace with included currently built blueprint name.



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.

Parameters:

  • eval_context (Blueprints::EvalContext)

    Context to build buildable object in.

  • build_once (true, false)

    Used if buildable is already built. If true then old one is updated else buildable is built again.

  • options (Hash)

    List of options to be accessible in the body of a blueprint.



25
26
27
28
29
30
31
32
33
34
# File 'lib/blueprints/blueprint.rb', line 25

def build_self(eval_context, build_once, options)
  @uses += 1 unless built?
  surface_errors do
    if built? and build_once
      eval_context.instance_eval(@context, options, &@update_block) if options.present?
    elsif @block
      result(eval_context) { eval_context.instance_eval(@context, options, &@block) }
    end
  end
end

#demolish(&block) ⇒ Object #demolish(eval_context) ⇒ Object

Overloads:

  • #demolish(&block) ⇒ Object

    Sets custom block for demolishing this blueprint.

  • #demolish(eval_context) ⇒ Object

    Demolishes blueprint by calling demolish block.

    Parameters:

    Raises:



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.

Examples:

Extending blueprints

Post.blueprint :post, :title => 'hello blueprints'
blueprint(:published_post).extends(:post, :published_at => Time.now)

Parameters:

  • parent (Symbol, String)

    Name of parent blueprint.

  • options (Hash)

    Options to be passed when building parent.



43
44
45
46
# File 'lib/blueprints/blueprint.rb', line 43

def extends(parent, options)
  attributes(options)
  @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