cereals 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/README.md +4 -2
- data/lib/cereals/array_serializer.rb +2 -1
- data/lib/cereals/serializer.rb +7 -5
- data/lib/cereals/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b5127c019eda037621ddf6454acb7ec7928db65
|
4
|
+
data.tar.gz: c85fff1cdcee1ecc436d7e4deb39d7f5f4d6df1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80299197a86e2d5e69226f1abd8c863837c850a1f644d51632d90e539165069212aac90d971dd895d7487690e08832951a67e2236b7a41fbae8b1f4dab1f0b2f
|
7
|
+
data.tar.gz: 420ea93379b8db5317cc470957f503bf861c722922944f8b8f0391add74be8eb31f9a1cfa490d46553a05c80dd8fd651b6a2e16b2efda8821eb0a1c500256685
|
data/README.md
CHANGED
@@ -43,18 +43,20 @@ class UserSerializer < Cereals::Serializer
|
|
43
43
|
end
|
44
44
|
```
|
45
45
|
|
46
|
+
Rails renderers take care of calling `#to_json` when you render json.
|
47
|
+
|
46
48
|
A JSON API might look like this:
|
47
49
|
|
48
50
|
```ruby
|
49
51
|
class UsersController
|
50
52
|
def show
|
51
53
|
user = User.find(params[:id])
|
52
|
-
render json: user
|
54
|
+
render json: user
|
53
55
|
end
|
54
56
|
|
55
57
|
def index
|
56
58
|
users = User.all
|
57
|
-
render json: ArraySerializer.new(users, "users")
|
59
|
+
render json: ArraySerializer.new(users, "users")
|
58
60
|
end
|
59
61
|
end
|
60
62
|
```
|
data/lib/cereals/serializer.rb
CHANGED
@@ -9,14 +9,16 @@ module Cereals
|
|
9
9
|
# see lib/active_support/core_ext/object/json.rb
|
10
10
|
# for the definitions of #to_json and #as_json added by ActiveSupport
|
11
11
|
|
12
|
-
# super is ActiveSupport::ToJsonWithActiveSupportEncoder
|
13
|
-
def to_json(
|
14
|
-
|
12
|
+
# super is defined in ActiveSupport::ToJsonWithActiveSupportEncoder
|
13
|
+
def to_json(options = {})
|
14
|
+
options[:root] = true unless options.key?(:root)
|
15
|
+
super(options)
|
15
16
|
end
|
16
17
|
|
17
18
|
# override ActiveSupport's #as_json
|
18
|
-
def as_json(
|
19
|
-
|
19
|
+
def as_json(options = {})
|
20
|
+
options[:root] = true unless options.key?(:root)
|
21
|
+
if options[:root]
|
20
22
|
{ root_name => to_hash }
|
21
23
|
else
|
22
24
|
to_hash
|
data/lib/cereals/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cereals
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|