Class: Blueprints::Context
- Inherits:
-
Object
- Object
- Blueprints::Context
- Defined in:
- lib/blueprints/context.rb
Overview
Class that blueprint files are evaluated against. Has methods for setting and retrieving attributes and dependencies. Allows defining new blueprints and namespaces.
Constant Summary collapse
- @@chain =
[]
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Class Method Summary collapse
-
.current ⇒ Blueprints::Context
Return current context.
-
.eval_within_context(new_options, &block) ⇒ Object
Creates child context and sets it as current.
Instance Method Summary collapse
-
#==(context) ⇒ true, false
Checks if two contexts are equal by comparing attributes, dependencies, namespace and file.
- #attributes(new_attributes = nil, &block) ⇒ Object
-
#blueprint(name = nil, &block) ⇒ Blueprints::Blueprint
Defines a new blueprint by name and block passed.
-
#d(*args) ⇒ Object
Initializes new Blueprints::Dependency object.
-
#depends_on(*new_dependencies, &block) ⇒ Blueprints::Context
Yields and returns child context that has dependencies set.
-
#find(path) ⇒ Blueprints::Buildable
(also: #[])
Finds blueprint/namespace by it’s path.
-
#initialize(options = {}) ⇒ Context
constructor
Initializes new context with passed parent, attributes, dependencies, file and namespace.
- #namespace(name = nil, &block) ⇒ Object
-
#with_context(options, &block) ⇒ Blueprints::Context
Yields and returns child context that has new options set.
Constructor Details
#initialize(options = {}) ⇒ Context
Initializes new context with passed parent, attributes, dependencies, file and namespace. Attributes and dependencies are automatically merged with parents’ attributes and dependencies. File and namespace are automatically set to parent counterparts unless they are explicitly changed.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/blueprints/context.rb', line 18 def initialize( = {}) .assert_valid_keys(:dependencies, :attributes, :file, :parent, :namespace) @dependencies = ([:dependencies] || []).collect(&:to_sym) @attributes = [:attributes] || {} @file = [:file] @namespace = [:namespace] if parent = [:parent] @attributes.reverse_merge!(parent.attributes) @dependencies = (parent.dependencies + @dependencies).uniq @file ||= parent.file @namespace ||= parent.namespace end end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
7 8 9 |
# File 'lib/blueprints/context.rb', line 7 def dependencies @dependencies end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
7 8 9 |
# File 'lib/blueprints/context.rb', line 7 def file @file end |
Class Method Details
.current ⇒ Blueprints::Context
Return current context.
146 147 148 |
# File 'lib/blueprints/context.rb', line 146 def self.current @@chain.last end |
.eval_within_context(new_options, &block) ⇒ Object
Creates child context and sets it as current. Evaluates block and file within child context if any are passed.
152 153 154 155 156 157 158 159 160 |
# File 'lib/blueprints/context.rb', line 152 def self.eval_within_context(, &block) @@chain << context = new() file = [:file] context.instance_eval(File.read(file), file) if file context.instance_eval(&block) if block @@chain.pop end |
Instance Method Details
#==(context) ⇒ true, false
Checks if two contexts are equal by comparing attributes, dependencies, namespace and file
36 37 38 |
# File 'lib/blueprints/context.rb', line 36 def ==(context) @dependencies == context.dependencies and @attributes == context.attributes and @file == context.file and @namespace == context.namespace end |
#attributes(new_attributes, &block) ⇒ Blueprints::Context #attributes ⇒ Hash
95 96 97 98 99 100 101 |
# File 'lib/blueprints/context.rb', line 95 def attributes(new_attributes = nil, &block) if new_attributes with_context(:attributes => new_attributes, &block) else @attributes end end |
#blueprint(name = nil, &block) ⇒ Blueprints::Blueprint
Defines a new blueprint by name and block passed.
47 48 49 |
# File 'lib/blueprints/context.rb', line 47 def blueprint(name = nil, &block) Blueprint.new(name, self, &block) end |
#d(name, options = {}) ⇒ Object #d(name, instance_variable_name, options = {}) ⇒ Object
Initializes new Blueprints::Dependency object.
132 133 134 |
# File 'lib/blueprints/context.rb', line 132 def d(*args) Dependency.new(*args) end |
#depends_on(*new_dependencies, &block) ⇒ Blueprints::Context
Yields and returns child context that has dependencies set.
113 114 115 |
# File 'lib/blueprints/context.rb', line 113 def depends_on(*new_dependencies, &block) with_context(:dependencies => new_dependencies, &block) end |
#find(path) ⇒ Blueprints::Buildable Also known as: []
Finds blueprint/namespace by it’s path
139 140 141 |
# File 'lib/blueprints/context.rb', line 139 def find(path) @namespace[path] end |
#namespace(name, &block) ⇒ Blueprints::Namespace #namespace ⇒ Blueprints::Namespace
64 65 66 67 68 69 70 71 72 |
# File 'lib/blueprints/context.rb', line 64 def namespace(name = nil, &block) if name Namespace.new(name, self).tap do |namespace| with_context(:namespace => namespace, &block) end else @namespace end end |
#with_context(options, &block) ⇒ Blueprints::Context
Yields and returns child context that has new options set.
120 121 122 |
# File 'lib/blueprints/context.rb', line 120 def with_context(, &block) Context.eval_within_context(.merge(:parent => self), &block) end |