class Hash
Public Instance Methods
assert_exclusive_keys(*exclusive_keys)
click to toggle source
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
# 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)
click to toggle source
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
# 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