Class: ObjectForge::Crucible
- Inherits:
-
UnBasicObject
- Object
- UnBasicObject
- ObjectForge::Crucible
- Defined in:
- lib/object_forge/crucible.rb
Overview
This class is not intended to be used directly, but it’s not a private API.
Melting pot for the forged object’s attributes.
Class Method Summary collapse
-
.call(attributes) ⇒ Hash{Symbol => Any}
(also: resolve)
Resolve all attributes by calling their Procs, using a new instance as evaluation context.
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Crucible
constructor
A new instance of Crucible.
-
#resolve! ⇒ Hash{Symbol => Any}
Resolve all attributes by calling their Procs, using
selfas the evaluation context.
Methods inherited from UnBasicObject
#class, #eql?, #freeze, #frozen?, #hash, #inspect, #is_a?, #pretty_print, #pretty_print_cycle, #respond_to?, #to_s
Constructor Details
#initialize(attributes) ⇒ Crucible
Returns a new instance of Crucible.
37 38 39 40 41 42 |
# File 'lib/object_forge/crucible.rb', line 37 def initialize(attributes) super() @attributes = attributes @resolved_attributes = ::Set.new @resolving_attributes = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Any (private) Also known as: []
Get the value of the attribute name.
To prevent problems with calling methods which may be defined, #[] can be used instead.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/object_forge/crucible.rb', line 91 def method_missing(name) # rubocop:disable Metrics/MethodLength if @attributes.key?(name) if @resolving_attributes.include?(name) raise_circular_dependency_error!(name) elsif !@resolved_attributes.include?(name) && (::Proc === @attributes[name]) begin @resolving_attributes << name @attributes[name] = instance_exec(&@attributes[name]) @resolved_attributes << name ensure @resolving_attributes.pop end end @attributes[name] else super end end |
Class Method Details
.call(attributes) ⇒ Hash{Symbol => Any} Also known as: resolve
This method destructively modifies initial attributes.
Resolve all attributes by calling their Procs, using a new instance as evaluation context.
27 28 29 |
# File 'lib/object_forge/crucible.rb', line 27 def call(attributes) new(attributes).resolve! end |
Instance Method Details
#resolve! ⇒ Hash{Symbol => Any}
This method destructively modifies initial attributes.
Resolve all attributes by calling their Procs, using self as the evaluation context.
Attributes can freely refer to each other inside Procs through bareword names or #[]. However, make sure to avoid cyclic dependencies: they can’t be resolved and will raise ObjectForge::CircularAttributeDependencyError.
57 58 59 60 |
# File 'lib/object_forge/crucible.rb', line 57 def resolve! @attributes.each_key { |name| method_missing(name) } @attributes end |