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, #namespace

Instance Method Summary collapse

Methods inherited from Namespace

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

Methods inherited from Buildable

#attributes, #build_parents, #built?, #depends_on, normalize_attributes, #normalized_attributes, #result, #result=, #undo!

Constructor Details

#initializeRootNamespace

Returns a new instance of RootNamespace.



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

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

  super ''
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#executed_blueprintsObject (readonly)

Returns the value of attribute executed_blueprints.



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

def executed_blueprints
  @executed_blueprints
end

Instance Method Details

#add_variable(name, value) ⇒ Object

Sets instance variable in current context to passed value. If instance variable with same name already exists, it is set only if it was set using this same method



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

def add_variable(name, value)
  if not @context.instance_variable_defined?(name) or @auto_iv_list.include?(name)
    @auto_iv_list << name
    @context.instance_variable_set(name, value)
  end
end

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

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



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

def build(names, 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(build_once, options) }.last
    else
      self[member].build(build_once)
    end
  end

  copy_ivars(context) if context
  result
end

#copy_ivars(to) ⇒ Object

Copies all instance variables from current context to another one.



29
30
31
32
33
# File 'lib/blueprints/root_namespace.rb', line 29

def copy_ivars(to)
  @context.instance_variables.each do |iv|
    to.instance_variable_set(iv, @context.instance_variable_get(iv))
  end
end

#prebuild(blueprints) ⇒ Object

Sets up global context and executes prebuilt blueprints against it.



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

def prebuild(blueprints)
  @context = Blueprints::Context.new
  build(blueprints) if blueprints

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

#setupObject

Loads all instance variables from global context to current one.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/blueprints/root_namespace.rb', line 16

def setup
  (@executed_blueprints - @global_executed_blueprints).each(&:undo!)
  @executed_blueprints = @global_executed_blueprints.clone
  @context = Blueprints::Context.new

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