Class: Blueprints::Buildable
- Inherits:
-
Object
- Object
- Blueprints::Buildable
- Defined in:
- lib/blueprints/buildable.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
Class Method Summary collapse
-
.normalize_attributes(attributes) ⇒ Object
Normalizes attributes by changing all :@var to values of @var, and all dependencies to the result of that blueprint.
Instance Method Summary collapse
-
#attributes(value) ⇒ Object
If value is passed then it sets attributes for this buildable object.
-
#build(build_once = true, options = {}) ⇒ Object
Builds dependencies of blueprint and then blueprint itself.
- #build_parents ⇒ Object
-
#built? ⇒ Boolean
Returns if blueprint has been built.
-
#depends_on(*scenarios) ⇒ Object
Defines blueprint dependencies.
-
#initialize(name) ⇒ Buildable
constructor
Initializes new Buildable object by name.
-
#normalized_attributes ⇒ Object
Returns normalized attributes for that particular blueprint.
-
#result ⇒ Object
Returns the result of blueprint.
-
#result=(value) ⇒ Object
Sets the result of blueprint.
-
#undo! ⇒ Object
Marks blueprint as not built.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/blueprints/buildable.rb', line 3 def name @name end |
#namespace ⇒ Object
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, = {}) return result if @building or (built? and build_once and .blank?) @building = true each_namespace {|namespace| namespace.build_parents } build_parents , old_attributes = Namespace.root.context., Namespace.root.context.attributes Namespace.root.context., Namespace.root.context.attributes = , normalized_attributes.merge() each_namespace {|namespace| Namespace.root.context.attributes.reverse_merge! namespace.normalized_attributes } build_self(build_once) Namespace.root.context., Namespace.root.context.attributes = , old_attributes Namespace.root.executed_blueprints << self @building = false result end |
#build_parents ⇒ Object
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
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_attributes ⇒ Object
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 |
#result ⇒ Object
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 |