Class: Blueprints::Buildable

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprints/buildable.rb

Direct Known Subclasses

Blueprint, Namespace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Buildable

Initializes new Buildable object by name. Name can be Symbol, String or Hash. If Hash is passed, then first key is assumed name, and value(s) of that key are assumed as dependencies. Raises error class of name parameter is not what is expected. Warns if name has already been taken.



10
11
12
13
14
15
16
# File 'lib/blueprints/buildable.rb', line 10

def initialize(name)
  @name, parents = parse_name(name)
  depends_on(*parents)

  Blueprints.warn("Overwriting existing blueprint", self) if Namespace.root and Namespace.root.children[@name]
  Namespace.root.add_child(self) if Namespace.root
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/blueprints/buildable.rb', line 3

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



4
5
6
# File 'lib/blueprints/buildable.rb', line 4

def namespace
  @namespace
end

Class Method Details

.normalize_attributes(attributes) ⇒ Object

Normalizes attributes by changing all :@var to values of @var, and all dependencies to the result of that blueprint.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/blueprints/buildable.rb', line 80

def self.normalize_attributes(attributes)
  attributes = attributes.dup
  attributes.each do |attr, value|
    attributes[attr] = value.blueprint_value if value.respond_to?(:blueprint_value)
    if value.is_a? Symbol and value.to_s =~ /^@.+$/
      STDERR.puts "DEPRECATION WARNING: :@variables are deprecated in favor of `d` method"
      attributes[attr] = Blueprints::Namespace.root.context.instance_variable_get(value)
    end
  end
  attributes
end

Instance Method Details

#attributes(value) ⇒ Object

If value is passed then it sets attributes for this buildable object. Otherwise returns attributes (defaulting to empty Hash)



69
70
71
72
# File 'lib/blueprints/buildable.rb', line 69

def attributes(value)
  @attributes = value
  self
end

#build(build_once = true, options = {}) ⇒ Object

Builds dependencies of blueprint and then blueprint itself.

build_once - pass false if you want to build blueprint again instead of updating old one.

options - list of options to be accessible in the body of a blueprint. Defaults to empty Hash.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/blueprints/buildable.rb', line 29

def build(build_once = true, options = {})
  return result if @building or (built? and build_once and options.blank?)
  @building = true

  each_namespace {|namespace| namespace.build_parents }
  build_parents

  old_options, old_attributes = Namespace.root.context.options, Namespace.root.context.attributes
  Namespace.root.context.options, Namespace.root.context.attributes = options, normalized_attributes.merge(options)
  each_namespace {|namespace| Namespace.root.context.attributes.reverse_merge! namespace.normalized_attributes }

  build_self(build_once)
  Namespace.root.context.options, Namespace.root.context.attributes = old_options, old_attributes
  Namespace.root.executed_blueprints << self
  @building = false
  result
end

#build_parentsObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/blueprints/buildable.rb', line 92

def build_parents
  @parents.each do |p|
    parent = begin
      namespace[p]
    rescue BlueprintNotFoundError
      Namespace.root[p]
    end

    parent.build
  end
end

#built?Boolean

Returns if blueprint has been built

Returns:

  • (Boolean)


58
59
60
# File 'lib/blueprints/buildable.rb', line 58

def built?
  Namespace.root.executed_blueprints.include?(self)
end

#depends_on(*scenarios) ⇒ Object

Defines blueprint dependencies. Used internally, but can be used externally too.



19
20
21
22
# File 'lib/blueprints/buildable.rb', line 19

def depends_on(*scenarios)
  @parents = (@parents || []) + scenarios.map { |s| s.to_sym }
  self
end

#normalized_attributesObject

Returns normalized attributes for that particular blueprint.



75
76
77
# File 'lib/blueprints/buildable.rb', line 75

def normalized_attributes
  Buildable.normalize_attributes(@attributes ||= {})
end

#resultObject

Returns the result of blueprint



48
49
50
# File 'lib/blueprints/buildable.rb', line 48

def result
  Namespace.root.context.instance_variable_get(variable_name)
end

#result=(value) ⇒ Object

Sets the result of blueprint



53
54
55
# File 'lib/blueprints/buildable.rb', line 53

def result=(value)
  Namespace.root.add_variable(variable_name, value)
end

#undo!Object

Marks blueprint as not built



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

def undo!
  Namespace.root.executed_blueprints.delete self
end