hash_diff 0.5.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 +15 -0
- data/.gitignore +17 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/hash_diff.gemspec +24 -0
- data/lib/hash_diff.rb +2 -0
- data/lib/hash_diff/comparison.rb +73 -0
- data/lib/hash_diff/version.rb +3 -0
- data/spec/hash_diff/comparison_spec.rb +88 -0
- data/spec/spec_helper.rb +12 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OTE5YWFkZDliYzM3NGNkZTk4ZDk1Y2E3YzI0OGI3MGNmMmFmM2U4OA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MDdhMjNjMGIxMTI5ODdlYjQ0NDliY2I3NGViZGNkY2Q2ZDEyODQ2NQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ODFlOWRmOGM4Zjc1YWYzZjgxZWM4MTRmZTMzZjY2YmRjNjJkZjI5NmNmMjVl
|
10
|
+
ZGE4Y2ZjYWRmNjM4YWJkNDIzY2Y2NjUxMGYyOGQ0M2Y0MzAyN2RjYTU0YmNh
|
11
|
+
YjY4YzQ1MDczNGNlOWQ0NmZkZjJkZjJkMDQ1ODFiOTY1NTYxMDU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZWMyZGY4ZTQ4ZDFmZDNkOGM3Y2FmMzYzODBhNjhiY2E5MGY0ZDY5NTdlY2E0
|
14
|
+
NGUyZmExMWQzZDkxMWQwNjNiODBhNTFkZjhkNGQxZWI5ZTVmM2ZiZmJhYTMw
|
15
|
+
M2Q5OWRhMjkxN2M5NzA3YTlhMGNmMDZkYjU3YjA3YzIyZGQwYzI=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 acuppy
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# HashDiff
|
2
|
+
|
3
|
+
Deep comparison of Ruby Hash objects
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hash_diff'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hash_diff
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Easily find the differences between two Ruby hashes.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
left = {
|
25
|
+
foo: 'bar',
|
26
|
+
bar: 'foo',
|
27
|
+
nested: {
|
28
|
+
foo: 'bar',
|
29
|
+
bar: {
|
30
|
+
one: 'foo1'
|
31
|
+
}
|
32
|
+
},
|
33
|
+
num: 1
|
34
|
+
}
|
35
|
+
|
36
|
+
right = {
|
37
|
+
foo: 'bar2',
|
38
|
+
bar: 'foo2',
|
39
|
+
nested: {
|
40
|
+
foo: 'bar2',
|
41
|
+
bar: {
|
42
|
+
two: 'foo2'
|
43
|
+
}
|
44
|
+
},
|
45
|
+
word: 'monkey'
|
46
|
+
}
|
47
|
+
|
48
|
+
hash_diff = HashDiff::Comparison.new( left, right )
|
49
|
+
```
|
50
|
+
|
51
|
+
Comparison#diff returns the left and right side differences
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
hash_diff.diff # => { foo: ["bar", "bar2"], bar: ["foo", "foo2"], nested: { foo: ["bar", "bar2"], bar: { one: ["foo1", nil], two: [nil, "foo2"] } }, num: [1, nil], word: [nil, "monkey"] }
|
55
|
+
```
|
56
|
+
|
57
|
+
Comparison#left_diff returns only the left side differences
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
hash_diff.left_diff # => { foo: "bar2", bar: "foo2", nested: { foo: "bar2", bar: { one: nil, two: "foo2" } }, num: nil, word: "monkey" }
|
61
|
+
```
|
62
|
+
|
63
|
+
Comparison#right_diff returns only the right side differences
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
hash_diff.right_diff # => { foo: "bar", bar: "foo", nested: { foo: "bar", bar: { one: "foo1", two: nil } }, num: 1, word: nil }
|
67
|
+
```
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/hash_diff.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_diff/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_diff"
|
8
|
+
spec.version = HashDiff::VERSION
|
9
|
+
spec.authors = ["Coding Zeal", "Adam Cuppy"]
|
10
|
+
spec.email = ["info@codingzeal.com"]
|
11
|
+
spec.description = %q{Diff tool for deep Ruby hash comparison}
|
12
|
+
spec.summary = %q{Deep Hash comparison}
|
13
|
+
spec.homepage = "https://github.com/CodingZeal/hash_diff"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.6.0"
|
24
|
+
end
|
data/lib/hash_diff.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module HashDiff
|
4
|
+
class Comparison < Struct.new(:left, :right)
|
5
|
+
|
6
|
+
def diff
|
7
|
+
@side ||= :both
|
8
|
+
@diff ||= differences(left, right)
|
9
|
+
end
|
10
|
+
|
11
|
+
def left_diff
|
12
|
+
@side = :left
|
13
|
+
@left_diff ||= differences(left, right)
|
14
|
+
end
|
15
|
+
|
16
|
+
def right_diff
|
17
|
+
@side = :right
|
18
|
+
@right_diff ||= differences(left, right)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def clone(left, right)
|
24
|
+
self.dup.tap do |inst|
|
25
|
+
inst.left = left
|
26
|
+
inst.right = right
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def differences(left, right)
|
31
|
+
combined_attribute_keys(left, right).reduce({}, &reduction_strategy)
|
32
|
+
end
|
33
|
+
|
34
|
+
def reduction_strategy(opts={})
|
35
|
+
lambda do |diff, key|
|
36
|
+
diff[key] = report(key) if not equal?(key)
|
37
|
+
diff
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def combined_attribute_keys(left, right)
|
42
|
+
(left.keys + right.keys).uniq
|
43
|
+
end
|
44
|
+
|
45
|
+
def equal?(key)
|
46
|
+
left.key?(key) && right.key?(key) && left[key] == right[key]
|
47
|
+
end
|
48
|
+
|
49
|
+
def hash?(value)
|
50
|
+
value.is_a? Hash
|
51
|
+
end
|
52
|
+
|
53
|
+
def comparable_key?(key)
|
54
|
+
hash?(left[key]) and hash?(right[key])
|
55
|
+
end
|
56
|
+
|
57
|
+
def report(key)
|
58
|
+
if comparable_key?(key)
|
59
|
+
clone(left[key], right[key]).diff
|
60
|
+
else
|
61
|
+
report_by_side(key)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def report_by_side(key)
|
66
|
+
case @side
|
67
|
+
when :left then right[key]
|
68
|
+
when :right then left[key]
|
69
|
+
when :both then [left[key], right[key]]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HashDiff::Comparison do
|
4
|
+
|
5
|
+
let(:app_v1_properties) { { foo: 'bar', bar: 'foo', nested: { foo: 'bar', bar: { one: 'foo1' } }, num: 1 } }
|
6
|
+
let(:app_v2_properties) { { foo: 'bar2', bar: 'foo2', nested: { foo: 'bar2', bar: { two: 'foo2' } }, word: 'monkey' } }
|
7
|
+
|
8
|
+
subject { HashDiff::Comparison.new(app_v1_properties, app_v2_properties) }
|
9
|
+
|
10
|
+
describe "#diff" do
|
11
|
+
|
12
|
+
context "when different" do
|
13
|
+
let(:diff) {
|
14
|
+
{
|
15
|
+
foo: ["bar", "bar2"],
|
16
|
+
bar: ["foo", "foo2"],
|
17
|
+
nested: {
|
18
|
+
foo: ["bar", "bar2"],
|
19
|
+
bar: {
|
20
|
+
one: ["foo1", nil],
|
21
|
+
two: [nil, "foo2"]
|
22
|
+
}
|
23
|
+
},
|
24
|
+
num: [1, nil],
|
25
|
+
word: [nil, "monkey"]
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
its(:diff) { should == diff }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when similar" do
|
33
|
+
let(:app_v1_properties) { { foo: 'bar', bar: 'foo' } }
|
34
|
+
|
35
|
+
context "in the same order" do
|
36
|
+
let(:app_v2_properties) { app_v1_properties }
|
37
|
+
|
38
|
+
its(:diff) { should be_empty }
|
39
|
+
end
|
40
|
+
|
41
|
+
context "in a different order" do
|
42
|
+
let(:app_v2_properties) { { bar: 'foo', foo: 'bar' } }
|
43
|
+
|
44
|
+
its(:diff) { should be_empty }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#left_diff" do
|
50
|
+
let(:diff) {
|
51
|
+
{
|
52
|
+
foo: "bar2",
|
53
|
+
bar: "foo2",
|
54
|
+
nested: {
|
55
|
+
foo: "bar2",
|
56
|
+
bar: {
|
57
|
+
one: nil,
|
58
|
+
two: "foo2"
|
59
|
+
}
|
60
|
+
},
|
61
|
+
num: nil,
|
62
|
+
word: "monkey"
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
its(:left_diff) { should == diff }
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#right_diff" do
|
70
|
+
let(:diff) {
|
71
|
+
{
|
72
|
+
foo: "bar",
|
73
|
+
bar: "foo",
|
74
|
+
nested: {
|
75
|
+
foo: "bar",
|
76
|
+
bar: {
|
77
|
+
one: "foo1",
|
78
|
+
two: nil
|
79
|
+
}
|
80
|
+
},
|
81
|
+
num: 1,
|
82
|
+
word: nil
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
its(:right_diff) { should == diff }
|
87
|
+
end
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
|
4
|
+
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
|
5
|
+
$LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
|
6
|
+
Dir[File.join(PROJECT_ROOT, 'spec/support/**/*.rb')].each { |file| require(file) }
|
7
|
+
|
8
|
+
require 'hash_diff'
|
9
|
+
|
10
|
+
RSpec.configure do |c|
|
11
|
+
c.filter_run_excluding :skip_on_windows => !(RbConfig::CONFIG['host_os'] =~ /mingw32/).nil?
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_diff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Coding Zeal
|
8
|
+
- Adam Cuppy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-12-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.6.0
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.6.0
|
56
|
+
description: Diff tool for deep Ruby hash comparison
|
57
|
+
email:
|
58
|
+
- info@codingzeal.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- hash_diff.gemspec
|
69
|
+
- lib/hash_diff.rb
|
70
|
+
- lib/hash_diff/comparison.rb
|
71
|
+
- lib/hash_diff/version.rb
|
72
|
+
- spec/hash_diff/comparison_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/CodingZeal/hash_diff
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.1.11
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Deep Hash comparison
|
98
|
+
test_files:
|
99
|
+
- spec/hash_diff/comparison_spec.rb
|
100
|
+
- spec/spec_helper.rb
|