Class: Blueprints::Buildable

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

Direct Known Subclasses

Blueprint, Namespace

Constant Summary collapse

BUILDING_MESSAGE =
'While building blueprint'

Instance Attribute Summary collapse

Class Method 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.



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

def initialize(name, context)
  @context = context

  name = self.class.infer_name(attributes) if name.nil?
  @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.



6
7
8
# File 'lib/blueprints/buildable.rb', line 6

def name
  @name
end

Class Method Details

.infer_name(attributes) ⇒ String

Infers name of buildable using default attributes from Blueprints.config

Parameters:

  • attributes (Hash)

    Attributes of buildable object to infer the name from.

Returns:

  • (String)

    Inferred name



118
119
120
121
# File 'lib/blueprints/buildable.rb', line 118

def self.infer_name(attributes)
  default_attribute = Blueprints.config.default_attributes.detect { |attribute| attributes.has_key?(attribute) }
  attributes[default_attribute].parameterize('_') if default_attribute
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



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

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

#build(environment, options = {}) ⇒ Object

Builds dependencies of buildable and then buildable itself.

Parameters:

  • environment (Object)

    Context to build buildable object in.

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

    List of options to build this buildable with.

Options Hash (options):

  • :options (Hash) — default: {}

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

  • :rebuild (true, false) — default: false

    If true this buildable is treated as not built yet and is rebuilt even if it was built before.

  • :strategy (Symbol) — default: :default

    Strategy to use when building.

  • :name (Symbol)

    Name of blueprint to use when building. Is usually passed for blueprints with regexp names.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/blueprints/buildable.rb', line 56

def build(environment, options = {})
  return result(environment) if @building or (built? and not options[:rebuild] and options[:options].blank?)
  @building = true

  result = nil
  surface_errors do
    each_namespace { |namespace| namespace.build_parents(environment) }
    build_parents(environment)
    result = build_self(environment, options)
  end
  Namespace.root.executed_blueprints << self

  result
ensure
  @building = false
end

#build_parents(environment) ⇒ Object

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

Parameters:

  • environment (Object)

    Context to build parents against.

Raises:



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/blueprints/buildable.rb', line 103

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

    parent.build(environment)
  end
end

#built?true, false

Returns if blueprint has been built

Returns:

  • (true, false)

    true if was built, false otherwise



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

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.



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

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

#full_nameString

Returns full name for this buildable

Returns:

  • (String)

    full buildable name



96
97
98
# File 'lib/blueprints/buildable.rb', line 96

def full_name
  path('.')
end

#inspectString

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

Returns:

  • (String)

    Inspected properties of buildable.



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

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

#path(join_with = '_', current_name = nil) ⇒ String

Returns full path to this buildable

Parameters:

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

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

  • current_name (#to_s) (defaults to: nil)

    Current name of this buildable. Used for regexp named buildables. Defaults to @name.

Returns:

  • (String)

    full path to this buildable joined with separator



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

def path(join_with = '_', current_name = nil)
  current_name ||= @name
  namespace_path = namespace.path(join_with) if namespace
  [namespace_path.presence, current_name].compact.join(join_with)
end

#undo!Object

Marks blueprint as not built



80
81
82
# File 'lib/blueprints/buildable.rb', line 80

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