Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#requires_fields(*_keys) ⇒ Object

Backwards compatability with older versions



3
# File 'lib/hash_plus.rb', line 3

def requires_fields(*_keys); requires_keys_are_not_nil(*_keys); end

#requires_keys_are_nil(*_keys) ⇒ Object

Requires that the values for these keys be nil



23
24
25
26
27
28
# File 'lib/hash_plus.rb', line 23

def requires_keys_are_nil(*_keys)
  invalid_keys = _keys.reject { |k| self[k].nil? }
  unless invalid_keys.empty?
    raise ArgumentError, "Non-Nil values for keys=#{invalid_keys.join(',')} in opts=#{self.inspect}"
  end
end

#requires_keys_are_not_nil(*_keys) ⇒ Object

Requires that the values for these keys be non-nil



15
16
17
18
19
20
# File 'lib/hash_plus.rb', line 15

def requires_keys_are_not_nil(*_keys)
  invalid_keys = _keys.select { |k| self[k].nil? }
  unless invalid_keys.empty?
    raise ArgumentError, "Nil values for keys=#{invalid_keys.join(',')} in opts=#{self.inspect}"
  end
end

#requires_keys_are_present(*_keys) ⇒ Object

Requires that the key simply be present The hash can contain any value for these keys



7
8
9
10
11
12
# File 'lib/hash_plus.rb', line 7

def requires_keys_are_present(*_keys)
  invalid_keys = _keys - self.keys
  unless invalid_keys.empty?
    raise ArgumentError, "Missing keys=#{invalid_keys.join(',')} in opts=#{self.inspect}"
  end
end