lib/active_object/hash.rb in active_object-2.2.5 vs lib/active_object/hash.rb in active_object-2.3.0
- old
+ new
@@ -4,11 +4,11 @@
def assert_valid_keys(*valid_keys)
valid_keys.flatten!
each_key do |k|
unless valid_keys.include?(k)
raise ArgumentError,
- "Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
+ "Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}"
end
end
end
end
@@ -39,10 +39,11 @@
this_value.deep_merge(other_value, &block)
else
block_given? && key?(current_key) ? block.call(current_key, this_value, other_value) : other_value
end
end
+
self
end
end
def except(*keys)
@@ -57,15 +58,11 @@
def nillify
dup.nillify!
end
def nillify!
- each do |k, v|
- if !v.nil? && ((v.respond_to?(:blank?) && v.blank?) || (v.respond_to?(:to_s) && v.to_s.blank?))
- self[k] = nil
- end
- end
+ each { |k, v| self[k] = nil if !v.nil? && (v.try(:blank?) || v.try(:to_s).blank?) }
end
def only(*keys)
dup.only!(*keys)
end
@@ -163,12 +160,12 @@
end
end
unless defined?(Rails)
def stringify_keys!
- inject({}) do |options,(k, v)|
- options[k.to_s] = v
+ inject({}) do |options, (key, value)|
+ options[key.to_s] = value
options
end
end
end
@@ -186,24 +183,24 @@
end
end
unless defined?(Rails)
def symbolize_keys!
- inject({}) do |options, (k, v)|
- options[(k.to_sym rescue k) || k] = v
+ inject({}) do |options, (key, value)|
+ options[(key.to_sym rescue key) || key] = value
options
end
end
end
def symbolize_and_underscore_keys
dup.symbolize_and_underscore_keys!
end
def symbolize_and_underscore_keys!
- inject({}) do |options, (k, v)|
- options[(k.to_s.gsub(" ", "_").underscore.to_sym rescue k) || k] = v
+ inject({}) do |options, (key, value)|
+ options[(key.to_s.gsub(' '.freeze, '_'.freeze).underscore.to_sym rescue key) || key] = value
options
end
end
unless defined?(Rails)
@@ -213,10 +210,11 @@
end
unless defined?(Rails)
def transform_keys!(&block)
return(enum_for(:transform_keys!)) unless block_given?
+
keys.each { |k| self[yield(k)] = delete(k) }
self
end
end
@@ -227,9 +225,10 @@
end
unless defined?(Rails)
def transform_values!(&block)
return(enum_for(:transform_values!)) unless block_given?
+
each { |k, v| self[k] = yield(v) }
end
end
end
\ No newline at end of file