Skip to content

Commit

Permalink
Add State#unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Jul 27, 2024
1 parent 3720962 commit 9ba6c33
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 20 additions & 3 deletions lib/pathway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/pathway/plugins/auto_deconstruct_state/ruby3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion lib/pathway/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Pathway
VERSION = '0.12.1'
VERSION = '0.12.2'
end
1 change: 1 addition & 0 deletions pathway.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions spec/plugins/auto_deconstruct_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9ba6c33

Please sign in to comment.