diff --git a/CHANGELOG.md b/CHANGELOG.md index 427b212..767d117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [0.12.2] - 2024-07-27 +### Added +- Add `Pathway::State#unwrap` and `Pathway::State#u` to access internal state + ## [0.12.1] - 2024-06-23 ### Added - Add support for pattern matching on `Result`, `State` and `Error` instances diff --git a/lib/pathway.rb b/lib/pathway.rb index 8c2df84..4b79d55 100644 --- a/lib/pathway.rb +++ b/lib/pathway.rb @@ -63,13 +63,13 @@ def default_message_for(type) class State extend Forwardable + delegate %i([] []= fetch store include? values_at deconstruct_keys) => :@hash + def initialize(operation, values = {}) @hash = operation.context.merge(values) @result_key = operation.result_key end - delegate %i([] []= fetch store include? values_at deconstruct_keys) => :@hash - def update(kargs) @hash.update(kargs) self @@ -83,7 +83,24 @@ def to_hash @hash end - alias :to_h :to_hash + def unwrap(&bl) + params = bl.parameters + unless params.size > 0 && params.all? { _1 in [:key|:keyreq|:keyrest, *] } + raise 'only keyword arguments are supported for unwraping' + end + + case params + in [*, [:keyrest, b], *] + bl.call(**to_hash) + else + keys = params.select { _1 in [:key|:keyreq,*] }.map(&:last) + + bl.call(**to_hash.slice(*keys)) + end + end + + alias_method :to_h, :to_hash + alias_method :u, :unwrap end module Plugins diff --git a/lib/pathway/plugins/auto_deconstruct_state/ruby3.rb b/lib/pathway/plugins/auto_deconstruct_state/ruby3.rb index 589c5f8..1578616 100644 --- a/lib/pathway/plugins/auto_deconstruct_state/ruby3.rb +++ b/lib/pathway/plugins/auto_deconstruct_state/ruby3.rb @@ -8,6 +8,7 @@ module DSLMethods def _callable(callable) if callable.is_a?(Symbol) && @operation.respond_to?(callable, true) && + @operation.method(callable).arity != 0 && @operation.method(callable).parameters.all? { _1 in [:key|:keyreq|:keyrest|:block,*] } -> state { @operation.send(callable, **state) } diff --git a/lib/pathway/version.rb b/lib/pathway/version.rb index b0092ab..87941e4 100644 --- a/lib/pathway/version.rb +++ b/lib/pathway/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Pathway - VERSION = '0.12.1' + VERSION = '0.12.2' end diff --git a/pathway.gemspec b/pathway.gemspec index fd71d15..bf6fe98 100644 --- a/pathway.gemspec +++ b/pathway.gemspec @@ -43,4 +43,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "pry" spec.add_development_dependency "pry-byebug" spec.add_development_dependency "pry-doc" + spec.add_development_dependency "pry-stack" end diff --git a/spec/plugins/auto_deconstruct_state_spec.rb b/spec/plugins/auto_deconstruct_state_spec.rb index d952abd..8c433e0 100644 --- a/spec/plugins/auto_deconstruct_state_spec.rb +++ b/spec/plugins/auto_deconstruct_state_spec.rb @@ -36,8 +36,10 @@ def create_model(name:, email:, **) UserModel.new(name, email) end - def notify(value:, **) - @notifier.call(value) + def notify(s) + s.u do |value:| + @notifier.call(value) + end end end