hash_diff 0.8.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e02ff6f11b18eafa65d02ecd98cccf8a789d8cda
4
- data.tar.gz: 70710b0568a977b1e4a4c4423a1e048a4cd76acc
3
+ metadata.gz: 0c353c43e17ed7d833fe9edf3fd4e6ec4af96aa6
4
+ data.tar.gz: b8b050b80bb2c69ad5e4b79a96c30cc1ede24092
5
5
  SHA512:
6
- metadata.gz: 17ff9470571e175b9ea761f8cbd3e0d1bfa06f26d56d08e006dfa877ec3ddc9a4f95355f59b256c3ce43dc4c32f54b0a7a6d3d58a481d39b79df08e2e963b57d
7
- data.tar.gz: 9451787251c0d041a7d2b014c86bad7bbae3d442fd4bd0ddb9d968996246ca27ac747f61f96c19af66d8fcc99f9774c04b891d80383fc2ff23e54c0bd4b5ee0d
6
+ metadata.gz: d528f1366911a7b69bff0af027621b0e37ca22b97b4150b4853277548f2ee242701a4320701a9432c6acfe5333b0f2b4df35ca3b0d9e401d16dab9086774086d
7
+ data.tar.gz: c95598db8dadac025c0f6526d55ca43ab4bf2154f782d6d1402ef3da88c7df5be5f3597f014c984e4d6858fd2cb84a364f37d9ee3c37124c73411ebb79e2be3f
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 acuppy
1
+ Copyright (c) 2013 Zeal, LLC (dba. Coding Zeal)
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # HashDiff
2
+
2
3
  [![Build Status](https://travis-ci.org/CodingZeal/hash_diff.png?branch=master)](https://travis-ci.org/CodingZeal/hash_diff) [![Code Climate](https://codeclimate.com/github/CodingZeal/hash_diff.png)](https://codeclimate.com/github/CodingZeal/hash_diff) [![Gem Version](https://badge.fury.io/rb/hash_diff.png)](http://badge.fury.io/rb/hash_diff)
3
4
 
4
5
  Deep comparison of Ruby Hash objects
@@ -31,7 +32,8 @@ Easily find the differences between two Ruby hashes.
31
32
  one: 'foo1'
32
33
  }
33
34
  },
34
- num: 1
35
+ num: 1,
36
+ favorite_restaurant: "Shoney's"
35
37
  }
36
38
 
37
39
  right = {
@@ -43,7 +45,8 @@ Easily find the differences between two Ruby hashes.
43
45
  two: 'foo2'
44
46
  }
45
47
  },
46
- word: 'monkey'
48
+ word: 'monkey',
49
+ favorite_restaurant: nil
47
50
  }
48
51
 
49
52
  hash_diff = HashDiff::Comparison.new( left, right )
@@ -52,19 +55,23 @@ Easily find the differences between two Ruby hashes.
52
55
  Comparison#diff returns the left and right side differences
53
56
 
54
57
  ```ruby
55
- hash_diff.diff # => { foo: ["bar", "bar2"], bar: ["foo", "foo2"], nested: { foo: ["bar", "bar2"], bar: { one: ["foo1", nil], two: [nil, "foo2"] } }, num: [1, nil], word: [nil, "monkey"] }
58
+ hash_diff.diff # => { foo: ["bar", "bar2"], bar: ["foo", "foo2"], nested: { foo: ["bar", "bar2"], bar: { one: ["foo1", HashDiff::NO_VALUE], two: [HashDiff::NO_VALUE, "foo2"] } }, num: [1, HashDiff::NO_VALUE], word: [HashDiff::NO_VALUE, "monkey"], favorite_restaurant: ["Shoney's", nil] }
56
59
  ```
57
60
 
61
+ #### Missing keys
62
+
63
+ When there is a key that exists on one side it will return `HashDiff::NO_VALUE` to represent a missing key.
64
+
58
65
  Comparison#left_diff returns only the left side differences
59
66
 
60
67
  ```ruby
61
- hash_diff.left_diff # => { foo: "bar2", bar: "foo2", nested: { foo: "bar2", bar: { one: nil, two: "foo2" } }, num: nil, word: "monkey" }
68
+ hash_diff.left_diff # => {:foo=>"bar2", :bar=>"foo2", :nested=>{:foo=>"bar2", :bar=>{:one=>HashDiff::NO_VALUE, :two=>"foo2"}}, :num=>HashDiff::NO_VALUE, :favorite_restaurant=>nil, :word=>"monkey"}
62
69
  ```
63
70
 
64
71
  Comparison#right_diff returns only the right side differences
65
72
 
66
73
  ```ruby
67
- hash_diff.right_diff # => { foo: "bar", bar: "foo", nested: { foo: "bar", bar: { one: "foo1", two: nil } }, num: 1, word: nil }
74
+ hash_diff.right_diff # => {:foo=>"bar", :bar=>"foo", :nested=>{:foo=>"bar", :bar=>{:one=>"foo1", :two=>HashDiff::NO_VALUE}}, :num=>1, :favorite_restaurant=>"Shoney's", :word=>HashDiff::NO_VALUE}
68
75
  ```
69
76
 
70
77
  You can also use these shorthand methods
@@ -87,14 +94,11 @@ Hash#diff is not provided by default, and monkey patching is frowned upon by som
87
94
  left.diff(right) # => { foo: 'baz' }
88
95
  ```
89
96
 
90
- ## Credits
91
-
92
- Authored by Adam Cuppy (@acuppy) of Coding ZEAL (http://codingzeal.com)
97
+ ## License
93
98
 
94
- ![Coding ZEAL](https://googledrive.com/host/0B3TWa6M1MsWeWmxRZWhscllwTzA/ZEAL-logo-final-150.png)
99
+ Authored by the Engineering Team of [Coding ZEAL](https://codingzeal.com?utm_source=github)
95
100
 
96
- This is freely distributed under the MIT license. Use it, modify it,
97
- enjoy :)
101
+ Copyright (c) 2017 Zeal, LLC. Licensed under the [MIT license](https://opensource.org/licenses/MIT).
98
102
 
99
103
  ## Contributing
100
104
 
@@ -2,6 +2,8 @@ require "hash_diff/version"
2
2
  require "hash_diff/comparison"
3
3
 
4
4
  module HashDiff
5
+ class NO_VALUE; end
6
+
5
7
  def self.patch!
6
8
  Hash.class_eval do
7
9
  def diff(right)
@@ -38,7 +38,7 @@ module HashDiff
38
38
  end
39
39
 
40
40
  def equal?(key)
41
- left[key] == right[key]
41
+ value_with_default(left, key) == value_with_default(right, key)
42
42
  end
43
43
 
44
44
  def hash?(value)
@@ -53,8 +53,15 @@ module HashDiff
53
53
  if comparable?(key)
54
54
  self.class.new(left[key], right[key]).find_differences(&reporter)
55
55
  else
56
- reporter.call(left[key], right[key])
56
+ reporter.call(
57
+ value_with_default(left, key),
58
+ value_with_default(right, key)
59
+ )
57
60
  end
58
61
  end
62
+
63
+ def value_with_default(obj, key)
64
+ obj.fetch(key, NO_VALUE)
65
+ end
59
66
  end
60
67
  end
@@ -1,3 +1,3 @@
1
1
  module HashDiff
2
- VERSION = "0.8.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -2,10 +2,37 @@ require "spec_helper"
2
2
 
3
3
  describe HashDiff::Comparison do
4
4
 
5
- let(:app_v1_properties) { { foo: 'bar', bar: 'foo', nested: { foo: 'bar', bar: { one: 'foo1' } }, num: 1 } }
6
- let(:app_v2_properties) { { foo: 'bar2', bar: 'foo2', nested: { foo: 'bar2', bar: { two: 'foo2' } }, word: 'monkey' } }
5
+ let(:app_v1_properties) {
6
+ {
7
+ foo: 'bar',
8
+ bar: 'foo',
9
+ nested: {
10
+ foo: 'bar',
11
+ bar: {
12
+ one: 'foo1'
13
+ }
14
+ },
15
+ num: 1,
16
+ word: nil
17
+ }
18
+ }
19
+ let(:app_v2_properties) {
20
+ {
21
+ foo: 'bar2',
22
+ bar: 'foo2',
23
+ nested: {
24
+ foo: 'bar2',
25
+ bar: {
26
+ two: 'foo2'
27
+ }
28
+ },
29
+ word: 'monkey'
30
+ }
31
+ }
7
32
 
8
- subject(:comparison) { HashDiff::Comparison.new(app_v1_properties, app_v2_properties) }
33
+ subject(:comparison) {
34
+ HashDiff::Comparison.new(app_v1_properties, app_v2_properties)
35
+ }
9
36
 
10
37
  describe "#diff" do
11
38
  subject { comparison.diff }
@@ -18,11 +45,11 @@ describe HashDiff::Comparison do
18
45
  nested: {
19
46
  foo: ["bar", "bar2"],
20
47
  bar: {
21
- one: ["foo1", nil],
22
- two: [nil, "foo2"]
48
+ one: ["foo1", HashDiff::NO_VALUE],
49
+ two: [HashDiff::NO_VALUE, "foo2"]
23
50
  }
24
51
  },
25
- num: [1, nil],
52
+ num: [1, HashDiff::NO_VALUE],
26
53
  word: [nil, "monkey"]
27
54
  }
28
55
  }
@@ -57,11 +84,11 @@ describe HashDiff::Comparison do
57
84
  nested: {
58
85
  foo: "bar2",
59
86
  bar: {
60
- one: nil,
87
+ one: HashDiff::NO_VALUE,
61
88
  two: "foo2"
62
89
  }
63
90
  },
64
- num: nil,
91
+ num: HashDiff::NO_VALUE,
65
92
  word: "monkey"
66
93
  }
67
94
  }
@@ -80,7 +107,7 @@ describe HashDiff::Comparison do
80
107
  foo: "bar",
81
108
  bar: {
82
109
  one: "foo1",
83
- two: nil
110
+ two: HashDiff::NO_VALUE
84
111
  }
85
112
  },
86
113
  num: 1,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Coding Zeal
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-08-28 00:00:00.000000000 Z
12
+ date: 2018-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.2.2
96
+ rubygems_version: 2.5.2
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Deep Ruby Hash comparison