Skip to content

Commit 71f6f54

Browse files
authored
Merge pull request #550 from tejasbubane/hwa-reverse-merge
Make HashWithIndifferentAccess handle reverse_merge
2 parents 2b82ae3 + 75b8ece commit 71f6f54

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/thor/core_ext/hash_with_indifferent_access.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ def merge!(other)
5151
self
5252
end
5353

54+
def reverse_merge(other)
55+
self.class.new(other).merge(self)
56+
end
57+
58+
def reverse_merge!(other_hash)
59+
replace(reverse_merge(other_hash))
60+
end
61+
62+
def replace(other_hash)
63+
super(other_hash)
64+
end
65+
5466
# Convert to a Hash with String keys.
5567
def to_hash
5668
Hash.new(default).merge!(self)

spec/core_ext/hash_with_indifferent_access_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,22 @@
6060
expect(@hash.to_hash.class).to eq(Hash)
6161
expect(@hash).to eq("foo" => "bar", "baz" => "bee", "force" => true)
6262
end
63+
64+
it "handles reverse_merge" do
65+
other = {:foo => "qux", "boo" => "bae"}
66+
new_hash = @hash.reverse_merge(other)
67+
68+
expect(@hash.object_id).not_to eq(new_hash.object_id)
69+
expect(new_hash[:foo]).to eq("bar")
70+
expect(new_hash[:boo]).to eq("bae")
71+
end
72+
73+
it "handles reverse_merge!" do
74+
other = {:foo => "qux", "boo" => "bae"}
75+
new_hash = @hash.reverse_merge!(other)
76+
77+
expect(@hash.object_id).to eq(new_hash.object_id)
78+
expect(new_hash[:foo]).to eq("bar")
79+
expect(new_hash[:boo]).to eq("bae")
80+
end
6381
end

0 commit comments

Comments
 (0)