lib/active_object/hash.rb in active_object-4.0.4 vs lib/active_object/hash.rb in active_object-4.0.5
- old
+ new
@@ -2,17 +2,26 @@
def assert_valid_keys(*valid_keys)
valid_keys.flatten!
each_key do |key|
- unless valid_keys.include?(key)
- raise ArgumentError,
- "Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
- end
+ next if valid_keys.include?(key)
+
+ raise ArgumentError,
+ "Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
end
end
+ def assert_valid_keys!(*valid_keys)
+ if empty?
+ raise ArgumentError,
+ "Empty hash. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
+ else
+ assert_valid_keys(*valid_keys)
+ end
+ end
+
def compact
select { |_, val| !val.nil? }
end
def compact!
@@ -41,17 +50,14 @@
end
# rubocop:enable Metrics/MethodLength
def dig(key, *rest)
value = (self[key] rescue nil)
- return if value.nil?
- if rest.empty?
- value
- elsif value.respond_to?(:dig)
- value.dig(*rest)
- end
+ return if value.nil?
+ return value if rest.empty?
+ return value.dig(*rest) if value.respond_to?(:dig)
end
def except(*keys)
dup.except!(*keys)
end
@@ -146,13 +152,12 @@
def shuffle!
replace(shuffle)
end
def slice(*keys)
- keys
- .flatten
- .each_with_object(self.class.new) { |key, hsh| hsh[key] = self[key] if key?(key) }
+ keys.flatten
+ .each_with_object(self.class.new) { |key, hsh| hsh[key] = self[key] if key?(key) }
end
def slice!(*keys)
omit = slice(*self.keys - keys)
hash = slice(*keys)
@@ -167,13 +172,11 @@
def stringify_keys
dup.stringify_keys!
end
def stringify_keys!
- each_with_object({}) do |(key, val), options|
- options[key.to_s] = val
- end
+ each_with_object({}) { |(key, val), options| options[key.to_s] = val }
end
def strip
select { |_, val| !val.blank? }
end
@@ -185,12 +188,10 @@
def symbolize_keys
dup.symbolize_keys!
end
def symbolize_keys!
- each_with_object({}) do |(key, val), options|
- options[(key.to_sym rescue key) || key] = val
- end
+ each_with_object({}) { |(key, val), options| options[(key.to_sym rescue key) || key] = val }
end
def symbolize_and_underscore_keys
dup.symbolize_and_underscore_keys!
end