Class: Blueprints::RootNamespace
- 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
-
#eval_context ⇒ Blueprints::EvalContext
Return current eval context or creates a new one.
-
#executed_blueprints ⇒ Object
readonly
Lists of executed blueprints (to prevent executing twice).
Attributes inherited from Namespace
Attributes inherited from Buildable
Instance Method Summary collapse
-
#build(names, current_context = nil, build_once = true) ⇒ Object
Builds blueprints that are passed against current context.
-
#initialize ⇒ RootNamespace
constructor
Initialized new root context.
-
#prebuild(blueprints) ⇒ Object
Sets up global context and executes prebuilt blueprints against it.
-
#setup ⇒ Object
Loads all instance variables from global context to current one.
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
#initialize ⇒ RootNamespace
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_context ⇒ Blueprints::EvalContext
Return current eval context or creates a new one.
63 64 65 |
# File 'lib/blueprints/root_namespace.rb', line 63 def eval_context @eval_context ||= EvalContext.new end |
#executed_blueprints ⇒ Object (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.
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, | self[name].build(eval_context, build_once, ) }.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.
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 |
#setup ⇒ Object
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 |