Class: Blueprints::Buildable

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprints/buildable.rb

Direct Known Subclasses

Blueprint, Namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, context) ⇒ Buildable

Initializes new Buildable object by name and context which it belongs to.

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.

Raises:

  • (TypeError)

    If name is invalid.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/blueprints/buildable.rb', line 11

def initialize(name, context)
  @context = context

  if name.nil?
    default_attribute = Blueprints.config.default_attributes.detect { |attribute| attributes.has_key?(attribute) }
    name = attributes[default_attribute].parameterize('_') if default_attribute
  end
  @name, parents = parse_name(name)
  depends_on(*parents)

  namespace.add_child(self) if namespace
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/blueprints/buildable.rb', line 4

def name
  @name
end

Instance Method Details

#attributesHash #attributes(value) ⇒ Object

Overloads:

  • #attributesHash

    Returns attributes of buildable

    Returns:

    • (Hash)

      Attributes of buildable

  • #attributes(value) ⇒ Object

    Merges attributes of buildable with new attributes by updating context

    Parameters:

    • Updated (Hash)

      attributes



42
43
44
45
46
47
48
# File 'lib/blueprints/buildable.rb', line 42

def attributes(value = nil)
  if value
    update_context(:attributes => value)
  else
    @context.attributes
  end
end

#build(eval_context, build_once = true, options = {}) ⇒ Object

Builds dependencies of blueprint and then blueprint itself.

Parameters:

  • eval_context (Blueprints::EvalContext)

    Context to build buildable object in.

  • build_once (true, false) (defaults to: true)

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

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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/blueprints/buildable.rb', line 54

def build(eval_context, build_once = true, options = {})
  return result(eval_context) if @building or (built? and build_once and options.blank?)
  @building = true

  each_namespace { |namespace| namespace.build_parents(eval_context) }
  build_parents(eval_context)

  result = build_self(eval_context, build_once, options)
  Namespace.root.executed_blueprints << self

  @building = false
  result
end

#build_parents(eval_context) ⇒ Object

Builds all dependencies. Should be called before building itself. Searches dependencies first in parent then in root namespace.

Parameters:

Raises:



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/blueprints/buildable.rb', line 95

def build_parents(eval_context)
  @context.dependencies.each do |name|
    parent = begin
      namespace[name]
    rescue BlueprintNotFoundError
      Namespace.root[name]
    end

    parent.build(eval_context)
  end
end

#built?true, false

Returns if blueprint has been built

Returns:

  • (true, false)

    true if was built, false otherwise



70
71
72
# File 'lib/blueprints/buildable.rb', line 70

def built?
  Namespace.root.executed_blueprints.include?(self)
end

#depends_on(*dependencies) ⇒ Object

Defines dependencies of buildable by updating it’s context.

Parameters:

  • dependencies (Array<String, Symbol>)

    List of dependencies.



32
33
34
# File 'lib/blueprints/buildable.rb', line 32

def depends_on(*dependencies)
  update_context(:dependencies => dependencies)
end

#full_nameString

Returns full name for this buildable

Returns:

  • (String)

    full buildable name



88
89
90
# File 'lib/blueprints/buildable.rb', line 88

def full_name
  path('.')
end

#inspectString

Returns class, name, attributes and dependencies of buildable in nice formatted string.

Returns:

  • (String)

    Inspected properties of buildable.



26
27
28
# File 'lib/blueprints/buildable.rb', line 26

def inspect
  "<##{self.class} name: #{full_name.inspect}, attributes: #{attributes.inspect}, dependencies: #{dependencies.inspect}>"
end

#path(join_with = '_') ⇒ String

Returns full path to this buildable

Parameters:

  • join_with (String) (defaults to: '_')

    Separator used to join names of parent namespaces and buildable itself.

Returns:

  • (String)

    full path to this buildable joined with separator



82
83
84
# File 'lib/blueprints/buildable.rb', line 82

def path(join_with = '_')
  (namespace.path(join_with) + join_with unless namespace.nil? or namespace.path.empty?).to_s + @name.to_s
end

#undo!Object

Marks blueprint as not built



75
76
77
# File 'lib/blueprints/buildable.rb', line 75

def undo!
  Namespace.root.executed_blueprints.delete self
end