Skip to content

Commit c0b7643

Browse files
committed
feat: add ArgumentSet#has? to say if an argument set has a value
1 parent 54f253b commit c0b7643

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/rapid/argument_set.rb

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ def to_hash
109109
end
110110
end
111111

112+
# Return whether an argument has been provided or not?
113+
#
114+
# @param name [Symbol]
115+
# @return [Boolean]
116+
def has?(key)
117+
@source.key?(key.to_sym)
118+
end
119+
112120
# Validate an argument set and return any errors as appropriate
113121
#
114122
# @param argument [Rapid::Argument]

spec/specs/rapid/argument_set_spec.rb

+35
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,41 @@
342342
end
343343
end
344344

345+
context '#has?' do
346+
it 'returns false when the argument has not been provided' do
347+
as = Rapid::ArgumentSet.create('ExampleSet') do
348+
argument :name, type: :string
349+
end
350+
instance = as.new({})
351+
expect(instance.has?(:name)).to be false
352+
end
353+
354+
it 'returns false for values that are provided but are not valid' do
355+
as = Rapid::ArgumentSet.create('ExampleSet') do
356+
argument :name, type: :string
357+
end
358+
instance = as.new({ age: 10 })
359+
expect(instance.has?(:age)).to be false
360+
end
361+
362+
it 'returns true when the argument has been provided but is nil' do
363+
as = Rapid::ArgumentSet.create('ExampleSet') do
364+
argument :name, type: :string
365+
end
366+
instance = as.new({ name: nil })
367+
expect(instance.has?(:name)).to be true
368+
end
369+
370+
it 'returns true when the argument has been provided' do
371+
as = Rapid::ArgumentSet.create('ExampleSet') do
372+
argument :name, type: :string
373+
end
374+
instance = as.new({ name: 'Dave' })
375+
expect(instance.has?(:name)).to be true
376+
expect(instance.has?('name')).to be true
377+
end
378+
end
379+
345380
context '#to_hash' do
346381
it 'should return a hash of the values' do
347382
as = Rapid::ArgumentSet.create('ExampleSet') do

0 commit comments

Comments
 (0)