Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/state_machines/assertions.rb

Direct Known Subclasses

StateMachines::MachineCollection

Instance Method Summary collapse

Instance Method Details

#assert_exclusive_keys(*exclusive_keys) ⇒ Object

Validates that the given hash only includes at most one of a set of exclusive keys. If more than one key is found, an ArgumentError will be raised.

Examples

options = {:only => :on, :except => :off}
options.assert_exclusive_keys(:only)                   # => nil
options.assert_exclusive_keys(:except)                 # => nil
options.assert_exclusive_keys(:only, :except)          # => ArgumentError: Conflicting keys: only, except
options.assert_exclusive_keys(:only, :except, :with)   # => ArgumentError: Conflicting keys: only, except

Raises:

  • (ArgumentError)

35
36
37
38
# File 'lib/state_machines/assertions.rb', line 35

def assert_exclusive_keys(*exclusive_keys)
  conflicting_keys = exclusive_keys & keys
  raise ArgumentError, "Conflicting keys: #{conflicting_keys.join(', ')}" unless conflicting_keys.length <= 1
end

#assert_valid_keys(*valid_keys) ⇒ Object

Validate all keys in a hash match *valid_keys, raising ArgumentError on a mismatch. Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols as keys, this will fail.

{ name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
{ name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
{ name: 'Rob', age: '28' }.assert_valid_keys(:name, :age)   # => passes, raises nothing

Code from ActiveSupport


14
15
16
17
18
19
20
21
# File 'lib/state_machines/assertions.rb', line 14

def assert_valid_keys(*valid_keys)
  valid_keys.flatten!
  each_key do |k|
    unless valid_keys.include?(k)
      raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
    end
  end
end