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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 138f8fc905ed5caa8160dc05131bfd1d1f484908
4
- data.tar.gz: 7bc15e4a8d6e0fa94bd1c4bc0f29d67e34ad7dc1
3
+ metadata.gz: e02ff6f11b18eafa65d02ecd98cccf8a789d8cda
4
+ data.tar.gz: 70710b0568a977b1e4a4c4423a1e048a4cd76acc
5
5
  SHA512:
6
- metadata.gz: 702c7c4b34e50a6b410ed191a57e185a55ba96d3ca95c855b68f349fc267f1204ab00bd439fa248d23ab22a456aff5a84c0fac04356004017a7d6ce77076e50d
7
- data.tar.gz: 14211e477a53d725526d6f7c271338a49727d81bafa447842cef00ded7e561184f590478294a1b7a19f868c122e2c60f8e4dd1969e7cc470f78cd6d96477f282
6
+ metadata.gz: 17ff9470571e175b9ea761f8cbd3e0d1bfa06f26d56d08e006dfa877ec3ddc9a4f95355f59b256c3ce43dc4c32f54b0a7a6d3d58a481d39b79df08e2e963b57d
7
+ data.tar.gz: 9451787251c0d041a7d2b014c86bad7bbae3d442fd4bd0ddb9d968996246ca27ac747f61f96c19af66d8fcc99f9774c04b891d80383fc2ff23e54c0bd4b5ee0d
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
7
+ rescue LoadError
8
+ end
@@ -2,25 +2,25 @@ require "hash_diff/version"
2
2
  require "hash_diff/comparison"
3
3
 
4
4
  module HashDiff
5
- class << self
6
- def diff(*args)
7
- Comparison.new(*args).diff
8
- end
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
- def left_diff(*args)
11
- Comparison.new(*args).left_diff
12
- end
15
+ def diff(*args)
16
+ Comparison.new(*args).diff
17
+ end
13
18
 
14
- def right_diff(*args)
15
- Comparison.new(*args).right_diff
16
- end
19
+ def left_diff(*args)
20
+ Comparison.new(*args).left_diff
21
+ end
17
22
 
18
- def patch!
19
- Hash.class_eval do
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
@@ -1,45 +1,39 @@
1
1
  module HashDiff
2
- class Comparison < Struct.new(:left, :right)
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 ||= differences(left, right)
11
+ @diff ||= find_differences { |l, r| [l, r] }
6
12
  end
7
13
 
8
14
  def left_diff
9
- @concern = :left
10
- @left_diff ||= differences(left, right)
15
+ @left_diff ||= find_differences { |_, r| r }
11
16
  end
12
17
 
13
18
  def right_diff
14
- @concern = :right
15
- @right_diff ||= differences(left, right)
19
+ @right_diff ||= find_differences { |l, _| l }
16
20
  end
17
21
 
18
- private
19
-
20
- def concern
21
- @concern ||= :both
22
- end
22
+ protected
23
23
 
24
- def clone(left, right)
25
- self.dup.tap do |inst|
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
- def differences(left, right)
32
- combined_attribute_keys.reduce({}, &reduction_strategy)
33
- end
28
+ private
34
29
 
35
- def reduction_strategy
36
- lambda do |diff, key|
37
- diff[key] = report(key) if not equal?(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 combined_attribute_keys
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? Hash
45
+ value.is_a?(Hash)
52
46
  end
53
47
 
54
48
  def comparable?(key)
55
- hash?(left[key]) and hash?(right[key])
49
+ hash?(left[key]) && hash?(right[key])
56
50
  end
57
51
 
58
- def report(key)
52
+ def report_difference(key, reporter)
59
53
  if comparable?(key)
60
- clone(left[key], right[key]).diff
54
+ self.class.new(left[key], right[key]).find_differences(&reporter)
61
55
  else
62
- report_concern(key)
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
@@ -1,3 +1,3 @@
1
1
  module HashDiff
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -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.7.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-06-23 00:00:00.000000000 Z
12
+ date: 2015-08-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler