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.

Constant Summary

Constants inherited from Buildable

Blueprints::Buildable::BUILDING_MESSAGE

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, 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.

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)

  @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(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

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

Parameters:

  • name (#to_sym)

    Name of strategy.

Returns:



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

Overloads:

  • #demolish(&block) ⇒ Object

    Sets custom block for demolishing this blueprint.

  • #demolish(environment) ⇒ Object

    Demolishes blueprint by calling demolish block.

    Parameters:

    • environment (Object)

      Context where blueprint was built in.

    • current_name (Symbol)

      Current name of blueprint (used when demolishing blueprints with regexp name). When nil is passed then @name is used.

    Raises:



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.

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) (defaults to: {})

    Options to be passed when building parent.



27
28
29
# File 'lib/blueprints/blueprint.rb', line 27

def extends(parent, options = {})
  attributes(options).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.

Parameters:

  • environment

    Context that blueprints are built against

  • options (Hash) (defaults to: {})

    Options hash, merged into attributes

Returns:

  • (Hash)

    normalized attributes for this blueprint



68
69
70
# File 'lib/blueprints/blueprint.rb', line 68

def normalized_attributes(environment, options = {})
  normalize_hash(environment, @context.attributes.merge(options))
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