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

Constants inherited from Buildable

Buildable::BUILDING_MESSAGE

Instance Attribute Summary collapse

Attributes inherited from Buildable

#name

Instance Method Summary collapse

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

#initializeRootNamespace

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_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, environment, options = {}) ⇒ Object

Builds blueprints that are passed against current context.

Parameters:

  • names (Array<Symbol, String>)

    List of blueprints/namespaces to build.

  • environment

    Object to build blueprints against.

  • options (Hash) (defaults to: {})

    List of options to build this buildable with.

Options Hash (options):

  • :options (Hash) — default: {}

    List of options to be accessible in the body of a blueprint.

  • :rebuild (true, false) — default: false

    If true this buildable is treated as not built yet and is rebuilt even if it was built before.

  • :strategy (Symbol) — default: :default

    Strategy to use when building.

  • :name (Symbol)

    Name of blueprint to use when building. Is usually passed for blueprints with regexp names.

Returns:

  • Result of last blueprint/namespace.



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

def build(names, environment, options = {})
  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, options.merge(:options => opts)) }.last
    else
      self[member].build(environment, options)
    end
  end

  result
end

#prebuild(blueprints) ⇒ Object

Sets up a context and executes prebuilt blueprints against it.

Parameters:

  • blueprints (Array<Symbol, String>)

    Names of blueprints that are prebuilt.



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