Module: Vident::Stimulus::Combinable
- Included in:
- Base
- Defined in:
- lib/vident/stimulus/combinable.rb
Overview
Shared ‘with(**overrides)` combinator for the frozen value classes. Mirrors Ruby’s ‘Data.define#with` convention (which Literal::Data doesn’t ship) so callers can decorate a value object without mutating it.
Instance Method Summary collapse
- #deconstruct ⇒ Object
-
#deconstruct_keys(keys) ⇒ Object
Canonical Ruby Data-object hooks.
- #with(**overrides) ⇒ Object
Instance Method Details
#deconstruct ⇒ Object
21 22 23 |
# File 'lib/vident/stimulus/combinable.rb', line 21 def deconstruct deconstruct_keys(nil).values end |
#deconstruct_keys(keys) ⇒ Object
Canonical Ruby Data-object hooks. The value classes override ‘to_h` to serialise to their data-attribute shape; without this module, `deconstruct_keys` would inherit that override and pattern-matching (`case a; in event:`) would silently fail.
14 15 16 17 18 19 |
# File 'lib/vident/stimulus/combinable.rb', line 14 def deconstruct_keys(keys) h = self.class.literal_properties.properties_index.each_with_object({}) do |(name, _), acc| acc[name.to_sym] = public_send(name) end keys ? h.slice(*keys) : h end |
#with(**overrides) ⇒ Object
25 26 27 |
# File 'lib/vident/stimulus/combinable.rb', line 25 def with(**overrides) self.class.new(**deconstruct_keys(nil).merge(overrides)) end |