Class: Blueprints::RootNamespace

Inherits:
Namespace show all
Defined in:
lib/blueprints/root_namespace.rb

Overview

Defines a root namespace that is used when no other namespace is. Apart from functionality in namespace it also allows building blueprints/namespaces by name. Is also used for copying instance variables between blueprints/contexts/global context.

Constant Summary collapse

@@root =
RootNamespace.new

Instance Attribute Summary collapse

Attributes inherited from Namespace

#children

Attributes inherited from Buildable

#name

Instance Method Summary collapse

Methods inherited from Namespace

#[], #add_child, #build_self, #demolish

Methods inherited from Buildable

#attributes, #build_parents, #built?, #depends_on, #full_name, #inspect, #path, #undo!

Constructor Details

#initializeRootNamespace

Initialized new root context.



13
14
15
16
17
18
# File 'lib/blueprints/root_namespace.rb', line 13

def initialize
  @executed_blueprints = @global_executed_blueprints = []
  @auto_iv_list = Set.new

  super '', Context.new
end

Instance Attribute Details

#eval_contextBlueprints::EvalContext

Return current eval context or creates a new one.

Returns:



63
64
65
# File 'lib/blueprints/root_namespace.rb', line 63

def eval_context
  @eval_context ||= EvalContext.new
end

#executed_blueprintsObject (readonly)

Lists of executed blueprints (to prevent executing twice). Cleared before each test.



8
9
10
# File 'lib/blueprints/root_namespace.rb', line 8

def executed_blueprints
  @executed_blueprints
end

Instance Method Details

#build(names, current_context = nil, build_once = true) ⇒ Object

Builds blueprints that are passed against current context. Copies instance variables to context given if one is given.

Parameters:

  • names (Array<Symbol, String>)

    List of blueprints/namespaces to build.

  • current_context (defaults to: nil)

    Object to copy instance variables from eval context after building to.

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

Returns:

  • Result of last blueprint/namespace.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/blueprints/root_namespace.rb', line 47

def build(names, current_context = nil, build_once = true)
  names = [names] unless names.is_a?(Array)
  result = names.inject(nil) do |result, member|
    if member.is_a?(Hash)
      member.map { |name, options| self[name].build(eval_context, build_once, options) }.last
    else
      self[member].build(eval_context, build_once)
    end
  end

  eval_context.copy_instance_variables(current_context) if current_context
  result
end

#prebuild(blueprints) ⇒ Object

Sets up global context and executes prebuilt blueprints against it.

Parameters:

  • blueprints (Array<Symbol, String>)

    Names of blueprints that are prebuilt.



35
36
37
38
39
40
# File 'lib/blueprints/root_namespace.rb', line 35

def prebuild(blueprints)
  build(blueprints) if blueprints

  @global_executed_blueprints = @executed_blueprints
  @global_variables = Marshal.dump(eval_context.instance_variables.each_with_object({}) { |iv, hash| hash[iv] = eval_context.instance_variable_get(iv) })
end

#setupObject

Loads all instance variables from global context to current one. Creates new eval context.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blueprints/root_namespace.rb', line 21

def setup
  @eval_context = EvalContext.new
  (@executed_blueprints - @global_executed_blueprints).each(&:undo!)
  @executed_blueprints = @global_executed_blueprints.clone

  if Blueprints.config.transactions
    Marshal.load(@global_variables).each { |name, value| eval_context.instance_variable_set(name, value) }
  else
    build(Blueprints.config.prebuild)
  end
end