diff --git a/lib/rapid/argument_set.rb b/lib/rapid/argument_set.rb index 68a4401..430c0d2 100644 --- a/lib/rapid/argument_set.rb +++ b/lib/rapid/argument_set.rb @@ -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] diff --git a/spec/specs/rapid/argument_set_spec.rb b/spec/specs/rapid/argument_set_spec.rb index dd317c8..b49f3ca 100644 --- a/spec/specs/rapid/argument_set_spec.rb +++ b/spec/specs/rapid/argument_set_spec.rb @@ -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