Skip to content

Commit

Permalink
Merge pull request #51 from pabloh/state_use
Browse files Browse the repository at this point in the history
Add 'State#use'
  • Loading branch information
pabloh authored Aug 7, 2024
2 parents ea09d45 + 8400cd4 commit df14f99
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## [0.12.3] - 2024-08-07
### Changed
- Renamed config option `:auto_wire_options` to `:auto_wire` at `:dry_validation` plugin
- Updated `Pathway::State#unwrap` to raise an `ArgumentError` exception on invalid arguments
### Added
- Provide alias `Pathway::State#use` to `Pathway::State#unwrap`

## [0.12.2] - 2024-08-06
### Added
Expand Down
15 changes: 8 additions & 7 deletions lib/pathway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ def to_hash
@hash
end

def unwrap(&bl)
raise 'a block must be provided' if !block_given?
def use(&bl)
raise ArgumentError, 'a block must be provided' if !block_given?

params = bl.parameters
unless params.all? { |(type,_)| [:block, :key, :keyreq, :keyrest].member?(type) }
raise 'only keyword arguments are supported for unwraping'
end

if params.any? {|(type,_)| type == :keyrest }
if !params.all? { |(type,_)| [:block, :key, :keyreq, :keyrest].member?(type) }
raise ArgumentError, 'only keyword arguments are supported'
elsif params.any? {|(type,_)| type == :keyrest }
bl.call(**to_hash)
else
keys = params.select {|(type,_)| type == :key || type == :keyreq }.map(&:last)
Expand All @@ -99,7 +99,8 @@ def unwrap(&bl)
end

alias_method :to_h, :to_hash
alias_method :u, :unwrap
alias_method :u, :use
alias_method :unwrap, :use
end

module Plugins
Expand Down
6 changes: 3 additions & 3 deletions spec/state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ class SimpleOp < Operation

it 'fails if at least one positional param is defined', :aggregate_failures do
expect { state.unwrap {|pos, input:| } }
.to raise_error('only keyword arguments are supported for unwraping')
.to raise_error('only keyword arguments are supported')
expect { state.unwrap {|input| } }
.to raise_error('only keyword arguments are supported for unwraping')
.to raise_error('only keyword arguments are supported')
end

context 'and it takes a block argument' do
it 'fails if it has positional params' do
expect { state.unwrap {|input, &bl| } }
.to raise_error('only keyword arguments are supported for unwraping')
.to raise_error('only keyword arguments are supported')
end

it 'does not fails if only keyword params', :aggregate_failures do
Expand Down

0 comments on commit df14f99

Please sign in to comment.