Skip to content

Commit

Permalink
feat: add ArgumentSet#has? to say if an argument set has a value
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Dec 7, 2020
1 parent 54f253b commit c0b7643
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rapid/argument_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ def to_hash
end
end

# Return whether an argument has been provided or not?
#
# @param name [Symbol]
# @return [Boolean]
def has?(key)
@source.key?(key.to_sym)
end

# Validate an argument set and return any errors as appropriate
#
# @param argument [Rapid::Argument]
Expand Down
35 changes: 35 additions & 0 deletions spec/specs/rapid/argument_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,41 @@
end
end

context '#has?' do
it 'returns false when the argument has not been provided' do
as = Rapid::ArgumentSet.create('ExampleSet') do
argument :name, type: :string
end
instance = as.new({})
expect(instance.has?(:name)).to be false
end

it 'returns false for values that are provided but are not valid' do
as = Rapid::ArgumentSet.create('ExampleSet') do
argument :name, type: :string
end
instance = as.new({ age: 10 })
expect(instance.has?(:age)).to be false
end

it 'returns true when the argument has been provided but is nil' do
as = Rapid::ArgumentSet.create('ExampleSet') do
argument :name, type: :string
end
instance = as.new({ name: nil })
expect(instance.has?(:name)).to be true
end

it 'returns true when the argument has been provided' do
as = Rapid::ArgumentSet.create('ExampleSet') do
argument :name, type: :string
end
instance = as.new({ name: 'Dave' })
expect(instance.has?(:name)).to be true
expect(instance.has?('name')).to be true
end
end

context '#to_hash' do
it 'should return a hash of the values' do
as = Rapid::ArgumentSet.create('ExampleSet') do
Expand Down

0 comments on commit c0b7643

Please sign in to comment.