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
Constants inherited from Buildable
Instance Attribute Summary collapse
-
#executed_blueprints ⇒ Object
readonly
Lists of executed blueprints (to prevent executing twice).
Attributes inherited from Buildable
Instance Method Summary collapse
-
#build(names, environment, options = {}) ⇒ Object
Builds blueprints that are passed against current context.
-
#initialize ⇒ RootNamespace
constructor
Initialized new root context.
-
#prebuild(blueprints) ⇒ Object
Sets up a context and executes prebuilt blueprints against it.
-
#setup(environment) ⇒ Object
Loads all instance variables from global context to current one.
Methods inherited from Namespace
#[], #add_child, #children, #demolish
Methods inherited from Buildable
#attributes, #build_parents, #built?, #depends_on, #full_name, infer_name, #inspect, #path, #undo!
Constructor Details
#initialize ⇒ RootNamespace
Initialized new root context.
11 12 13 14 15 16 |
# File 'lib/blueprints/root_namespace.rb', line 11 def initialize @executed_blueprints = @global_executed_blueprints = [] @auto_iv_list = Set.new super '', Context.new end |
Instance Attribute Details
#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, environment, options = {}) ⇒ Object
Builds blueprints that are passed against current context.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/blueprints/root_namespace.rb', line 47 def build(names, environment, = {}) names = [names] unless names.is_a?(Array) result = names.inject(nil) do |result, member| if member.is_a?(Hash) member.map { |name, opts| self[name].build(environment, .merge(:options => opts)) }.last else self[member].build(environment, ) end end result end |
#prebuild(blueprints) ⇒ Object
Sets up a context and executes prebuilt blueprints against it.
32 33 34 35 36 37 38 39 |
# File 'lib/blueprints/root_namespace.rb', line 32 def prebuild(blueprints) environment = Object.new environment.extend Blueprints::Helper build(blueprints, environment) if blueprints @global_executed_blueprints = @executed_blueprints @global_variables = Marshal.dump(environment.instance_variables.each_with_object({}) { |iv, hash| hash[iv] = environment.instance_variable_get(iv) }) end |
#setup(environment) ⇒ Object
Loads all instance variables from global context to current one.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/blueprints/root_namespace.rb', line 19 def setup(environment) (@executed_blueprints - @global_executed_blueprints).each(&:undo!) @executed_blueprints = @global_executed_blueprints.clone if Blueprints.config.transactions Marshal.load(@global_variables).each { |name, value| environment.instance_variable_set(name, value) } else build(Blueprints.config.prebuild, environment) end end |