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

Constant Summary

Constants inherited from Buildable

Buildable::BUILDING_MESSAGE

Instance Attribute Summary

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



11
12
13
14
15
16
17
18
19
# File 'lib/blueprints/namespace.rb', line 11

def initialize(name, context)
  @children = Hash.new do |hash, search_key|
    pair = hash.detect do |name,|
      name.is_a?(Regexp) and search_key.to_s =~ name
    end
    hash[search_key] = BlueprintNameProxy.new(search_key, pair[1]) if pair
  end
  super(name, context)
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:



38
39
40
41
42
43
44
45
46
47
# File 'lib/blueprints/namespace.rb', line 38

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:



23
24
25
26
# File 'lib/blueprints/namespace.rb', line 23

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

#childrenArray<Blueprints::Buildable>

Returns all direct children blueprints and namespaces of this namespace.

Returns:



30
31
32
# File 'lib/blueprints/namespace.rb', line 30

def children
  @children.values
end

#demolish(environment) ⇒ Object

Demolishes all child blueprints and namespaces.

Parameters:

  • environment (Object)

    Eval context that this namespace was built in.



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

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