hash_diff 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +7 -0
- data/lib/hash_diff.rb +17 -17
- data/lib/hash_diff/comparison.rb +24 -38
- data/lib/hash_diff/version.rb +1 -1
- data/spec/spec_helper.rb +1 -2
- 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: e02ff6f11b18eafa65d02ecd98cccf8a789d8cda
|
4
|
+
data.tar.gz: 70710b0568a977b1e4a4c4423a1e048a4cd76acc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17ff9470571e175b9ea761f8cbd3e0d1bfa06f26d56d08e006dfa877ec3ddc9a4f95355f59b256c3ce43dc4c32f54b0a7a6d3d58a481d39b79df08e2e963b57d
|
7
|
+
data.tar.gz: 9451787251c0d041a7d2b014c86bad7bbae3d442fd4bd0ddb9d968996246ca27ac747f61f96c19af66d8fcc99f9774c04b891d80383fc2ff23e54c0bd4b5ee0d
|
data/Rakefile
CHANGED
data/lib/hash_diff.rb
CHANGED
@@ -2,25 +2,25 @@ require "hash_diff/version"
|
|
2
2
|
require "hash_diff/comparison"
|
3
3
|
|
4
4
|
module HashDiff
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def self.patch!
|
6
|
+
Hash.class_eval do
|
7
|
+
def diff(right)
|
8
|
+
HashDiff.left_diff(self, right)
|
9
|
+
end
|
10
|
+
end unless Hash.new.respond_to?(:diff)
|
11
|
+
end
|
12
|
+
|
13
|
+
module_function
|
9
14
|
|
10
|
-
|
11
|
-
|
12
|
-
|
15
|
+
def diff(*args)
|
16
|
+
Comparison.new(*args).diff
|
17
|
+
end
|
13
18
|
|
14
|
-
|
15
|
-
|
16
|
-
|
19
|
+
def left_diff(*args)
|
20
|
+
Comparison.new(*args).left_diff
|
21
|
+
end
|
17
22
|
|
18
|
-
|
19
|
-
|
20
|
-
def diff right
|
21
|
-
HashDiff.left_diff self, right
|
22
|
-
end
|
23
|
-
end unless self.class.respond_to? :diff
|
24
|
-
end
|
23
|
+
def right_diff(*args)
|
24
|
+
Comparison.new(*args).right_diff
|
25
25
|
end
|
26
26
|
end
|
data/lib/hash_diff/comparison.rb
CHANGED
@@ -1,45 +1,39 @@
|
|
1
1
|
module HashDiff
|
2
|
-
class Comparison
|
3
|
-
|
2
|
+
class Comparison
|
3
|
+
def initialize(left, right)
|
4
|
+
@left = left
|
5
|
+
@right = right
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :left, :right
|
9
|
+
|
4
10
|
def diff
|
5
|
-
@diff ||=
|
11
|
+
@diff ||= find_differences { |l, r| [l, r] }
|
6
12
|
end
|
7
13
|
|
8
14
|
def left_diff
|
9
|
-
@
|
10
|
-
@left_diff ||= differences(left, right)
|
15
|
+
@left_diff ||= find_differences { |_, r| r }
|
11
16
|
end
|
12
17
|
|
13
18
|
def right_diff
|
14
|
-
@
|
15
|
-
@right_diff ||= differences(left, right)
|
19
|
+
@right_diff ||= find_differences { |l, _| l }
|
16
20
|
end
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
def concern
|
21
|
-
@concern ||= :both
|
22
|
-
end
|
22
|
+
protected
|
23
23
|
|
24
|
-
def
|
25
|
-
|
26
|
-
inst.left = left
|
27
|
-
inst.right = right
|
28
|
-
end
|
24
|
+
def find_differences(&reporter)
|
25
|
+
combined_keys.each_with_object({ }, &comparison_strategy(reporter))
|
29
26
|
end
|
30
27
|
|
31
|
-
|
32
|
-
combined_attribute_keys.reduce({}, &reduction_strategy)
|
33
|
-
end
|
28
|
+
private
|
34
29
|
|
35
|
-
def
|
36
|
-
lambda do |
|
37
|
-
diff[key] =
|
38
|
-
diff
|
30
|
+
def comparison_strategy(reporter)
|
31
|
+
lambda do |key, diff|
|
32
|
+
diff[key] = report_difference(key, reporter) unless equal?(key)
|
39
33
|
end
|
40
34
|
end
|
41
35
|
|
42
|
-
def
|
36
|
+
def combined_keys
|
43
37
|
(left.keys + right.keys).uniq
|
44
38
|
end
|
45
39
|
|
@@ -48,26 +42,18 @@ module HashDiff
|
|
48
42
|
end
|
49
43
|
|
50
44
|
def hash?(value)
|
51
|
-
value.is_a?
|
45
|
+
value.is_a?(Hash)
|
52
46
|
end
|
53
47
|
|
54
48
|
def comparable?(key)
|
55
|
-
hash?(left[key])
|
49
|
+
hash?(left[key]) && hash?(right[key])
|
56
50
|
end
|
57
51
|
|
58
|
-
def
|
52
|
+
def report_difference(key, reporter)
|
59
53
|
if comparable?(key)
|
60
|
-
|
54
|
+
self.class.new(left[key], right[key]).find_differences(&reporter)
|
61
55
|
else
|
62
|
-
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def report_concern(key)
|
67
|
-
case concern
|
68
|
-
when :left then right[key]
|
69
|
-
when :right then left[key]
|
70
|
-
when :both then [left[key], right[key]]
|
56
|
+
reporter.call(left[key], right[key])
|
71
57
|
end
|
72
58
|
end
|
73
59
|
end
|
data/lib/hash_diff/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'rspec'
|
2
|
-
require 'rspec/autorun'
|
3
2
|
|
4
3
|
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
|
5
4
|
$LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
|
@@ -9,4 +8,4 @@ require 'hash_diff'
|
|
9
8
|
|
10
9
|
RSpec.configure do |c|
|
11
10
|
c.filter_run_excluding :skip_on_windows => !(RbConfig::CONFIG['host_os'] =~ /mingw32/).nil?
|
12
|
-
end
|
11
|
+
end
|
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.
|
4
|
+
version: 0.8.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-
|
12
|
+
date: 2015-08-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|