Class: Stannum::Constraints::Hashes::IndifferentExtraKeys

Inherits:
ExtraKeys
  • Object
show all
Defined in:
lib/stannum/constraints/hashes/indifferent_extra_keys.rb

Overview

Constraint for validating the keys of an indifferent hash-like object.

When using this constraint, the keys must be strings or symbols, but it does not matter which - a constraint configured with string keys will match a hash with symbol keys, and vice versa.

Examples:

keys       = %i[fuel mass size]
constraint = Stannum::Constraints::Hashes::ExpectedKeys.new(keys)

constraint.matches?({})                                #=> true
constraint.matches?({ fuel: 'Monopropellant' })        #=> true
constraint.matches?({ 'fuel' => 'Monopropellant' })    #=> true
constraint.matches?({ electric: true, fuel: 'Xenon' }) #=> false
constraint.matches?({ fuel: 'LF/O', mass: '1 ton', size: 'Medium' })
#=> true
constraint.matches?(
  { fuel: 'LF', mass: '2 tons', nuclear: true, size: 'Medium' }
)
#=> false

Constant Summary

Constants inherited from ExtraKeys

ExtraKeys::NEGATED_TYPE, ExtraKeys::TYPE

Constants inherited from Base

Base::NEGATED_TYPE, Base::TYPE

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from ExtraKeys

#does_not_match?, #errors_for, #initialize, #matches?

Methods inherited from Base

#==, #clone, #does_not_match?, #dup, #errors_for, #initialize, #match, #matches?, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options

Constructor Details

This class inherits a constructor from Stannum::Constraints::Hashes::ExtraKeys

Instance Method Details

#expected_keysSet

Returns the expected keys.

Returns:

  • (Set)

    the expected keys.



29
30
31
32
33
34
35
# File 'lib/stannum/constraints/hashes/indifferent_extra_keys.rb', line 29

def expected_keys
  keys = options[:expected_keys]

  return indifferent_keys_for(keys) unless keys.is_a?(Proc)

  indifferent_keys_for(keys.call)
end