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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: afd9c0474de6cf65f0447a761bd4d082175d791b
4
- data.tar.gz: bb9020c942add2cb6205efa221b6e103b808623e
3
+ metadata.gz: 4b5127c019eda037621ddf6454acb7ec7928db65
4
+ data.tar.gz: c85fff1cdcee1ecc436d7e4deb39d7f5f4d6df1b
5
5
  SHA512:
6
- metadata.gz: afc716fe9f2d911fa0808a62fdb94f2fb2781a8c566036b65fdbf1af7485daed2e8c31ccc7dc9c2031c4ed42d658f14d3cdb3807a5a26889840b0de8eefa2aa0
7
- data.tar.gz: 757b55fabf660ff4a6831ca4c715be9b92c15b8bfd9b0320a445888a4817338cfef91743e68a2901e0566b738dbcdddbeb2e9bf380f578a06a571a4a1f20eaff
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.to_json
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").to_json
59
+ render json: ArraySerializer.new(users, "users")
58
60
  end
59
61
  end
60
62
  ```
@@ -9,7 +9,8 @@ module Cereals
9
9
  @root = root
10
10
  end
11
11
 
12
- def to_json
12
+ # accept arguments to work with rails rendering
13
+ def to_json(_ = nil)
13
14
  { @root => wrap_objects.as_json(root: false) }.to_json
14
15
  end
15
16
 
@@ -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(root: true)
14
- super(root: root)
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(root: true)
19
- if root
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cereals
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
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.1
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-26 00:00:00.000000000 Z
11
+ date: 2017-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport