Class: Blueprints::Buildable
- Inherits:
-
Object
- Object
- Blueprints::Buildable
- Defined in:
- lib/blueprints/buildable.rb
Constant Summary collapse
- BUILDING_MESSAGE =
'While building blueprint'
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.infer_name(attributes) ⇒ String
Infers name of buildable using default attributes from Blueprints.config.
Instance Method Summary collapse
- #attributes(value = nil) ⇒ Object
-
#build(environment, options = {}) ⇒ Object
Builds dependencies of buildable and then buildable itself.
-
#build_parents(environment) ⇒ 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 = '_', current_name = nil) ⇒ 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.
13 14 15 16 17 18 19 20 21 |
# File 'lib/blueprints/buildable.rb', line 13 def initialize(name, context) @context = context name = self.class.infer_name(attributes) if name.nil? @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.
6 7 8 |
# File 'lib/blueprints/buildable.rb', line 6 def name @name end |
Class Method Details
.infer_name(attributes) ⇒ String
Infers name of buildable using default attributes from Blueprints.config
118 119 120 121 |
# File 'lib/blueprints/buildable.rb', line 118 def self.infer_name(attributes) default_attribute = Blueprints.config.default_attributes.detect { |attribute| attributes.has_key?(attribute) } attributes[default_attribute].parameterize('_') if default_attribute end |
Instance Method Details
#attributes ⇒ Hash #attributes(value) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/blueprints/buildable.rb', line 41 def attributes(value = nil) if value update_context(:attributes => value) else @context.attributes end end |
#build(environment, options = {}) ⇒ Object
Builds dependencies of buildable and then buildable itself.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/blueprints/buildable.rb', line 56 def build(environment, = {}) return result(environment) if @building or (built? and not [:rebuild] and [:options].blank?) @building = true result = nil surface_errors do each_namespace { |namespace| namespace.build_parents(environment) } build_parents(environment) result = build_self(environment, ) end Namespace.root.executed_blueprints << self result ensure @building = false end |
#build_parents(environment) ⇒ Object
Builds all dependencies. Should be called before building itself. Searches dependencies first in parent then in root namespace.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/blueprints/buildable.rb', line 103 def build_parents(environment) @context.dependencies.each do |name| parent = begin namespace[name] rescue BlueprintNotFoundError Namespace.root[name] end parent.build(environment) end end |
#built? ⇒ true, false
Returns if blueprint has been built
75 76 77 |
# File 'lib/blueprints/buildable.rb', line 75 def built? Namespace.root.executed_blueprints.include?(self) end |
#depends_on(*dependencies) ⇒ Object
Defines dependencies of buildable by updating it’s context.
31 32 33 |
# File 'lib/blueprints/buildable.rb', line 31 def depends_on(*dependencies) update_context(:dependencies => dependencies) end |
#full_name ⇒ String
Returns full name for this buildable
96 97 98 |
# File 'lib/blueprints/buildable.rb', line 96 def full_name path('.') end |
#inspect ⇒ String
Returns class, name, attributes and dependencies of buildable in nice formatted string.
25 26 27 |
# File 'lib/blueprints/buildable.rb', line 25 def inspect "<##{self.class} name: #{full_name.inspect}, attributes: #{attributes.inspect}, dependencies: #{dependencies.inspect}>" end |
#path(join_with = '_', current_name = nil) ⇒ String
Returns full path to this buildable
88 89 90 91 92 |
# File 'lib/blueprints/buildable.rb', line 88 def path(join_with = '_', current_name = nil) current_name ||= @name namespace_path = namespace.path(join_with) if namespace [namespace_path.presence, current_name].compact.join(join_with) end |