Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'State#use' #51

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading