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 +4 -4
- data/changelog.md +4 -0
- data/lib/hashdiff/diff.rb +18 -14
- data/lib/hashdiff/util.rb +16 -0
- data/lib/hashdiff/version.rb +1 -1
- data/spec/hashdiff/best_diff_spec.rb +1 -1
- data/spec/hashdiff/diff_spec.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8b0a6ff22612f26b5bca9a05e0aa2a59903d8ea
|
4
|
+
data.tar.gz: 239c5cc0d80aad921ab88ae4712c215598b964e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f32454eb369913c1ef2da688d843d3fa2f06b7d98d14cbea8a586534735cf0ac548beef2c49608e5d637d0569c08567b11b8bc89a575b01c13ec855be7f51f29
|
7
|
+
data.tar.gz: dcd6291074a3038274b07ea772f210050dcd8cd448ed0d9b53a47572206bae3d9a64d19b1807194fd10a5ba69e9466999889fc7b190c4e505198c72b9a8887c2
|
data/changelog.md
CHANGED
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
|
-
|
84
|
-
|
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
|
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
|
-
|
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
|
data/lib/hashdiff/version.rb
CHANGED
data/spec/hashdiff/diff_spec.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2014-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|