Module: ObjectForge::Molds
- Defined in:
- lib/object_forge/molds.rb,
lib/object_forge/molds/hash_mold.rb,
lib/object_forge/molds/struct_mold.rb,
lib/object_forge/molds/wrapped_mold.rb,
lib/object_forge/molds/keywords_mold.rb,
lib/object_forge/molds/single_argument_mold.rb
Overview
This module provides a collection of predefined molds to be used in common cases.
Mold is an object that knows how to take a hash of attributes and create an object from them. Molds are #callable objects responsible for actually building objects produced by factories (or doing other, interesting things with them (truly, only the code review is the limit!)). They are supposed to be immutable, shareable, and persistent: initialize once, use for the whole runtime.
A simple mold can easily be just a Proc. All molds must have the following #call signature: call(forged:, attributes:, **). The extra keywords are ignored for possibility of future extensions.
Defined Under Namespace
Classes: HashMold, KeywordsMold, SingleArgumentMold, StructMold, WrappedMold
Class Method Summary collapse
-
.mold_for(forged) ⇒ #call
Get maybe appropriate mold for the given
forgedclass or object. -
.wrap_mold(mold) ⇒ #call?
Wrap mold if needed.
Class Method Details
.mold_for(forged) ⇒ #call
Get maybe appropriate mold for the given forged class or object.
Currently provides specific recognition for:
-
subclasses of
Struct(StructMold), -
subclasses of
Data(KeywordsMold), -
Hashand subclasses (HashMold).
Other objects just get SingleArgumentMold.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/object_forge/molds.rb', line 78 def self.mold_for(forged) if ::Class === forged if forged < ::Struct StructMold.new elsif defined?(::Data) && forged < ::Data KeywordsMold.new elsif forged <= ::Hash HashMold.new else SingleArgumentMold.new end else SingleArgumentMold.new end end |
.wrap_mold(mold) ⇒ #call?
Wrap mold if needed.
If mold is nil or a callable object, returns it. If it is a Class with #call, wraps it in WrappedMold. Otherwise, raises an error.
109 110 111 112 113 114 115 116 117 |
# File 'lib/object_forge/molds.rb', line 109 def self.wrap_mold(mold) if mold.nil? || mold.respond_to?(:call) mold # : ObjectForge::mold? elsif ::Class === mold && mold.public_method_defined?(:call) WrappedMold.new(mold) else raise MoldError, "mold must respond to or implement #call" end end |