hash_deep_diff 0.3.0 → 0.3.3

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
  SHA256:
3
- metadata.gz: f27828ee0fbf8a3d07505ed475c3117d2734dd9505e57e3e12d0a360eab4df08
4
- data.tar.gz: b23a5ef94237454e5a50cac7a59e1624a3d8e6f29d249c6d5836ddd596fe097c
3
+ metadata.gz: fe3724394c10b36b7537f35bce5c7f079cae0ee3344062a78bdfe7256363624b
4
+ data.tar.gz: e9baa112b03013b91f75ebea9f243a0a6d51ca746ca8c8e66ea6fa38803198fd
5
5
  SHA512:
6
- metadata.gz: e83d2e0c8e2843f084aa82ea3e246659b6e078a7c3183f03c236bea8e4ac7848d39ed18bc2f81dfa4aecc005af4149372b5d2da28d87760ee4ff1c330f67f54a
7
- data.tar.gz: b56714d24122c5cf9ab11616257cb75a2c2c4cb17225f2b0ad8665cbb64c1b49a6fb0d0f544a6961d5db236ef2e8896ebed8fa0d461479be24629cf6d2ecbf71
6
+ metadata.gz: b7393bb437eddfb8e0ed66d4eba3b4135e221c7c2262df14aa4509dba98345c4766b598bd3c3f11f311c84b25b4f9f0b2bca3dfc5083c023c87c582fa607761a
7
+ data.tar.gz: 57708513050ac1e63c0ce64b9650e0ae0e3128127cbb4dfc5b248cc3caca3267cbd26bed7b939be4413c11f1a7038f5503aef143119a09e336cc113324ae182a
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # HashDeepDiff
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hash_deep_diff`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Find the exact difference between two Hash objects and build a report to visualize it
6
4
 
7
5
  ## Installation
8
6
 
@@ -21,15 +19,20 @@ Or install it yourself as:
21
19
  $ gem install hash_deep_diff
22
20
 
23
21
  ## Usage
22
+ Basic example
24
23
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+ ```ruby
25
+ left = { a: :a }
26
+ right = { a: :b }
30
27
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+ HashDeepDiff::Comparison.new(left, right).report
29
+ ```
30
+ ```diff
31
+ - left[a] = a
32
+ + right[a] = b
33
+ ```
32
34
 
33
35
  ## Contributing
34
36
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hash_deep_diff.
37
+ Bug reports and pull requests are welcome on GitHub at [bpohoriletz](https://github.com/bpohoriletz/hash_deep_diff).
38
+
@@ -26,53 +26,22 @@ module HashDeepDiff
26
26
  @path = path.to_ary
27
27
  end
28
28
 
29
- def extra_report(memo, keys, value)
30
- if value.respond_to?(:to_hash)
31
- value.each_key { |key| extra_report(memo, keys + [key], value[key]) }
32
- else
33
- memo << Delta::Left.new(path: keys, value: value)
34
- end
35
- end
36
-
37
- def missing_report(memo, keys, value)
38
- if value.respond_to?(:to_hash)
39
- value.each_key { |key| missing_report(memo, keys + [key], value[key]) }
40
- else
41
- memo << Delta::Right.new(path: keys, value: value)
42
- end
43
- end
44
-
45
- def delta_report(memo, keys, value)
46
- if value.respond_to?(:to_hash) && value.keys != %i[left right]
47
- value.each_key { |key| delta_report(memo, keys + [key], value[key]) }
48
- elsif value.instance_of?(Array) && value.size == 3 && value.all? { |el| el.respond_to?(:to_hash) }
49
- # [{}, {}, {:i=>:i}]
50
- extra_report(memo, keys, value[0]) unless value[0].empty?
51
- delta_report(memo, keys, value[1]) unless value[1].empty?
52
- missing_report(memo, keys, value[2]) unless value[2].empty?
53
- else
54
- memo << Delta::Inner.new(path: keys, value: value)
55
- end
56
- end
57
-
58
29
  def deep_delta(&block)
59
- result = delta(&block)
60
-
61
- result.each_with_object([]) do |diff, memo|
62
- if left.dig(*diff.path).respond_to?(:to_hash) && right.dig(*diff.path).respond_to?(:to_hash)
63
- self.class.new(left.dig(*diff.path), right.dig(*diff.path), path + diff.path).diff.each do |diff|
64
- memo << diff
65
- end
30
+ delta(&block).flat_map do |diff|
31
+ if diff.complex?
32
+ self.class.new(diff.left, diff.right, diff.path).diff
66
33
  else
67
- memo << diff
34
+ diff
68
35
  end
69
36
  end
70
37
  end
71
38
 
39
+ def left_delta
40
+ left_diff_keys.map { |key| Delta::Left.new(path: path + [key], value: left[key]) }
41
+ end
42
+
72
43
  def right_delta
73
- right_diff_keys.each_with_object([]) do |key, memo|
74
- memo << Delta::Right.new(path: path + [key], value: right[key])
75
- end
44
+ right_diff_keys.map { |key| Delta::Right.new(path: path + [key], value: right[key]) }
76
45
  end
77
46
 
78
47
  def delta(&block)
@@ -88,12 +57,6 @@ module HashDeepDiff
88
57
  end
89
58
  end
90
59
 
91
- def left_delta
92
- left_diff_keys.each_with_object([]) do |key, memo|
93
- memo << Delta::Left.new(path: path + [key], value: left[key])
94
- end
95
- end
96
-
97
60
  def common_keys
98
61
  left.keys & right.keys
99
62
  end
@@ -23,6 +23,7 @@ module HashDeepDiff
23
23
  end
24
24
 
25
25
  # TOFIX poor naming
26
+ # overrides parameter in initializer
26
27
  def path
27
28
  @prefix + [@delta.keys.first]
28
29
  end
@@ -42,6 +43,10 @@ module HashDeepDiff
42
43
  def to_str
43
44
  raise NoMethodError, "expected #{self.class} to implement #to_str"
44
45
  end
46
+
47
+ def complex?
48
+ raise NoMethodError, "expected #{self.class} to implement #complex?"
49
+ end
45
50
  end
46
51
 
47
52
  # Override #initialize method
@@ -50,9 +55,11 @@ module HashDeepDiff
50
55
  # TOFIX this may prohibit usage of hashes with Array keys
51
56
  if path.respond_to?(:to_ary)
52
57
  @delta = { path[-1] => value }
58
+ @value = value
53
59
  @prefix = path[0..-2]
54
60
  else
55
61
  @delta = { path => value }
62
+ @value = value
56
63
  @prefix = []
57
64
  end
58
65
  end
@@ -11,33 +11,29 @@ module HashDeepDiff
11
11
  include Delta::ActsAsDelta
12
12
 
13
13
  def to_str
14
- if @delta.values.first.respond_to?(:to_hash)
15
- if @delta.values.first.keys == %i[left right]
16
- if @delta.values.first[:left].respond_to?(:to_hash) && @delta.values.first[:right].respond_to?(:to_hash)
17
- HashDeepDiff::Comparison.new(
18
- @delta.values.first[:left],
19
- @delta.values.first[:right],
20
- path
21
- ).report
22
- else
23
- lines = <<~Q
24
- -left#{diff_prefix} = #{@delta.values.first[:left]}
25
- +right#{diff_prefix} = #{@delta.values.first[:right]}
26
- Q
27
- lines.strip
28
- end
29
- else
30
- @delta.values.first.keys.map do |key|
31
- self.class.new(path: path + [key], value: @delta.values.first[key])
32
- end.join("\n").strip
33
- end
34
- else
35
- lines = <<~Q
36
- -left#{diff_prefix} = #{@delta.values.first[:left]}
37
- +right#{diff_prefix} = #{@delta.values.first[:right]}
38
- Q
39
- lines.strip
40
- end
14
+ return diff unless complex?
15
+
16
+ HashDeepDiff::Comparison.new(left, right, path).report
17
+ end
18
+
19
+ def diff
20
+ lines = <<~Q
21
+ -left#{diff_prefix} = #{left}
22
+ +right#{diff_prefix} = #{right}
23
+ Q
24
+ lines.strip
25
+ end
26
+
27
+ def complex?
28
+ left.respond_to?(:to_hash) && right.respond_to?(:to_hash)
29
+ end
30
+
31
+ def left
32
+ @value[:left]
33
+ end
34
+
35
+ def right
36
+ @value[:right]
41
37
  end
42
38
  end
43
39
  end
@@ -11,13 +11,19 @@ module HashDeepDiff
11
11
  include Delta::ActsAsDelta
12
12
 
13
13
  def to_str
14
- if @delta.values.first.respond_to?(:to_hash)
15
- @delta.values.first.keys.map do |key|
16
- self.class.new(path: path + [key], value: @delta.values.first[key])
17
- end.join("\n").strip
18
- else
19
- "+left#{diff_prefix} = #{@delta.values.first}"
20
- end
14
+ return "+left#{diff_prefix} = #{left}" unless left.respond_to?(:to_hash)
15
+
16
+ left.keys.map do |key|
17
+ self.class.new(path: path + [key], value: left[key])
18
+ end.join("\n").strip
19
+ end
20
+
21
+ def left
22
+ @value
23
+ end
24
+
25
+ def right
26
+ nil
21
27
  end
22
28
  end
23
29
  end
@@ -11,13 +11,19 @@ module HashDeepDiff
11
11
  include Delta::ActsAsDelta
12
12
 
13
13
  def to_str
14
- if @delta.values.first.respond_to?(:to_hash)
15
- @delta.values.first.keys.map do |key|
16
- self.class.new(path: path + [key], value: @delta.values.first[key])
17
- end.join("\n").strip
18
- else
19
- "-left#{diff_prefix} = #{@delta.values.first}"
20
- end
14
+ return "-left#{diff_prefix} = #{right}" unless right.respond_to?(:to_hash)
15
+
16
+ right.keys.map do |key|
17
+ self.class.new(path: path + [key], value: right[key])
18
+ end.join("\n").strip
19
+ end
20
+
21
+ def left
22
+ nil
23
+ end
24
+
25
+ def right
26
+ @value
21
27
  end
22
28
  end
23
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HashDeepDiff
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_deep_diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bohdan Pohorilets
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-11 00:00:00.000000000 Z
11
+ date: 2022-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler