Skip to content

Commit

Permalink
Add minimum arity lookup to parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Apr 18, 2020
1 parent c17f56c commit 7b2fffc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/tty/option/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def check_settings!(settings)
end
end

def default_arity
1
end

def arity(value = (not_set = true))
if not_set
@settings.fetch(:arity) { default_arity }
Expand All @@ -51,6 +47,17 @@ def arity(value = (not_set = true))
end
end

def default_arity
1
end

# Determine minimum boundary for arity parameter
#
# @api private
def min_arity
arity < 0 ? arity.abs - 1 : arity
end

# Check if multiple occurrences are allowed
#
# @return [Boolean]
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/parameter/argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
it "defaults to 1" do
arg = described_class.new(:foo)
expect(arg.arity).to eq(1)
expect(arg.min_arity).to eq(1)
expect(arg.multiple?).to eq(false)
end

Expand All @@ -37,12 +38,21 @@
it "accepts * as zero or more arity" do
arg = described_class.new(:foo, arity: "*")
expect(arg.arity).to eq(-1)
expect(arg.min_arity).to eq(0)
expect(arg.multiple?).to eq(true)
end

it "accepts :any as zero or more arity" do
arg = described_class.new(:foo, arity: :any)
expect(arg.arity).to eq(-1)
expect(arg.min_arity).to eq(0)
expect(arg.multiple?).to eq(true)
end

it "accepts one or more arity" do
arg = described_class.new(:foo, arity: -2)
expect(arg.arity).to eq(-2)
expect(arg.min_arity).to eq(1)
expect(arg.multiple?).to eq(true)
end
end
Expand Down

0 comments on commit 7b2fffc

Please sign in to comment.