Class: ObjectForge::Molds::StructMold
- Inherits:
-
Object
- Object
- ObjectForge::Molds::StructMold
- Defined in:
- lib/object_forge/molds/struct_mold.rb
Overview
Mold for building Structs.
Supports all variations of keyword_init.
Constant Summary collapse
- RUBY_FEATURE_AUTO_KEYWORDS =
Does Struct automatically use keyword initialization when
keyword_initis not specified /nil?This should be true on Ruby 3.2.0 and later.
(::Struct.new(:a, :b).new(a: 1, b: 2).a == 1)
Instance Attribute Summary collapse
-
#lax ⇒ Boolean
(also: #lax?)
readonly
Whether to work around argument hashes with extra keys.
Instance Method Summary collapse
-
#call(forge_target:, attributes:, **_) ⇒ Struct
Instantiate target struct with a hash of attributes.
-
#initialize(lax: true) ⇒ StructMold
constructor
A new instance of StructMold.
Constructor Details
#initialize(lax: true) ⇒ StructMold
Returns a new instance of StructMold.
31 32 33 |
# File 'lib/object_forge/molds/struct_mold.rb', line 31 def initialize(lax: true) @lax = lax end |
Instance Attribute Details
#lax ⇒ Boolean (readonly) Also known as: lax?
Whether to work around argument hashes with extra keys.
23 24 25 |
# File 'lib/object_forge/molds/struct_mold.rb', line 23 def lax @lax end |
Instance Method Details
#call(forge_target:, attributes:, **_) ⇒ Struct
Instantiate target struct with a hash of attributes.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/object_forge/molds/struct_mold.rb', line 40 def call(forge_target:, attributes:, **_) if forge_target.keyword_init? if lax forge_target.new(attributes.slice(*forge_target.members)) else forge_target.new(attributes) end elsif forge_target.keyword_init? == false forge_target.new(*attributes.values_at(*forge_target.members)) else build_struct_with_unspecified_keyword_init(forge_target, attributes) end end |