hashdiff 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca3dcdddb4d1143b186229d4e7d27e9ded3b752b
4
- data.tar.gz: edf3e5858ede25b65ad79a9fcbbb5903ff60e5b7
3
+ metadata.gz: d8b0a6ff22612f26b5bca9a05e0aa2a59903d8ea
4
+ data.tar.gz: 239c5cc0d80aad921ab88ae4712c215598b964e5
5
5
  SHA512:
6
- metadata.gz: a7f0e3f36f65b01281792c5f81fc7a3d4f1bc6b9e0bcfa47dd65202aabcb5ea93acb956e6deeadc437b6a0a812a517336eb2430b6d9f1c1056ebbb080980a1f0
7
- data.tar.gz: be5bd3ca90b482c7287f65da86b64559cc83f0073901f61f549a627025c277187c160488519ea91a92ab688478bd008af7273fc725e515bd8c6f2f090a074a0a
6
+ metadata.gz: f32454eb369913c1ef2da688d843d3fa2f06b7d98d14cbea8a586534735cf0ac548beef2c49608e5d637d0569c08567b11b8bc89a575b01c13ec855be7f51f29
7
+ data.tar.gz: dcd6291074a3038274b07ea772f210050dcd8cd448ed0d9b53a47572206bae3d9a64d19b1807194fd10a5ba69e9466999889fc7b190c4e505198c72b9a8887c2
data/changelog.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## v0.2.1 2014-7-13
4
+
5
+ * yield added/deleted keys for custom comparison
6
+
3
7
  ## v0.2.0 2014-3-29
4
8
 
5
9
  * support custom comparison blocks
data/lib/hashdiff/diff.rb CHANGED
@@ -80,18 +80,8 @@ module HashDiff
80
80
  opts[:comparison] = block if block_given?
81
81
 
82
82
  # prefer to compare with provided block
83
- if opts[:comparison]
84
- res = opts[:comparison].call(opts[:prefix], obj1, obj2)
85
-
86
- # nil != false here
87
- if res == false
88
- return [['~', opts[:prefix], obj1, obj2]]
89
- elsif res == true
90
- return []
91
- else
92
- # compare with specified built-in helpers
93
- end
94
- end
83
+ result = custom_compare(opts[:comparison], opts[:prefix], obj1, obj2)
84
+ return result if result
95
85
 
96
86
  if obj1.nil? and obj2.nil?
97
87
  return []
@@ -144,7 +134,15 @@ module HashDiff
144
134
  end
145
135
 
146
136
  # add deleted properties
147
- deleted_keys.each {|k| result << ['-', "#{prefix}#{k}", obj1[k]] }
137
+ deleted_keys.each do |k|
138
+ custom_result = custom_compare(opts[:comparison], "#{prefix}#{k}", obj1[k], nil)
139
+
140
+ if custom_result
141
+ result.concat(custom_result)
142
+ else
143
+ result << ['-', "#{prefix}#{k}", obj1[k]]
144
+ end
145
+ end
148
146
 
149
147
  # recursive comparison for common keys
150
148
  common_keys.each {|k| result.concat(diff(obj1[k], obj2[k], opts.merge(prefix: "#{prefix}#{k}"))) }
@@ -152,7 +150,13 @@ module HashDiff
152
150
  # added properties
153
151
  obj2.each do |k, v|
154
152
  unless obj1.key?(k)
155
- result << ['+', "#{prefix}#{k}", obj2[k]]
153
+ custom_result = custom_compare(opts[:comparison], "#{prefix}#{k}", nil, v)
154
+
155
+ if custom_result
156
+ result.concat(custom_result)
157
+ else
158
+ result << ['+', "#{prefix}#{k}", obj2[k]]
159
+ end
156
160
  end
157
161
  end
158
162
  else
data/lib/hashdiff/util.rb CHANGED
@@ -108,4 +108,20 @@ module HashDiff
108
108
  return true if !strict && obj1.is_a?(Numeric) && obj2.is_a?(Numeric)
109
109
  obj1.is_a?(obj2.class) && obj2.is_a?(obj1.class)
110
110
  end
111
+
112
+ # @private
113
+ #
114
+ # try custom comparison
115
+ def self.custom_compare(method, key, obj1, obj2)
116
+ if method
117
+ res = method.call(key, obj1, obj2)
118
+
119
+ # nil != false here
120
+ if res == false
121
+ return [['~', key, obj1, obj2]]
122
+ elsif res == true
123
+ return []
124
+ end
125
+ end
126
+ end
111
127
  end
@@ -1,3 +1,3 @@
1
1
  module HashDiff
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -24,7 +24,7 @@ describe HashDiff do
24
24
  diff = HashDiff.best_diff(a, b) do |path, obj1, obj2|
25
25
  case path
26
26
  when /^x\[.\]\..$/
27
- obj1.length == obj2.length
27
+ obj1.length == obj2.length if obj1 and obj2
28
28
  end
29
29
  end
30
30
 
@@ -204,6 +204,19 @@ describe HashDiff do
204
204
  diff.should == [['~', 'b', 'boat', 'truck']]
205
205
  end
206
206
 
207
+ it 'should yield added keys' do
208
+ x = {a:'car', b:'boat'}
209
+ y = {a:'car'}
210
+
211
+ diff = HashDiff.diff(x, y) do |prefix, obj1, obj2|
212
+ case prefix
213
+ when /b/
214
+ true
215
+ end
216
+ end
217
+ diff.should == []
218
+ end
219
+
207
220
  it 'should compare with both proc and :strip when both provided' do
208
221
  diff = HashDiff.diff(a, b, :strip => true) do |prefix, obj1, obj2|
209
222
  case prefix
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashdiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Fengyun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-30 00:00:00.000000000 Z
11
+ date: 2014-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec