-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48ceb08
commit 9342b54
Showing
4 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module TTY | ||
module Option | ||
module ParamPermitted | ||
# Convert parameter value to another type | ||
# | ||
# @example | ||
# param = Parameter::Argument.create(:foo, permit: %w[11 12 13]) | ||
# ParamPermitted[param, "12"] # => 12 | ||
# | ||
# @api public | ||
def call(param, value) | ||
return value unless param.permit? | ||
|
||
if param.permit.include?(value) | ||
value | ||
else | ||
raise UnpermittedArgument, | ||
format("unpermitted argument %s for %s parameter", | ||
value, param.name.inspect) | ||
end | ||
end | ||
module_function :call | ||
|
||
alias :[] :call | ||
module_function :[] | ||
end # ParamPermitted | ||
end # Option | ||
end # TTY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe TTY::Option::ParamPermitted do | ||
it "skips check when no permit setting" do | ||
param = TTY::Option::Parameter::Option.create(:foo) | ||
|
||
expect(described_class[param, "a"]).to eq("a") | ||
end | ||
|
||
it "permits an option argument" do | ||
param = TTY::Option::Parameter::Option.create(:foo, permit: %w[a b c]) | ||
|
||
expect(described_class[param, "a"]).to eq("a") | ||
end | ||
|
||
it "doesn't permit an option arguemnt" do | ||
param = TTY::Option::Parameter::Option.create(:foo, permit: %w[a b c]) | ||
|
||
expect { | ||
described_class[param, "d"] | ||
}.to raise_error(TTY::Option::UnpermittedArgument, | ||
"unpermitted argument d for :foo parameter") | ||
end | ||
end |