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? (::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(forged:, attributes:, **_) ⇒ Struct
Instantiate
forgedstruct 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.
29 30 31 |
# File 'lib/object_forge/molds/struct_mold.rb', line 29 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.
21 22 23 |
# File 'lib/object_forge/molds/struct_mold.rb', line 21 def lax @lax end |
Instance Method Details
#call(forged:, attributes:, **_) ⇒ Struct
Instantiate forged struct with a hash of attributes.
38 39 40 41 42 43 44 45 46 |
# File 'lib/object_forge/molds/struct_mold.rb', line 38 def call(forged:, attributes:, **_) if forged.keyword_init? lax ? forged.new(attributes.slice(*forged.members)) : forged.new(attributes) elsif forged.keyword_init? == false forged.new(*attributes.values_at(*forged.members)) else build_struct_with_unspecified_keyword_init(forged, attributes) end end |