hash_deep_diff 0.3.2 → 0.4.1

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: 184fe8aaacd6b6d10635f8ff8f0986381c8e2d907b14a80d86050aea75896136
4
- data.tar.gz: e4e33cc8c4ce40d84b0fbbe78fa380d1d62f65805f3ce5dc01e2049100db5b16
3
+ metadata.gz: 965ed70dc327febfcab65e1d8a9ae2511e06b117316a368140eae6a00dc8afba
4
+ data.tar.gz: f905ef6c174c729ec28f9d585db116deb32727276ae61b1e448f4c17917cbf47
5
5
  SHA512:
6
- metadata.gz: b299b5217a613175c47e5d56da723e0691cf7e2344abc9bbf9fab9525c96c776616971636365f0c2067f05ec8d22c205c98d896df4b1b17ee73f3541bb4e5736
7
- data.tar.gz: 159ffbeb7cdef03c130ed356f965379db05ebf8adec8b7e5186e1a45b4cbcc68489ce17b92a7e2bc38e58cdd2fafcb31c573703f566f348b0efdc0ef8c504d4e
6
+ metadata.gz: 6669aa02e12598277b0d59e0abb1f173a1a28dfb5aa9770e5c209bde4972bb7e9166788dfcae30743fcc95050bf23104e768edae1ecec855f07b72fa704ad46a
7
+ data.tar.gz: d4f9fc51fc4acbaaf0228f84c8aa47e84cb1b17dbad6e45df7047df0cf086b7ff6deeab85de648ec5916174b0d12e1a982bac975d9a05ef65609714f23ab91dc
@@ -0,0 +1,39 @@
1
+ name: CI
2
+ on:
3
+ pull_request:
4
+
5
+ jobs:
6
+ minitest:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - name: Checkout Repo
10
+ uses: actions/checkout@v2
11
+
12
+ - name: Install Ruby
13
+ uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6
16
+ bundler-cache: false
17
+
18
+ - name: Install Gems
19
+ run: bin/bundle
20
+
21
+ - name: Run Minitest
22
+ run: bin/rake test
23
+ rubocop:
24
+ runs-on: ubuntu-latest
25
+ steps:
26
+ - name: Checkout Repo
27
+ uses: actions/checkout@v2
28
+
29
+ - name: Install Ruby
30
+ uses: ruby/setup-ruby@v1
31
+ with:
32
+ ruby-version: 2.6
33
+ bundler-cache: false
34
+
35
+ - name: Install Gems
36
+ run: bin/bundle
37
+
38
+ - name: Run Rubocop
39
+ run: bin/rubocop
data/CHANGELOG.md ADDED
File without changes
data/Guardfile CHANGED
@@ -20,7 +20,6 @@
20
20
  guard :rubocop do
21
21
  watch(/.+\.rb$/)
22
22
  watch(/.+\.gemspec$/)
23
- watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
24
23
  end
25
24
 
26
25
  guard :minitest do
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 - 2022 Bohdan Pohorilets
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # HashDeepDiff
2
+ [![Gem
3
+ Version](https://badge.fury.io/rb/hash_deep_diff.svg)](https://badge.fury.io/rb/hash_deep_diff) ![GitHub Workflow
4
+ Status](https://img.shields.io/github/workflow/status/bpohoriletz/hash_deep_diff/CI)
5
+ ![GitHub](https://img.shields.io/github/license/bpohoriletz/hash_deep_diff)
2
6
 
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
7
 
5
- TODO: Delete this and the text above, and describe your gem
8
+ Find the exact difference between two Hash objects and build a report to visualize it
6
9
 
7
10
  ## Installation
8
11
 
@@ -21,15 +24,20 @@ Or install it yourself as:
21
24
  $ gem install hash_deep_diff
22
25
 
23
26
  ## Usage
27
+ Basic example
24
28
 
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.
29
+ ```ruby
30
+ left = { a: :a }
31
+ right = { a: :b }
30
32
 
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).
33
+ HashDeepDiff::Comparison.new(left, right).report
34
+ ```
35
+ ```diff
36
+ - left[a] = a
37
+ + left[a] = b
38
+ ```
32
39
 
33
40
  ## Contributing
34
41
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hash_deep_diff.
42
+ Bug reports and pull requests are welcome on GitHub at [bpohoriletz](https://github.com/bpohoriletz/hash_deep_diff).
43
+
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/testtask'
5
5
 
6
6
  Rake::TestTask.new do |t|
7
7
  t.libs << 'test'
8
- t.test_files = FileList['test/**/*_spec.rb']
8
+ t.test_files = FileList['test/hash_deep_diff/test_*.rb']
9
9
  t.verbose = true
10
10
  end
11
11
 
@@ -39,7 +39,7 @@ Gem::Specification.new do |spec|
39
39
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
40
40
  spec.require_paths = ['lib']
41
41
 
42
- spec.add_development_dependency 'bundler', '~> 1.17.2'
42
+ spec.add_development_dependency 'bundler', '~> 2.3.11'
43
43
  spec.add_development_dependency 'guard', '~> 2.18.0'
44
44
  spec.add_development_dependency 'guard-minitest', '~> 2.4.6'
45
45
  spec.add_development_dependency 'guard-rubocop', '~> 1.5.0'
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module HashDeepDiff
6
+ # This module includes behavior that is needed to use deltas instead of Hash inside this gem
7
+ module ActsAsHash
8
+ def self.included(base)
9
+ base.include(InstanceMethods)
10
+ base.extend(Forwardable)
11
+ base.def_delegators :@delta, :==, :each_with_object, :each_key, :[],
12
+ :to_a, :empty?, :keys
13
+ end
14
+
15
+ # Assumes that the class will include method delta that will return a representation of an
16
+ # instance of a class as a Hash
17
+ module InstanceMethods
18
+ def to_h
19
+ @delta
20
+ end
21
+
22
+ def to_hash
23
+ @delta
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,17 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'delta/left'
4
- require_relative 'delta/inner'
5
- require_relative 'delta/right'
3
+ require_relative 'delta'
6
4
 
7
5
  # :nodoc:
8
6
  module HashDeepDiff
9
- # :nodoc:
7
+ # An instrument to build and report the difference between two hash-like objects
10
8
  class Comparison
11
9
  attr_reader :left, :right, :path
12
10
 
13
- def diff(&block)
14
- left_delta + deep_delta(&block) + right_delta
11
+ def diff
12
+ deep_delta
15
13
  end
16
14
 
17
15
  def report
@@ -26,8 +24,8 @@ module HashDeepDiff
26
24
  @path = path.to_ary
27
25
  end
28
26
 
29
- def deep_delta(&block)
30
- delta(&block).flat_map do |diff|
27
+ def deep_delta
28
+ delta.flat_map do |diff|
31
29
  if diff.complex?
32
30
  self.class.new(diff.left, diff.right, diff.path).diff
33
31
  else
@@ -36,37 +34,19 @@ module HashDeepDiff
36
34
  end
37
35
  end
38
36
 
39
- def left_delta
40
- left_diff_keys.map { |key| Delta::Left.new(path: path + [key], value: left[key]) }
41
- end
42
-
43
- def right_delta
44
- right_diff_keys.map { |key| Delta::Right.new(path: path + [key], value: right[key]) }
45
- end
46
-
47
- def delta(&block)
48
- block ||= ->(val) { val }
49
-
37
+ def delta
50
38
  common_keys.each_with_object([]) do |key, memo|
51
- value_left = block.call(left[key])
52
- value_right = block.call(right[key])
39
+ value_left = left[key] || NO_VALUE
40
+ value_right = right[key] || NO_VALUE
53
41
 
54
42
  next if value_right.instance_of?(value_left.class) && (value_right == value_left)
55
43
 
56
- memo << Delta::Inner.new(path: path + [key], value: { left: value_left, right: value_right })
44
+ memo << Delta.new(path: path + [key], value: { left: value_left, right: value_right })
57
45
  end
58
46
  end
59
47
 
60
48
  def common_keys
61
- left.keys & right.keys
62
- end
63
-
64
- def right_diff_keys
65
- right.keys - left.keys
66
- end
67
-
68
- def left_diff_keys
69
- left.keys - right.keys
49
+ (left.keys + right.keys).uniq
70
50
  end
71
51
  end
72
52
  end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'acts_as_hash'
4
+
5
+ module HashDeepDiff
6
+ # Representation of the diff of two values
7
+ # examples:
8
+ # - diff of { a: a } and {} is { a: { left: a, right: HashDeepDiff::NO_VALUE } }
9
+ # - diff of { a: a } and { a: b } is { a: { left: a, right: b } }
10
+ # - diff of {} and { a: b } is { a: { left: HashDeepDiff::NO_VALUE, right: b } }
11
+ class Delta
12
+ include ActsAsHash
13
+
14
+ def to_str
15
+ [deletion, addition].compact.join("\n")
16
+ end
17
+
18
+ def complex?
19
+ left.respond_to?(:to_hash) && right.respond_to?(:to_hash)
20
+ end
21
+
22
+ # TOFIX poor naming
23
+ # overrides parameter in initializer
24
+ def path
25
+ @prefix + [@delta.keys.first]
26
+ end
27
+
28
+ def left
29
+ @value[:left]
30
+ end
31
+
32
+ def right
33
+ @value[:right]
34
+ end
35
+
36
+ def to_s
37
+ to_str
38
+ end
39
+
40
+ private
41
+
42
+ def initialize(path:, value:)
43
+ # TOFIX this may prohibit usage of hashes with Array keys
44
+ if path.respond_to?(:to_ary)
45
+ @delta = { path[-1] => value }
46
+ @value = value
47
+ @prefix = path[0..-2]
48
+ else
49
+ @delta = { path => value }
50
+ @value = value
51
+ @prefix = []
52
+ end
53
+ end
54
+
55
+ def deletion
56
+ return nil if left == NO_VALUE
57
+
58
+ if left.respond_to?(:to_hash)
59
+ left.keys.map { |key| "-left#{diff_prefix}[#{key}] = #{left[key]}" }.join("\n")
60
+ else
61
+ "-left#{diff_prefix} = #{left}"
62
+ end
63
+ end
64
+
65
+ def addition
66
+ return nil if right == NO_VALUE
67
+
68
+ if right.respond_to?(:to_hash)
69
+ right.keys.map { |key| "+left#{diff_prefix}[#{key}] = #{right[key]}" }.join("\n")
70
+ else
71
+ "+left#{diff_prefix} = #{right}"
72
+ end
73
+ end
74
+
75
+ # TOFIX poor naming
76
+ def diff_prefix
77
+ path.map { |key| "[#{key}]" }.join
78
+ end
79
+ end
80
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HashDeepDiff
4
- VERSION = '0.3.2'
4
+ VERSION = '0.4.1'
5
5
  end
@@ -4,5 +4,6 @@ require 'hash_deep_diff/version'
4
4
  require 'hash_deep_diff/comparison'
5
5
 
6
6
  module HashDeepDiff
7
+ NO_VALUE = Class.new(NilClass)
7
8
  class Error < StandardError; end
8
9
  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.2
4
+ version: 0.4.1
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-12 00:00:00.000000000 Z
11
+ date: 2022-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.17.2
19
+ version: 2.3.11
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.17.2
26
+ version: 2.3.11
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: guard
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -173,10 +173,13 @@ extra_rdoc_files: []
173
173
  files:
174
174
  - ".bundle/.keep"
175
175
  - ".bundle/config"
176
+ - ".github/workflows/ci.yml"
176
177
  - ".gitignore"
177
178
  - ".rubocop.yml"
179
+ - CHANGELOG.md
178
180
  - Gemfile
179
181
  - Guardfile
182
+ - MIT-LICENSE
180
183
  - README.md
181
184
  - Rakefile
182
185
  - bin/_guard-core
@@ -188,11 +191,9 @@ files:
188
191
  - bin/setup
189
192
  - hash_deep_diff.gemspec
190
193
  - lib/hash_deep_diff.rb
194
+ - lib/hash_deep_diff/acts_as_hash.rb
191
195
  - lib/hash_deep_diff/comparison.rb
192
- - lib/hash_deep_diff/delta/acts_as_delta.rb
193
- - lib/hash_deep_diff/delta/inner.rb
194
- - lib/hash_deep_diff/delta/left.rb
195
- - lib/hash_deep_diff/delta/right.rb
196
+ - lib/hash_deep_diff/delta.rb
196
197
  - lib/hash_deep_diff/version.rb
197
198
  homepage: https://github.com/bpohoriletz/hash_deep_diff
198
199
  licenses:
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- module HashDeepDiff
6
- module Delta
7
- # This module includes behavior that is needed to use deltas instead of Hash inside this gem
8
- module ActsAsDelta
9
- def self.included(base)
10
- base.prepend(Initialize)
11
- base.include(InstanceMethods)
12
- base.extend(Forwardable)
13
- base.def_delegators :@delta, :==, :each_with_object, :each_key, :[],
14
- :to_a, :empty?, :keys
15
- end
16
-
17
- # Assumes that the class will include method delta that will return a representation of an
18
- # instance of a class as a Hash
19
- module InstanceMethods
20
- # TOFIX poor naming
21
- def diff_prefix
22
- path.map { |key| "[#{key}]" }.join
23
- end
24
-
25
- # TOFIX poor naming
26
- def path
27
- @prefix + [@delta.keys.first]
28
- end
29
-
30
- def to_h
31
- @delta
32
- end
33
-
34
- def to_hash
35
- @delta
36
- end
37
-
38
- def to_s
39
- to_str
40
- end
41
-
42
- def to_str
43
- raise NoMethodError, "expected #{self.class} to implement #to_str"
44
- end
45
-
46
- def complex?
47
- raise NoMethodError, "expected #{self.class} to implement #complex?"
48
- end
49
- end
50
-
51
- # Override #initialize method
52
- module Initialize
53
- def initialize(path:, value:)
54
- # TOFIX this may prohibit usage of hashes with Array keys
55
- if path.respond_to?(:to_ary)
56
- @delta = { path[-1] => value }
57
- @prefix = path[0..-2]
58
- else
59
- @delta = { path => value }
60
- @prefix = []
61
- end
62
- end
63
- end
64
- end
65
- end
66
- end
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'acts_as_delta'
4
-
5
- module HashDeepDiff
6
- module Delta
7
- # Representation of the pure left diff
8
- # i.e element that are missing in the hash on the right of the comparison
9
- # for example left diff of { a: a } and {} is { a: a }
10
- class Inner
11
- include Delta::ActsAsDelta
12
-
13
- def to_str
14
- if @delta.values.first.respond_to?(:to_hash)
15
- if @delta.values.first.keys == %i[left right]
16
- if complex?
17
- HashDeepDiff::Comparison.new(left, right, path).report
18
- else
19
- lines = <<~Q
20
- -left#{diff_prefix} = #{left}
21
- +right#{diff_prefix} = #{right}
22
- Q
23
- lines.strip
24
- end
25
- else
26
- @delta.values.first.keys.map do |key|
27
- self.class.new(path: path + [key], value: @delta.values.first[key])
28
- end.join("\n").strip
29
- end
30
- else
31
- lines = <<~Q
32
- -left#{diff_prefix} = #{left}
33
- +right#{diff_prefix} = #{right}
34
- Q
35
- lines.strip
36
- end
37
- end
38
-
39
- def complex?
40
- left.respond_to?(:to_hash) && right.respond_to?(:to_hash)
41
- end
42
-
43
- def left
44
- @delta.values.first[:left]
45
- end
46
-
47
- def right
48
- @delta.values.first[:right]
49
- end
50
- end
51
- end
52
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'acts_as_delta'
4
-
5
- module HashDeepDiff
6
- module Delta
7
- # Representation of the pure left diff
8
- # i.e element that are missing in the hash on the right of the comparison
9
- # for example left diff of { a: a } and {} is { a: a }
10
- class Left
11
- include Delta::ActsAsDelta
12
-
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
21
- end
22
- end
23
- end
24
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'acts_as_delta'
4
-
5
- module HashDeepDiff
6
- module Delta
7
- # Representation of the pure right diff
8
- # i.e element that are missing in the hash on the right of the comparison
9
- # for example right diff of {} and { a: a } is { a: a }
10
- class Right
11
- include Delta::ActsAsDelta
12
-
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
21
- end
22
- end
23
- end
24
- end