cereals 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8cbb295200da71a6eb80a440f3f7cfe77ca4545
4
+ data.tar.gz: 48dcbbd6c495b0da2a407134a54d1a242a264610
5
+ SHA512:
6
+ metadata.gz: d1809ee98e931cf46ed8078d6b84f4a416085e64796deab40e91ac2fd3687db6188249f5971a35bbbb1a401368107945b652d82c1d10be32d85d9bd4936c0cab
7
+ data.tar.gz: a8d8e67a763f0a74e5ec544da1ab1c5e5f7b91719decd26ea07029ee03f48a3cbf6aa36503003b24f79636e9c7bb2bc441b8752f8a74309e75a8c8e0b2604dd0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Tee Parham, Michael Guida
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # Cereals
2
+
3
+ Cereals makes it easy to serialize ruby objects to JSON.
4
+ Cereals provides serializer classes for single objects and collections.
5
+
6
+ Cereals depends on ActiveSupport's `#to_json` and `#to_hash` implementations.
7
+
8
+ ## Install
9
+
10
+ Add the gem to your Gemfile:
11
+
12
+ ```ruby
13
+ gem "cereals"
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Include the `Cereals::Base` module in the classes you want to serialize.
19
+ If you're using Rails 5+, add it to `ApplicationRecord` like so:
20
+
21
+ ```ruby
22
+ class ApplicationRecord
23
+ include Cereals::Base
24
+ end
25
+ ```
26
+
27
+ Create serializer classes with names corresponding to your models.
28
+ Implement `#to_hash` on each serializer.
29
+
30
+ ```ruby
31
+ class UserSerializer < Cereals::Serializer
32
+ def to_hash
33
+ {
34
+ first_name: @object.first_name,
35
+ last_name: @object.last_name
36
+ }
37
+ end
38
+ ```
39
+
40
+ A JSON API might look like this:
41
+
42
+ ```ruby
43
+ class UsersController
44
+ def show
45
+ user = User.find(params[:id])
46
+ render json: user.to_json
47
+ end
48
+
49
+ def index
50
+ users = User.all
51
+ render json: ArraySerializer.new(users, "users").to_json
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## JSON Format
57
+
58
+ Single objects have a root node.
59
+
60
+ `UserSerializer.new(user).to_json`
61
+
62
+ ```
63
+ {
64
+ "user": {
65
+ "first_name":"Bart",
66
+ "last_name":"Simpson"
67
+ }
68
+ }
69
+ ```
70
+
71
+ Arrays have a root node, and items within the array do not.
72
+
73
+ `ArraySerializer.new([user1, user2]).to_json`
74
+
75
+ ```
76
+ {
77
+ "users": [
78
+ {
79
+ "first_name":"Bart",
80
+ "last_name":"Simpson"
81
+ },
82
+ {
83
+ "first_name":"Lisa",
84
+ "last_name":"Simpson"
85
+ }
86
+ ]
87
+ }
88
+ ```
89
+
90
+ ## Configuration
91
+
92
+ Specify a different serializer class for a model:
93
+
94
+ ```ruby
95
+ class User
96
+ def serializer_class
97
+ PersonSerializer
98
+ end
99
+ end
100
+ ```
101
+
102
+ Specify a different root name for a serializer:
103
+
104
+ ```ruby
105
+ class UserSerializer
106
+ private
107
+
108
+ def root_name
109
+ "person"
110
+ end
111
+ end
112
+ ```
113
+
114
+
115
+ ## Development
116
+
117
+ After checking out the repo, run `bin/setup` to install dependencies.
118
+ Run `rake test` to run the tests. You can also run `bin/console` for an
119
+ interactive REPL.
120
+
121
+ ## Contributing
122
+
123
+ Bug reports and pull requests are welcome on GitHub at https://github.com/teeparham/cereals.
124
+
125
+ This project is intended to be a safe, welcoming space for collaboration,
126
+ and contributors are expected to adhere to the
127
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
128
+
129
+ ## License
130
+
131
+ The gem is available as open source under the terms of the
132
+ [MIT License](http://opensource.org/licenses/MIT).
133
+
134
+ ## Code of Conduct
135
+
136
+ Everyone interacting in the Cereals project’s codebases, issue trackers, chat rooms and
137
+ mailing lists is expected to follow the
138
+ [code of conduct](https://github.com/teeparham/cereals/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cereals
4
+ class ArraySerializer
5
+ # objects must respond to :serializer
6
+ # root is a string, the name of the root JSON key
7
+ def initialize(objects, root)
8
+ @objects = objects
9
+ @root = root
10
+ end
11
+
12
+ def to_json
13
+ { @root => wrap_objects.as_json(root: false) }.to_json
14
+ end
15
+
16
+ private
17
+
18
+ def wrap_objects
19
+ @objects.map(&:serializer)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cereals
4
+ module Base
5
+ def self.included(base)
6
+ base.delegate :to_json, to: :serializer
7
+ end
8
+
9
+ def serializer_class
10
+ Object.const_get("#{self.class.name}Serializer")
11
+ end
12
+
13
+ def serializer
14
+ serializer_class.new(self)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cereals
4
+ class Serializer
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ # see lib/active_support/core_ext/object/json.rb
10
+ # for the definitions of #to_json and #as_json added by ActiveSupport
11
+
12
+ # super is ActiveSupport::ToJsonWithActiveSupportEncoder
13
+ def to_json(root: true)
14
+ super(root: root)
15
+ end
16
+
17
+ # override ActiveSupport's #as_json
18
+ def as_json(root: true)
19
+ if root
20
+ { root_name => to_hash }
21
+ else
22
+ to_hash
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def root_name
29
+ @object.class.name.underscore
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cereals
4
+ VERSION = "0.0.0"
5
+ end
data/lib/cereals.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cereals/version"
4
+ require "cereals/array_serializer"
5
+ require "cereals/base"
6
+ require "cereals/serializer"
7
+ require "active_support/all"
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cereals
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tee Parham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: JSON serializer classes
70
+ email:
71
+ - tee@neighborland.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE.txt
77
+ - README.md
78
+ - lib/cereals.rb
79
+ - lib/cereals/array_serializer.rb
80
+ - lib/cereals/base.rb
81
+ - lib/cereals/serializer.rb
82
+ - lib/cereals/version.rb
83
+ homepage: https://github.com/teeparham/cereals
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.12
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: JSON serializer classes
107
+ test_files: []
108
+ has_rdoc: