Unfortunately Hash#deep_transform_values
takes in block only values, not keys. And unfortunately Hash#deep_merge
and Hash#deep_merge!
are not so powerful
You can write your own method(s). It is just idea, you can improve it
class Hash
def deep_replace(hash)
deep_dup.deep_replace!(hash)
end
def deep_replace!(hash)
each_key do |key|
hash.each do |k, v|
self[key] = v if key == k
end
_replace_object!(self[key], hash)
end
end
private
def _replace_object!(object, hash)
case object
when Hash
object.deep_replace!(hash)
when Array
object.map! { |v| _replace_object!(v, hash) }
else
object
end
end
end
hash = {:id=>"11ed35b8e53c442ea210c39d6f24bddf",
:createdAt=>"2022-09-16T12:12:55.454Z",
:updatedAt=>"2022-09-16T12:12:55.454Z",
:status=>"ACTIVE",
:description=>"test",
:goals=>
[{:Definitions=>[{:text=>"Search for relics", :status=>"INACTIVE", :id=>"11ec1fc4bd36f876963867013cee2799"}],
:text=>"Follows Action Plan Appropriately, Worse or Warning Symptoms",
:status=>"ACTIVE",
:id=>"11e818be2f0157329c76634ee23c1d8f"}],
:healthConcernDefinitions=>[]}
And after that you can apply it
# Replace one key
hash.deep_replace(status: "TRANSFORMED")
# => {:id=>"11ed35b8e53c442ea210c39d6f24bddf",
# :createdAt=>"2022-09-16T12:12:55.454Z",
# :updatedAt=>"2022-09-16T12:12:55.454Z",
# :status=>"TRANSFORMED",
# :description=>"test",
# :goals=>
# [{:Definitions=>[{:text=>"Search for relics", :status=>"TRANSFORMED", :id=>"11ec1fc4bd36f876963867013cee2799"}],
# :text=>"Follows Action Plan Appropriately, Worse or Warning Symptoms",
# :status=>"TRANSFORMED",
# :id=>"11e818be2f0157329c76634ee23c1d8f"}],
# :healthConcernDefinitions=>[]}
# Replace few keys
hash.deep_replace(status: "TRANSFORMED", txid: "555", id: "99999999999999999999999999999999")
# => {:id=>"99999999999999999999999999999999",
# :createdAt=>"2022-09-16T12:12:55.454Z",
# :updatedAt=>"2022-09-16T12:12:55.454Z",
# :status=>"TRANSFORMED",
# :description=>"test",
# :goals=>
# [{:Definitions=>[{:text=>"Search for relics", :status=>"TRANSFORMED", :id=>"99999999999999999999999999999999"}],
# :text=>"Follows Action Plan Appropriately, Worse or Warning Symptoms",
# :status=>"TRANSFORMED",
# :id=>"99999999999999999999999999999999"}],
# :healthConcernDefinitions=>[]}
# Check original (it wasn't changed)
hash
# => {:id=>"11ed35b8e53c442ea210c39d6f24bddf",
# :createdAt=>"2022-09-16T12:12:55.454Z",
# :updatedAt=>"2022-09-16T12:12:55.454Z",
# :status=>"ACTIVE",
# :description=>"test",
# :goals=>
# [{:Definitions=>[{:text=>"Search for relics", :status=>"INACTIVE", :id=>"11ec1fc4bd36f876963867013cee2799"}],
# :text=>"Follows Action Plan Appropriately, Worse or Warning Symptoms",
# :status=>"ACTIVE",
# :id=>"11e818be2f0157329c76634ee23c1d8f"}],
# :healthConcernDefinitions=>[]}
# Destructive method
hash.deep_replace!(status: "TRANSFORMED", id: "99999999999999999999999999999999")
# => {:id=>"99999999999999999999999999999999",
# :createdAt=>"2022-09-16T12:12:55.454Z",
# :updatedAt=>"2022-09-16T12:12:55.454Z",
# :status=>"TRANSFORMED",
# :description=>"test",
# :goals=>
# [{:Definitions=>[{:text=>"Search for relics", :status=>"TRANSFORMED", :id=>"99999999999999999999999999999999"}],
# :text=>"Follows Action Plan Appropriately, Worse or Warning Symptoms",
# :status=>"TRANSFORMED",
# :id=>"99999999999999999999999999999999"}],
# :healthConcernDefinitions=>[]}
# Check original (it was changed)
hash
# => {:id=>"99999999999999999999999999999999",
# :createdAt=>"2022-09-16T12:12:55.454Z",
# :updatedAt=>"2022-09-16T12:12:55.454Z",
# :status=>"TRANSFORMED",
# :description=>"test",
# :goals=>
# [{:Definitions=>[{:text=>"Search for relics", :status=>"TRANSFORMED", :id=>"99999999999999999999999999999999"}],
# :text=>"Follows Action Plan Appropriately, Worse or Warning Symptoms",
# :status=>"TRANSFORMED",
# :id=>"99999999999999999999999999999999"}],
# :healthConcernDefinitions=>[]}