Skip to content

Commit

Permalink
Add State#unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Aug 5, 2024
1 parent 3720962 commit 638aed4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 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
17 changes: 16 additions & 1 deletion lib/pathway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,22 @@ def to_hash
@hash
end

alias :to_h :to_hash
def unwrap(&bl)
params = bl.parameters
unless params.size > 0 && params.all? { |(type,_)| [:key, :keyreq, :keyrest].member?(type) }
raise 'only keyword arguments are supported for unwraping'
end

if params.any? {|(type,_)| type == :keyrest }
bl.call(**to_hash)
else
keys = params.select {|(type,_)| type == :key || type == :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 638aed4

Please sign in to comment.