Class: Blueprints::Namespace

Inherits:
Buildable show all
Defined in:
lib/blueprints/namespace.rb

Overview

Namespace class, inherits from Buildable. Allows adding and finding child blueprints/namespaces and building all it’s children.

Direct Known Subclasses

RootNamespace

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) ⇒ Namespace

Creates namespace by name. See Buildable#new.

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.



12
13
14
15
# File 'lib/blueprints/namespace.rb', line 12

def initialize(name, context)
  @children = {}
  super(name, context)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

Instance Method Details

#[](path) ⇒ Blueprints::Buildable

Finds child by relative name.

Parameters:

  • path (String)

    Path to child. Path parts should be joined with ‘.’ symbol.

Returns:

Raises:



28
29
30
31
32
33
34
35
36
37
# File 'lib/blueprints/namespace.rb', line 28

def [](path)
  child_name, path = path.to_s.split('.', 2)

  child = @children[child_name.to_sym] or raise BlueprintNotFoundError, child_name
  if path
    child[path]
  else
    child
  end
end

#add_child(child) ⇒ Object

Adds child to namespaces children. Warns if this will overwrite existing child.

Parameters:



19
20
21
22
# File 'lib/blueprints/namespace.rb', line 19

def add_child(child)
  Blueprints.warn("Overwriting existing blueprint", child) if @children[child.name]
  @children[child.name] = child
end

#build_self(eval_context, build_once, options) ⇒ Array

Builds all children and sets an instance variable named by name of namespace with the results.

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.

Returns:

  • (Array)

    Results of all blueprints.



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

def build_self(eval_context, build_once, options)
  result(eval_context) { @children.values.collect { |child| child.build(eval_context, build_once, options) }.uniq }
end

#demolish(eval_context) ⇒ Object

Demolishes all child blueprints and namespaces.

Parameters:



50
51
52
# File 'lib/blueprints/namespace.rb', line 50

def demolish(eval_context)
  @children.each_value { |blueprint| blueprint.demolish(eval_context) }
end