json-merge_patch 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/lib/json/merge_patch.rb +11 -2
- data/lib/json/merge_patch/version.rb +1 -1
- data/test/merge_patch_test.rb +56 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ce09ddf6d3610381e68fbb86d8ccf6874573007
|
4
|
+
data.tar.gz: 15046d860a25fc45329f784fd3a9a639534be7d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3afcecc326503602ae67a18db19bc3d34596543ffe68e57a0284eb22c4b7bb4bbc5d7e2d70b03d8000e7077e0bf5bdd11ef14fd55b266939b5da4f6ee7255641
|
7
|
+
data.tar.gz: fadeda3faadfbe333ad0e9da52a0301d17b008936d3a41f46d6345ad1bca05487988947ae7d1208577438bd5e867904b61b3ad6a27547db157cd60b95a408c4d
|
data/.travis.yml
CHANGED
data/lib/json/merge_patch.rb
CHANGED
@@ -18,10 +18,19 @@ module JSON
|
|
18
18
|
|
19
19
|
if merge_patch.is_a? Hash
|
20
20
|
document.merge!(merge_patch)
|
21
|
-
document.delete_if
|
21
|
+
document.delete_if(&method(:recursive_delete_if))
|
22
22
|
return JSON.dump(document)
|
23
23
|
end
|
24
|
-
rescue
|
24
|
+
rescue
|
25
25
|
raise MergeError
|
26
26
|
end
|
27
|
+
|
28
|
+
def self.recursive_delete_if(key, value)
|
29
|
+
if value.kind_of?(Hash)
|
30
|
+
value.delete_if(&method(:recursive_delete_if))
|
31
|
+
nil
|
32
|
+
else
|
33
|
+
value.nil?
|
34
|
+
end
|
35
|
+
end
|
27
36
|
end
|
data/test/merge_patch_test.rb
CHANGED
@@ -87,6 +87,17 @@ describe "section 2" do
|
|
87
87
|
provided data whose value is explicitly null is to be treated
|
88
88
|
as if the member were undefined.
|
89
89
|
=end
|
90
|
+
describe "object" do
|
91
|
+
it "replaces members" do
|
92
|
+
document = %q'{"foo":"bar"}'
|
93
|
+
|
94
|
+
merge_patch = %q'{"foo":"baz"}'
|
95
|
+
|
96
|
+
expected = %q'{"foo":"baz"}'
|
97
|
+
|
98
|
+
assert_equal expected, JSON.merge(document, merge_patch)
|
99
|
+
end
|
100
|
+
end
|
90
101
|
end
|
91
102
|
|
92
103
|
describe "part 4" do
|
@@ -98,6 +109,29 @@ describe "section 2" do
|
|
98
109
|
in the provided data whose value is explicitly null is to be
|
99
110
|
treated as if the member was undefined.
|
100
111
|
=end
|
112
|
+
describe "patch has a primitive" do
|
113
|
+
it "replaces members" do
|
114
|
+
document = %q'{"foo":{"bar":"baz"}}'
|
115
|
+
|
116
|
+
merge_patch = %q'{"foo":2}'
|
117
|
+
|
118
|
+
expected = %q'{"foo":2}'
|
119
|
+
|
120
|
+
assert_equal expected, JSON.merge(document, merge_patch)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "patch has an array" do
|
125
|
+
it "replaces members" do
|
126
|
+
document = %q'{"foo":{"bar":"baz"}}'
|
127
|
+
|
128
|
+
merge_patch = %q'{"foo":["bar"]}'
|
129
|
+
|
130
|
+
expected = %q'{"foo":["bar"]}'
|
131
|
+
|
132
|
+
assert_equal expected, JSON.merge(document, merge_patch)
|
133
|
+
end
|
134
|
+
end
|
101
135
|
end
|
102
136
|
|
103
137
|
describe "part 5" do
|
@@ -107,6 +141,27 @@ describe "section 2" do
|
|
107
141
|
also a JSON object, then recursively apply Rule #2 to each
|
108
142
|
object.
|
109
143
|
=end
|
144
|
+
describe "nested objects" do
|
145
|
+
it "recursively " do
|
146
|
+
document = %q'{"foo":{"bar":"baz"}}'
|
147
|
+
|
148
|
+
merge_patch = %q'{"foo":{"bar":{"baz":"qux"}}}'
|
149
|
+
|
150
|
+
expected = %q'{"foo":{"bar":{"baz":"qux"}}}'
|
151
|
+
|
152
|
+
assert_equal expected, JSON.merge(document, merge_patch)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
it "recursive removal" do
|
157
|
+
document = %q'{"foo":{"bar":"baz"}}'
|
158
|
+
|
159
|
+
merge_patch = %q'{"foo":{"bar":null}}'
|
160
|
+
|
161
|
+
expected = %q'{"foo":{}}'
|
162
|
+
|
163
|
+
assert_equal expected, JSON.merge(document, merge_patch)
|
164
|
+
end
|
110
165
|
end
|
111
166
|
|
112
167
|
describe "part 6" do
|
@@ -152,7 +207,7 @@ describe "README example" do
|
|
152
207
|
}
|
153
208
|
JSON
|
154
209
|
|
155
|
-
expected = <<-JSON.strip_heredoc.
|
210
|
+
expected = <<-JSON.strip_heredoc.gsub(/\s/, "")
|
156
211
|
{
|
157
212
|
"title": "Goodbye!",
|
158
213
|
"author" : {
|