jsondiff 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +8 -0
- data/lib/jsondiff.rb +13 -4
- data/lib/jsondiff/array_diff.rb +4 -10
- data/lib/jsondiff/hash_diff.rb +2 -8
- data/lib/jsondiff/helpers.rb +2 -2
- data/lib/jsondiff/version.rb +1 -1
- data/spec/diff_spec.rb +6 -0
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -31,6 +31,14 @@ JsonDiff.generate({foo: :bar}, {foo: :plop})
|
|
31
31
|
|
32
32
|
rspec
|
33
33
|
|
34
|
+
## Changelog
|
35
|
+
|
36
|
+
- 0.0.3:
|
37
|
+
- Allow to compare root Hash and Array.
|
38
|
+
- Performance improvements
|
39
|
+
- 0.0.2: Fix require 'jsondiff'
|
40
|
+
- 0.0.1: Initial release
|
41
|
+
|
34
42
|
## License
|
35
43
|
|
36
44
|
Copyright (c) 2013 François de Metz
|
data/lib/jsondiff.rb
CHANGED
@@ -3,13 +3,22 @@ require 'jsondiff/hash_diff'
|
|
3
3
|
require 'jsondiff/array_diff'
|
4
4
|
|
5
5
|
module JsonDiff
|
6
|
+
include Helpers
|
7
|
+
|
6
8
|
# Generate a patch from two ruby hash
|
7
9
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
+
# arg1 - the first argument
|
11
|
+
# arg2 - the second argument
|
10
12
|
#
|
11
13
|
# Returns an array of operations
|
12
|
-
def self.generate(
|
13
|
-
|
14
|
+
def self.generate(arg1, arg2, result=[], prefix="")
|
15
|
+
if Hash === arg1 && Hash === arg2
|
16
|
+
HashDiff.generate(result, prefix, arg1, arg2)
|
17
|
+
elsif Array === arg1 && Array === arg2
|
18
|
+
ArrayDiff.generate(result, prefix, arg1, arg2)
|
19
|
+
else
|
20
|
+
result << replace_op(prefix, arg2)
|
21
|
+
end
|
22
|
+
result
|
14
23
|
end
|
15
24
|
end
|
data/lib/jsondiff/array_diff.rb
CHANGED
@@ -5,28 +5,22 @@ module JsonDiff
|
|
5
5
|
def self.generate(result, prefix, array1, array2)
|
6
6
|
if array1.size < array2.size
|
7
7
|
array2.each_with_index do |value, index|
|
8
|
-
if array1
|
8
|
+
if array1[index] != value
|
9
9
|
result << add_op(prefix, index, value)
|
10
10
|
array1.insert(index, value)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
elsif array1.size > array2.size
|
14
14
|
array1.each_with_index do |value, index|
|
15
|
-
if array2
|
15
|
+
if array2[index] != value
|
16
16
|
result << remove_op(prefix, index)
|
17
17
|
array1.delete_at(index)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
21
21
|
array2.each_with_index do |value, index|
|
22
|
-
if array1
|
23
|
-
|
24
|
-
HashDiff.generate(result, "#{prefix}/#{index}", array1.at(index), value)
|
25
|
-
elsif value.kind_of?(Array) && array1.at(index).kind_of?(Array)
|
26
|
-
ArrayDiff.generate(result, "#{prefix}/#{index}", array1.at(index), value)
|
27
|
-
else
|
28
|
-
result << replace_op(prefix, index, value)
|
29
|
-
end
|
22
|
+
if array1[index] != value
|
23
|
+
JsonDiff.generate(array1.at(index), value, result, "#{prefix}/#{index}")
|
30
24
|
end
|
31
25
|
end
|
32
26
|
end
|
data/lib/jsondiff/hash_diff.rb
CHANGED
@@ -7,15 +7,9 @@ module JsonDiff
|
|
7
7
|
if !hash1.has_key? key
|
8
8
|
result << add_op(prefix, key, value)
|
9
9
|
else
|
10
|
-
value2 = hash1
|
10
|
+
value2 = hash1[key]
|
11
11
|
if value != value2
|
12
|
-
|
13
|
-
ArrayDiff.generate(result, "#{prefix}/#{key}", value2, value)
|
14
|
-
elsif value.kind_of?(Hash) && value2.kind_of?(Hash)
|
15
|
-
HashDiff.generate(result, "#{prefix}/#{key}", value2, value)
|
16
|
-
else
|
17
|
-
result << replace_op(prefix, key, value)
|
18
|
-
end
|
12
|
+
JsonDiff.generate(value2, value, result, "#{prefix}/#{key}")
|
19
13
|
end
|
20
14
|
end
|
21
15
|
end
|
data/lib/jsondiff/helpers.rb
CHANGED
@@ -13,8 +13,8 @@ module JsonDiff
|
|
13
13
|
{op: :remove, path: "#{prefix}/#{key}"}
|
14
14
|
end
|
15
15
|
|
16
|
-
def replace_op(prefix,
|
17
|
-
{op: :replace, path:
|
16
|
+
def replace_op(prefix, value)
|
17
|
+
{op: :replace, path: prefix, value: value}
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/jsondiff/version.rb
CHANGED
data/spec/diff_spec.rb
CHANGED
@@ -82,6 +82,12 @@ describe JsonDiff do
|
|
82
82
|
{foo: [:bar]})
|
83
83
|
.should == [{ op: :replace, path: "/foo", value: [:bar] }]
|
84
84
|
end
|
85
|
+
|
86
|
+
it "replace everything" do
|
87
|
+
subject.generate({foo: :bar},
|
88
|
+
[:foo])
|
89
|
+
.should == [{ op: :replace, path: "", value: [:foo] }]
|
90
|
+
end
|
85
91
|
end
|
86
92
|
end
|
87
93
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsondiff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &10290680 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 2.13.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *10290680
|
25
25
|
description: Generate a JSON Patch from 2 ruby hash
|
26
26
|
email:
|
27
27
|
- francois@2metz.fr
|