Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Example comparator diffs #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Hash#diff fix
  • Loading branch information
Marek Kowalcze committed Apr 18, 2016
commit d38faeb2034c2bfe46cff9a2107f7a4a3f9f069c
8 changes: 7 additions & 1 deletion lib/polish_geeks/dev_tools/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class Hash < ::Hash
def diff(other)
diffs = {}

if different_keys_than?(other)
if other.nil?
diff_as_array = keys.each_with_object({}) do |key, result|
result[key] = [self[key], nil]
end

diffs.merge! diff_as_array
elsif different_keys_than?(other)
different_keys = keys - other.keys | other.keys - keys

diff_as_array = different_keys.each_with_object({}) do |key, result|
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/polish_geeks/dev_tools/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

it { expect(subject).to eq(a: { e: [nil, 1], b: { d: [nil, 1] } }) }
end

context 'across 2 levels' do
let(:h1) { described_class.new.merge(a: { b: { c: 1, d: 1 }, e: 1 }) }
let(:h2) { described_class.new.merge(a: { b: nil }) }

it { expect(subject).to eq(a: { e: [1, nil], b: {:c=>[1, nil], :d=>[1, nil] }}) }
end
end

context 'when they have same structure but different data' do
Expand Down