lib/refinements/hashes.rb in refinements-9.8.0 vs lib/refinements/hashes.rb in refinements-10.0.0
- old
+ new
@@ -14,17 +14,15 @@
end
refine Hash do
import_methods Shared::Enumerables::Many
- def compress = dup.compress!
+ def compress = compact.delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
def compress!
- return self if empty?
-
- compact!
delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
+ compact!
end
def deep_merge other
clazz = self.class
@@ -47,28 +45,21 @@
def deep_symbolize_keys! = replace(deep_symbolize_keys)
def fetch_value(key, *default_value, &) = fetch(key, *default_value, &) || default_value.first
- # :reek:TooManyStatements
- def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
- fail StandardError, "Unknown cast: #{cast}." unless %i[to_sym to_s].include? cast
+ def flatten_keys prefix: nil, delimiter: "_"
+ reduce({}) do |accumulator, (key, value)|
+ flat_key = prefix ? "#{prefix}#{delimiter}#{key}".to_sym : key
- reduce({}) do |flat, (key, value)|
- flat_key = prefix ? "#{prefix}#{delimiter}#{key}" : key
+ next accumulator.merge flat_key => value unless value in Hash
- next flat.merge flat_key.public_send(cast) => value unless value in Hash
-
- flat.merge(
- recurse { value.flatten_keys prefix: flat_key, delimiter:, cast: }
- )
+ accumulator.merge(recurse { value.flatten_keys prefix: flat_key, delimiter: })
end
end
- def flatten_keys! prefix: nil, delimiter: "_", cast: :to_sym
- replace flatten_keys(prefix:, delimiter:, cast:)
- end
+ def flatten_keys!(prefix: nil, delimiter: "_") = replace flatten_keys(prefix:, delimiter:)
def recurse &block
return self unless block
transform = yield self
@@ -84,14 +75,10 @@
def symbolize_keys! = transform_keys!(&:to_sym)
def transform_with(operations) = dup.transform_with! operations
def transform_with! operations
- operations.each do |key, function|
- value = self[key]
- self[key] = function.call value if value
- end
-
+ operations.each { |key, function| self[key] = function.call self[key] if key? key }
self
end
def use &block
return [] unless block