Skip to content

Commit

Permalink
Merge pull request rails#52234 from kwstannard/indifferent
Browse files Browse the repository at this point in the history
refactor HashWithIndifferentAccess#to_hash
  • Loading branch information
byroot authored Jun 30, 2024
2 parents 88924dd + ed5d646 commit d43ee20
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions activesupport/lib/active_support/hash_with_indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,10 @@ def compact

# Convert to a regular hash with string keys.
def to_hash
_new_hash = Hash.new
set_defaults(_new_hash)

each do |key, value|
_new_hash[key] = convert_value(value, conversion: :to_hash)
end
_new_hash
copy = Hash[self]
copy.transform_values! { |v| convert_value_to_hash(v) }
set_defaults(copy)
copy
end

def to_proc
Expand All @@ -398,11 +395,7 @@ def convert_key(key)

def convert_value(value, conversion: nil)
if value.is_a? Hash
if conversion == :to_hash
value.to_hash
else
value.nested_under_indifferent_access
end
value.nested_under_indifferent_access
elsif value.is_a?(Array)
if conversion != :assignment || value.frozen?
value = value.dup
Expand All @@ -413,6 +406,17 @@ def convert_value(value, conversion: nil)
end
end

def convert_value_to_hash(value)
if value.is_a? Hash
value.to_hash
elsif value.is_a?(Array)
value.map { |e| convert_value_to_hash(e) }
else
value
end
end


def set_defaults(target)
if default_proc
target.default_proc = default_proc.dup
Expand Down

0 comments on commit d43ee20

Please sign in to comment.