Skip to content

Commit

Permalink
Make HashWithIndifferentAccess handle reverse_merge
Browse files Browse the repository at this point in the history
Fixes #543
  • Loading branch information
tejasbubane committed Mar 1, 2017
1 parent 2b82ae3 commit 75b8ece
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/thor/core_ext/hash_with_indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ def merge!(other)
self
end

def reverse_merge(other)
self.class.new(other).merge(self)
end

def reverse_merge!(other_hash)
replace(reverse_merge(other_hash))
end

def replace(other_hash)
super(other_hash)
end

# Convert to a Hash with String keys.
def to_hash
Hash.new(default).merge!(self)
Expand Down
18 changes: 18 additions & 0 deletions spec/core_ext/hash_with_indifferent_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,22 @@
expect(@hash.to_hash.class).to eq(Hash)
expect(@hash).to eq("foo" => "bar", "baz" => "bee", "force" => true)
end

it "handles reverse_merge" do
other = {:foo => "qux", "boo" => "bae"}
new_hash = @hash.reverse_merge(other)

expect(@hash.object_id).not_to eq(new_hash.object_id)
expect(new_hash[:foo]).to eq("bar")
expect(new_hash[:boo]).to eq("bae")
end

it "handles reverse_merge!" do
other = {:foo => "qux", "boo" => "bae"}
new_hash = @hash.reverse_merge!(other)

expect(@hash.object_id).to eq(new_hash.object_id)
expect(new_hash[:foo]).to eq("bar")
expect(new_hash[:boo]).to eq("bae")
end
end

0 comments on commit 75b8ece

Please sign in to comment.