Class: ObjectForge::Molds::HashMold
- Inherits:
-
Object
- Object
- ObjectForge::Molds::HashMold
- Defined in:
- lib/object_forge/molds/hash_mold.rb
Overview
Mold for constructing Hashes.
Instance Attribute Summary collapse
-
#default ⇒ Any?
readonly
Default value to be assigned to each produced hash.
-
#default_proc ⇒ Proc?
readonly
Default proc to be assigned to each produced hash.
Instance Method Summary collapse
-
#call(forged:, attributes:, **_) ⇒ Hash
Build a new hash using
forged.[]. -
#initialize(default_value = nil) {|hash, key| ... } ⇒ HashMold
constructor
Initialize new HashMold with default value or default proc to be assigned to each produced hash.
Constructor Details
#initialize(default_value = nil) {|hash, key| ... } ⇒ HashMold
Initialize new HashMold with default value or default proc to be assigned to each produced hash.
The same exact objects are used for each hash. It is not advised to use mutable objects as default values. Be aware that using a default proc with assignment is inherently not safe, see this Ruby issue: bugs.ruby-lang.org/issues/19237.
34 35 36 37 |
# File 'lib/object_forge/molds/hash_mold.rb', line 34 def initialize(default_value = nil, &default_proc) @default = default_value @default_proc = default_proc end |
Instance Attribute Details
#default ⇒ Any? (readonly)
Default value to be assigned to each produced hash.
14 15 16 |
# File 'lib/object_forge/molds/hash_mold.rb', line 14 def default @default end |
#default_proc ⇒ Proc? (readonly)
Default proc to be assigned to each produced hash.
17 18 19 |
# File 'lib/object_forge/molds/hash_mold.rb', line 17 def default_proc @default_proc end |
Instance Method Details
#call(forged:, attributes:, **_) ⇒ Hash
Build a new hash using forged.[].
46 47 48 49 50 51 |
# File 'lib/object_forge/molds/hash_mold.rb', line 46 def call(forged:, attributes:, **_) hash = forged[attributes] hash.default = @default if @default hash.default_proc = @default_proc if @default_proc hash end |