json-merge_patch 1.0.0 → 1.1.0
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 +4 -4
- data/README.md +26 -1
- data/lib/json/merge_patch.rb +7 -36
- data/lib/json/merge_patch/railtie.rb +16 -0
- data/lib/json/merge_patch/version.rb +1 -1
- data/test/merge_patch_test.rb +25 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 72a0a7781db050c81b3df548812dd1f48c13f750
|
|
4
|
+
data.tar.gz: 7f4e7f89f6a31b91c49edf2f4d8165088e1990fe
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9137b208eaaa2806fbe20adbc5e13596550415415477baa68b2bf54d280aad82bc03d59ae4a0ffdeafe5abe97617da44d78fb5071a89256540cd357df2a42e8c
|
|
7
|
+
data.tar.gz: 50f346c2c6622fae988302ac1937a42b78886b69c6ca485a0b6b5c00182255abac556b65aab1ec05c6d9d18935fd2e93bbef2ec3424712eb8febc58226f1d116
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# JSON::MergePatch
|
|
2
2
|
|
|
3
3
|
[](https://travis-ci.org/steveklabnik/json-merge_patch) [](https://codeclimate.com/github/steveklabnik/json-merge_patch) [](https://coveralls.io/r/steveklabnik/json-merge_patch)
|
|
4
4
|
|
|
@@ -6,6 +6,9 @@ This gem augments Ruby's built-in JSON library to support merging JSON blobs
|
|
|
6
6
|
in accordance with the [draft-snell-merge-patch
|
|
7
7
|
draft](http://tools.ietf.org/html/draft-snell-merge-patch-08).
|
|
8
8
|
|
|
9
|
+
As far as I know, it is a complete implementation of the draft. If you find
|
|
10
|
+
something that's not compliant, please let me know.
|
|
11
|
+
|
|
9
12
|
## Installation
|
|
10
13
|
|
|
11
14
|
Add this line to your application's Gemfile:
|
|
@@ -80,6 +83,28 @@ JSON::MergePatch.new({}, {"foo" => "bar"}).call
|
|
|
80
83
|
Also check out [http://json-merge-patch.herokuapp.com/](http://json-merge-patch.herokuapp.com/),
|
|
81
84
|
which is a Rails app that serves up `json-merge-patch` responses.
|
|
82
85
|
|
|
86
|
+
### Use in Rails
|
|
87
|
+
|
|
88
|
+
JSON::MergePatch provides a Railtie that registers the proper MIME type with
|
|
89
|
+
Rails. To use it, do something like this:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
def update
|
|
93
|
+
safe_params = params.require(:merge).permit(:original, :patch)
|
|
94
|
+
|
|
95
|
+
@result = JSON::MergePatch.new(
|
|
96
|
+
safe_params[:original],
|
|
97
|
+
safe_params[:patch]
|
|
98
|
+
).call
|
|
99
|
+
|
|
100
|
+
respond_to do |format|
|
|
101
|
+
format.json_merge_patch do
|
|
102
|
+
render :json => @result
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
```
|
|
107
|
+
|
|
83
108
|
## Contributing
|
|
84
109
|
|
|
85
110
|
1. Fork it
|
data/lib/json/merge_patch.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
require 'json'
|
|
2
2
|
require "json/merge_patch/version"
|
|
3
3
|
|
|
4
|
+
require 'json/merge_patch/railtie' if defined?(Rails)
|
|
5
|
+
|
|
4
6
|
module JSON
|
|
5
7
|
# This represents an error that occurs during merging.
|
|
6
8
|
MergeError = Class.new(StandardError)
|
|
@@ -50,24 +52,14 @@ module JSON
|
|
|
50
52
|
@orig[m] = @patch[m]
|
|
51
53
|
else
|
|
52
54
|
if @orig[m].kind_of?(Array)
|
|
53
|
-
|
|
54
|
-
if result.nil?
|
|
55
|
-
@orig.delete(m)
|
|
56
|
-
else
|
|
57
|
-
@orig[m] = result
|
|
58
|
-
end
|
|
55
|
+
@orig[m] = purge_nils(@patch[m])
|
|
59
56
|
else
|
|
60
57
|
@orig[m] = self.class.new(@orig[m], @patch[m]).call
|
|
61
58
|
end
|
|
62
59
|
end
|
|
63
60
|
end
|
|
64
61
|
elsif !(@patch[m].nil?)
|
|
65
|
-
|
|
66
|
-
if result.nil?
|
|
67
|
-
@orig.delete(m)
|
|
68
|
-
else
|
|
69
|
-
@orig[m] = result
|
|
70
|
-
end
|
|
62
|
+
@orig[m] = purge_nils(@patch[m])
|
|
71
63
|
end
|
|
72
64
|
end
|
|
73
65
|
end
|
|
@@ -77,18 +69,8 @@ module JSON
|
|
|
77
69
|
private
|
|
78
70
|
|
|
79
71
|
def is_primitive?(val)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
true
|
|
83
|
-
when Fixnum
|
|
84
|
-
true
|
|
85
|
-
when TrueClass
|
|
86
|
-
true
|
|
87
|
-
when FalseClass
|
|
88
|
-
true
|
|
89
|
-
else
|
|
90
|
-
false
|
|
91
|
-
end
|
|
72
|
+
[String, Fixnum,
|
|
73
|
+
TrueClass, FalseClass].include?(val.class)
|
|
92
74
|
end
|
|
93
75
|
|
|
94
76
|
def purge_nils(obj)
|
|
@@ -96,18 +78,7 @@ module JSON
|
|
|
96
78
|
return obj.compact if obj.kind_of?(Array)
|
|
97
79
|
return obj if is_primitive?(obj)
|
|
98
80
|
|
|
99
|
-
obj.
|
|
100
|
-
if obj[m].nil?
|
|
101
|
-
if obj.kind_of?(Array)
|
|
102
|
-
obj.delete_at(m)
|
|
103
|
-
else
|
|
104
|
-
obj.delete(m)
|
|
105
|
-
end
|
|
106
|
-
elsif obj[m].kind_of?(Hash)
|
|
107
|
-
purge_nils(obj[m])
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
obj
|
|
81
|
+
obj.delete_if {|k, v| v.nil? }
|
|
111
82
|
end
|
|
112
83
|
end
|
|
113
84
|
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'rails'
|
|
2
|
+
|
|
3
|
+
module JSON
|
|
4
|
+
class MergePatch
|
|
5
|
+
# This class registers our gem with Rails.
|
|
6
|
+
class Railtie < ::Rails::Railtie
|
|
7
|
+
|
|
8
|
+
# When the application loads, this will cause Rails to know
|
|
9
|
+
# how to serve up the proper type.
|
|
10
|
+
initializer 'json-merge_patch' do
|
|
11
|
+
Mime::Type.register 'application/json-merge-patch', :json_merge_patch
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
data/test/merge_patch_test.rb
CHANGED
|
@@ -182,6 +182,31 @@ describe "section 2" do
|
|
|
182
182
|
end
|
|
183
183
|
end
|
|
184
184
|
|
|
185
|
+
describe "missing coverage" do
|
|
186
|
+
it "deals with a nil patch" do
|
|
187
|
+
document = %q'{"foo":"bar"}'
|
|
188
|
+
expected = %q'{"foo":"bar"}'
|
|
189
|
+
|
|
190
|
+
assert_equal expected, JSON::MergePatch.new(document, nil).call
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
it "handles primitive true" do
|
|
194
|
+
document = %q'{"foo":"bar"}'
|
|
195
|
+
patch = %q'{"foo":true}'
|
|
196
|
+
expected = %q'{"foo":true}'
|
|
197
|
+
|
|
198
|
+
assert_equal expected, JSON.merge(document, patch)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
it "handles primitive false" do
|
|
202
|
+
document = %q'{"foo":"bar"}'
|
|
203
|
+
patch = %q'{"foo":false}'
|
|
204
|
+
expected = %q'{"foo":false}'
|
|
205
|
+
|
|
206
|
+
assert_equal expected, JSON.merge(document, patch)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
185
210
|
describe "README example" do
|
|
186
211
|
it "works properly" do
|
|
187
212
|
document = <<-JSON.strip_heredoc.chomp
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json-merge_patch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Steve Klabnik
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2013-05-
|
|
11
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -67,6 +67,7 @@ files:
|
|
|
67
67
|
- Rakefile
|
|
68
68
|
- json-merge_patch.gemspec
|
|
69
69
|
- lib/json/merge_patch.rb
|
|
70
|
+
- lib/json/merge_patch/railtie.rb
|
|
70
71
|
- lib/json/merge_patch/version.rb
|
|
71
72
|
- test/merge_patch_test.rb
|
|
72
73
|
- test/test_helper.rb
|