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.
Instance Method Summary collapse
- #attributes(value = nil) ⇒ Object
-
#build(eval_context, build_once = true, options = {}) ⇒ Object
Builds dependencies of blueprint and then blueprint itself.
-
#build_parents(eval_context) ⇒ Object
Builds all dependencies.
-
#built? ⇒ true, false
Returns if blueprint has been built.
-
#depends_on(*dependencies) ⇒ Object
Defines dependencies of buildable by updating it’s context.
-
#full_name ⇒ String
Returns full name for this buildable.
-
#initialize(name, context) ⇒ Buildable
constructor
Initializes new Buildable object by name and context which it belongs to.
-
#inspect ⇒ String
Returns class, name, attributes and dependencies of buildable in nice formatted string.
-
#path(join_with = '_') ⇒ String
Returns full path to this buildable.
-
#undo! ⇒ Object
Marks blueprint as not built.
Constructor Details
#initialize(name, context) ⇒ Buildable
Initializes new Buildable object by name and context which it belongs to.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/blueprints/buildable.rb', line 11 def initialize(name, context) @context = context if name.nil? default_attribute = Blueprints.config.default_attributes.detect { |attribute| attributes.has_key?(attribute) } name = attributes[default_attribute].parameterize('_') if default_attribute end @name, parents = parse_name(name) depends_on(*parents) namespace.add_child(self) if namespace end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/blueprints/buildable.rb', line 4 def name @name end |
Instance Method Details
#attributes ⇒ Hash #attributes(value) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/blueprints/buildable.rb', line 42 def attributes(value = nil) if value update_context(:attributes => value) else @context.attributes end end |
#build(eval_context, build_once = true, options = {}) ⇒ Object
Builds dependencies of blueprint and then blueprint itself.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/blueprints/buildable.rb', line 54 def build(eval_context, build_once = true, = {}) return result(eval_context) if @building or (built? and build_once and .blank?) @building = true each_namespace { |namespace| namespace.build_parents(eval_context) } build_parents(eval_context) result = build_self(eval_context, build_once, ) Namespace.root.executed_blueprints << self @building = false result end |
#build_parents(eval_context) ⇒ Object
Builds all dependencies. Should be called before building itself. Searches dependencies first in parent then in root namespace.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/blueprints/buildable.rb', line 95 def build_parents(eval_context) @context.dependencies.each do |name| parent = begin namespace[name] rescue BlueprintNotFoundError Namespace.root[name] end parent.build(eval_context) end end |
#built? ⇒ true, false
Returns if blueprint has been built
70 71 72 |
# File 'lib/blueprints/buildable.rb', line 70 def built? Namespace.root.executed_blueprints.include?(self) end |
#depends_on(*dependencies) ⇒ Object
Defines dependencies of buildable by updating it’s context.
32 33 34 |
# File 'lib/blueprints/buildable.rb', line 32 def depends_on(*dependencies) update_context(:dependencies => dependencies) end |
#full_name ⇒ String
Returns full name for this buildable
88 89 90 |
# File 'lib/blueprints/buildable.rb', line 88 def full_name path('.') end |
#inspect ⇒ String
Returns class, name, attributes and dependencies of buildable in nice formatted string.
26 27 28 |
# File 'lib/blueprints/buildable.rb', line 26 def inspect "<##{self.class} name: #{full_name.inspect}, attributes: #{attributes.inspect}, dependencies: #{dependencies.inspect}>" end |
#path(join_with = '_') ⇒ String
Returns full path to this buildable
82 83 84 |
# File 'lib/blueprints/buildable.rb', line 82 def path(join_with = '_') (namespace.path(join_with) + join_with unless namespace.nil? or namespace.path.empty?).to_s + @name.to_s end |