Class: ObjectForge::Crucible
- Inherits:
-
UnBasicObject
- Object
- UnBasicObject
- ObjectForge::Crucible
- Defined in:
- lib/object_forge/crucible.rb,
sig/object_forge.rbs
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.
Constant Summary collapse
- EMPTY_YARD =
steep:ignore UnannotatedEmptyCollection
{}.freeze
Instance Attribute Summary collapse
-
#yard ⇒ Hash{Symbol => Any}, Forgeyard
readonly
Additional context for the crucible.
Class Method Summary collapse
-
.call(attributes, yard: EMPTY_YARD) ⇒ Hash{Symbol => Any}
(also: resolve)
Resolve all attributes by calling their +Proc+s, using a new instance as evaluation context.
Instance Method Summary collapse
-
#initialize(attributes, yard: EMPTY_YARD) ⇒ Crucible
constructor
A new instance of Crucible.
-
#resolve! ⇒ Hash{Symbol => Any}
Resolve all attributes by calling their +Proc+s, using
selfas the evaluation context.
Methods inherited from UnBasicObject
#block_given?, #class, #eql?, #freeze, #frozen?, #hash, #inspect, #is_a?, #kind_of?, #pretty_print, #pretty_print_cycle, #raise, #respond_to?, #to_s
Constructor Details
#initialize(attributes, yard: EMPTY_YARD) ⇒ Crucible
Returns a new instance of Crucible.
44 45 46 47 48 49 50 |
# File 'lib/object_forge/crucible.rb', line 44 def initialize(attributes, yard: EMPTY_YARD) super() @attributes = attributes @yard = yard || EMPTY_YARD @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.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/object_forge/crucible.rb', line 99 def method_missing(name) if @attributes.key?(name) raise_circular_dependency_error!(name) if @resolving_attributes.include?(name) resolve_attribute!(name) unless @resolved_attributes.include?(name) @attributes[name] elsif @yard.key?(name) @yard[name] else super end end |
Instance Attribute Details
#yard ⇒ Hash{Symbol => Any}, Forgeyard (readonly)
Returns additional context for the crucible.
40 41 42 |
# File 'lib/object_forge/crucible.rb', line 40 def yard @yard end |
Class Method Details
.call(attributes, yard: EMPTY_YARD) ⇒ Hash{Symbol => Any} Also known as: resolve
This method destructively modifies initial attributes.
Resolve all attributes by calling their +Proc+s, using a new instance as evaluation context.
30 31 32 |
# File 'lib/object_forge/crucible.rb', line 30 def call(attributes, yard: EMPTY_YARD) new(attributes, yard:).resolve! end |
Instance Method Details
#resolve! ⇒ Hash{Symbol => Any}
This method destructively modifies initial attributes.
Resolve all attributes by calling their +Proc+s,
using self as the evaluation context.
Attributes can freely refer to each other inside +Proc+s
through bareword names or #[].
However, make sure to avoid cyclic dependencies:
they can't be resolved and will raise ObjectForge::CircularAttributeDependencyError.
65 66 67 68 |
# File 'lib/object_forge/crucible.rb', line 65 def resolve! @attributes.each_key { |name| method_missing(name) } @attributes end |