serializr 0.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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +176 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/serializer/USAGE +19 -0
- data/lib/generators/serializer/serializer_generator.rb +5 -0
- data/lib/generators/serializr/USAGE +19 -0
- data/lib/generators/serializr/serializr_generator.rb +47 -0
- data/lib/generators/serializr/templates/application_serializr.rb +4 -0
- data/lib/generators/serializr/templates/serializr.rb +7 -0
- data/lib/serializer.rb +1 -0
- data/lib/serializr/integration.rb +44 -0
- data/lib/serializr/railtie.rb +14 -0
- data/lib/serializr/version.rb +3 -0
- data/lib/serializr.rb +65 -0
- data/serializr.gemspec +28 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 41ea89e74e081490d15d51602f036231410bd996
|
4
|
+
data.tar.gz: 1b6e49a40938681ed7526e83ec76ba3d1597bb7f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 59e11f40d0aed5c14189b0f3a9db99c1878207b7c77ee99669584bdf5c13ab98405d34b078828447c56aa36183fd44d6e42ea7a7e37323405343136dbae61f78
|
7
|
+
data.tar.gz: 7adc1cc8275f63e20f1e135345faad18aedb11ccfd92b436379be6cd5ac2ea45082b6d74d56bdbbaf2a4ef1d4663e779e85dc9678ba8ca1d867d38681597857d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at gsamokovarov@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Genadi Samokovarov
|
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,176 @@
|
|
1
|
+
# Serializr
|
2
|
+
|
3
|
+
Serializr is a library for creating canonical JSON representation of objects
|
4
|
+
for your RESTful APIs.
|
5
|
+
|
6
|
+
Think of the serializers as the view layer of your application. They are not
|
7
|
+
the only JSON representation of an object, but they are _the_ representation
|
8
|
+
you wanna show to the API.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'serializr'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Using the serializr library is pretty simple. To generate your first
|
21
|
+
serializer, execute:
|
22
|
+
|
23
|
+
$ rails generate serializr User id name email
|
24
|
+
|
25
|
+
The output should hint that the generator created two files:
|
26
|
+
|
27
|
+
create app/serializers/application_serializer.rb
|
28
|
+
create app/serializers/user_serializer.rb
|
29
|
+
|
30
|
+
The `app/serializers/user_serializer.rb` file should contain:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class UserSerializer < ApplicationSerializer
|
34
|
+
attributes :id, :name, :email
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
This says: expect the object to be serialized to respond to `#id`, `#name` and
|
39
|
+
`#email` and show the resulting JSON as:
|
40
|
+
|
41
|
+
```json
|
42
|
+
{
|
43
|
+
"id": 42,
|
44
|
+
"name": "John Doe",
|
45
|
+
"email": "john@nsa.gov"
|
46
|
+
}
|
47
|
+
```
|
48
|
+
|
49
|
+
Oh, wait... Your users don't respond to `#name`?
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class UserSerializer < ApplicationSerializer
|
53
|
+
attributes :id, :name, :email
|
54
|
+
|
55
|
+
def name
|
56
|
+
"#{object.first_name} #{object.last_name}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
That's fine. Your serializers can render fields your object don't respond to.
|
62
|
+
|
63
|
+
Now, to render the JSON object, you need to say the following in your
|
64
|
+
controller:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class UsersController < ApplicationController
|
68
|
+
def show
|
69
|
+
user = User.find(params[:id])
|
70
|
+
|
71
|
+
render json: user
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
The serializr library hooks itself into `ActionController::Base` or
|
77
|
+
`ActionController::API` and it can infer the `UserSerializer` out of the `User`
|
78
|
+
object. You can also be explicit, in which case the inferring logic won't be
|
79
|
+
triggered at all.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class UsersController < ApplicationController
|
83
|
+
def show
|
84
|
+
user = User.find(params[:id])
|
85
|
+
|
86
|
+
render json: user, serializer: UserSerializer
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
You can also render collections of objects:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class FriendsController < ApplicationController
|
95
|
+
def index
|
96
|
+
user = User.friends_of(params[:id])
|
97
|
+
|
98
|
+
render json: friends
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
Being explicit here may have performance benefits, as to guess the Serializer
|
104
|
+
class to use, we need to unroll the collection. The explicit usage, unarguably,
|
105
|
+
looks pretty awesome as well, so you can wow your friends! Which, is always
|
106
|
+
cool, you know. 😎
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
class FriendsController < ApplicationController
|
110
|
+
def index
|
111
|
+
user = User.friends_of(params[:id])
|
112
|
+
|
113
|
+
render json: friends, serializer: UserSerializer[]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
And this is how you drop `Action View` off your API's, kids!
|
119
|
+
|
120
|
+
(_Not really, but anyway, I have the stage now, I do the typing. Fuck off and
|
121
|
+
call my therapist!_)
|
122
|
+
|
123
|
+
### 1️⃣ Last Thing
|
124
|
+
|
125
|
+
To fill the API cliché, we need to go over one last file:
|
126
|
+
`app/serializers/application_serializer.rb`. At first, it looks like this:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
class ApplicationSerializer < Serializr
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
The grown ups call it [Layer
|
134
|
+
Supertype](http://martinfowler.com/eaaCatalog/layerSupertype.html). We'll call
|
135
|
+
it that thing that looks like `ApplicationController` and serves the same
|
136
|
+
purpose, but for the serializers, not the controllers. You can use it to put
|
137
|
+
common utilities shared by all the serializers.
|
138
|
+
|
139
|
+
For example:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
class ApplicationSerializer < Serializr
|
143
|
+
# You may need the routes helpers, so you can link between resources in your
|
144
|
+
# JSON responses.
|
145
|
+
inlcude Rails.application.routes.url_helpers
|
146
|
+
|
147
|
+
EPOCH_TIME = Time.at(0).in_time_zone('UTC')
|
148
|
+
|
149
|
+
# You may wanna render this timestamp instead of `null` for unset timestamps.
|
150
|
+
# Or do whatever, really. I'm not your parents.
|
151
|
+
def render_timestamp(timestamp)
|
152
|
+
timestamp || EPOCH_TIME
|
153
|
+
end
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
### 1️⃣ Last Thing, For Real This Time
|
158
|
+
|
159
|
+
Serializr? Really? I know. It's fine.
|
160
|
+
|
161
|
+
You can require `serializr`, you can require `serializer` as well. The
|
162
|
+
constants? Both of `Serializr` and `Serializer` point to the same thing. Same
|
163
|
+
for the generators. Use whatever your brain and 🖐 like.
|
164
|
+
|
165
|
+
## Contributing
|
166
|
+
|
167
|
+
Bug reports and pull requests are welcome on GitHub at
|
168
|
+
https://github.com/gsamokovarov/serializr. This project is intended to be a
|
169
|
+
safe, welcoming space for collaboration, and contributors are expected to
|
170
|
+
adhere to the [Contributor Covenant](http://contributor-covenant.org) code of
|
171
|
+
conduct.
|
172
|
+
|
173
|
+
## License
|
174
|
+
|
175
|
+
The gem is available as open source under the terms of the [MIT
|
176
|
+
License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "serializr"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Description:
|
2
|
+
Stubs out a new serializer. Pass the serializer name, either CamelCased or
|
3
|
+
under_scored and an optional list of attribute pairs as arguments.
|
4
|
+
|
5
|
+
Attribute pairs are field arguments specifying the exposed serializer's
|
6
|
+
JSON object attributes.
|
7
|
+
|
8
|
+
Finally, if --parent option is given, it's used as superclass of the
|
9
|
+
created serializer. The default parent is ApplicationSerializer, that you
|
10
|
+
can use in the same way ApplicationController: a place for common utilities
|
11
|
+
shared between all the serializers.
|
12
|
+
|
13
|
+
Example:
|
14
|
+
`rails generate serializer User`
|
15
|
+
|
16
|
+
This will create:
|
17
|
+
|
18
|
+
app/serializers/application_serializer.rb
|
19
|
+
app/serializers/user_serializer.rb
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Description:
|
2
|
+
Stubs out a new serializer. Pass the serializer name, either CamelCased or
|
3
|
+
under_scored and an optional list of attribute pairs as arguments.
|
4
|
+
|
5
|
+
Attribute pairs are field arguments specifying the exposed serializer's
|
6
|
+
JSON object attributes.
|
7
|
+
|
8
|
+
Finally, if --parent option is given, it's used as superclass of the
|
9
|
+
created serializer. The default parent is ApplicationSerializer, that you
|
10
|
+
can use in the same way ApplicationController: a place for common utilities
|
11
|
+
shared between all the serializers.
|
12
|
+
|
13
|
+
Example:
|
14
|
+
`rails generate serializer User`
|
15
|
+
|
16
|
+
This will create:
|
17
|
+
|
18
|
+
app/serializers/application_serializer.rb
|
19
|
+
app/serializers/user_serializer.rb
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
class SerializrGenerator < Rails::Generators::NamedBase
|
4
|
+
SERIALIZR_DIR = Pathname.new('app/serializers')
|
5
|
+
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
argument :attributes, type: :array, default: [], banner: 'field field'
|
9
|
+
|
10
|
+
check_class_collision suffix: 'Serializer'
|
11
|
+
class_option :parent, type: :string, desc: 'The parent class for the generated serializr'
|
12
|
+
|
13
|
+
def create_serializer_file
|
14
|
+
generate_application_serilzer unless non_standard_parent_class_name?
|
15
|
+
template 'serializr.rb', serializer_file_name
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def parent_class_name
|
21
|
+
options[:parent] || 'ApplicationSerializer'
|
22
|
+
end
|
23
|
+
|
24
|
+
def non_standard_parent_class_name?
|
25
|
+
parent_class_name != 'ApplicationSerializer'
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_application_serilzer
|
29
|
+
if self.behavior == :invoke && !application_serilzer_exist?
|
30
|
+
template 'application_serializr.rb', application_serilzer_file_name
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def application_serilzer_exist?
|
35
|
+
file_exist = nil
|
36
|
+
in_root { file_exist = File.exist?(application_serilzer_file_name) }
|
37
|
+
file_exist
|
38
|
+
end
|
39
|
+
|
40
|
+
def serializer_file_name
|
41
|
+
SERIALIZR_DIR.join(*class_path, "#{file_name}_serializer.rb").to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
def application_serilzer_file_name
|
45
|
+
SERIALIZR_DIR.join('application_serializer.rb').to_s
|
46
|
+
end
|
47
|
+
end
|
data/lib/serializer.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'serializr'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Serializr
|
2
|
+
# Integration for AbstactController::Metal subclasses like
|
3
|
+
# ActionController::Base or ActionController::API.
|
4
|
+
#
|
5
|
+
# Introduces a `:serializer` option that is automatically inferred by the
|
6
|
+
# resource to be rendered in `render :json`.
|
7
|
+
#
|
8
|
+
# Setting it explicitly prevents the inferring altogether.
|
9
|
+
module Integration
|
10
|
+
def _normalize_options(options)
|
11
|
+
# The resource can be nil, check if it is explicitly passed as such.
|
12
|
+
if options.key?(:json)
|
13
|
+
resource = options[:json]
|
14
|
+
serializer = options[:serializer] || begin
|
15
|
+
if resource.respond_to?(:to_ary)
|
16
|
+
# Assume a collection if the resource responds to #to_ary. The
|
17
|
+
# regular arrays and ActiveRecord::Relations do and will be
|
18
|
+
# represented as arrays, while hashes won't be.
|
19
|
+
inferred = serializer_class_cache[resource.to_ary.first.class]
|
20
|
+
inferred[] if inferred
|
21
|
+
else
|
22
|
+
serializer_class_cache[resource.class]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
options[:json] = serializer.new(resource, options) if serializer
|
27
|
+
end
|
28
|
+
|
29
|
+
super
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def serializer_class_cache
|
35
|
+
@@_serializer_class_cache ||= Hash.new do |hash, cls|
|
36
|
+
hash[cls] = "#{cls}Serializer".safe_constantize ||
|
37
|
+
# Try to infer the superclass, if someone let a serializer for it and
|
38
|
+
# not the concrete classes. Don't go deeper, though, just one level
|
39
|
+
# is enough.
|
40
|
+
"#{cls.superclass}Serializer".safe_constantize
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Serializr
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer 'serializr.initialize' do
|
4
|
+
require 'serializr/integration'
|
5
|
+
|
6
|
+
# For Rails 5, automatically include this in the API controllers.
|
7
|
+
if defined?(ActionController::API)
|
8
|
+
ActionController::API.include(::Serializr::Integration)
|
9
|
+
end
|
10
|
+
|
11
|
+
ActionController::Base.include(::Serializr::Integration)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/serializr.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
class Serializr
|
2
|
+
class << self
|
3
|
+
NOT_GIVEN = Object.new
|
4
|
+
|
5
|
+
def [](object = NOT_GIVEN, options = {})
|
6
|
+
cls = collection_class_cache[self]
|
7
|
+
object == NOT_GIVEN ? cls : cls.new(object, options)
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes(*attr_names)
|
11
|
+
if attr_names.empty?
|
12
|
+
@attrs ||= []
|
13
|
+
else
|
14
|
+
(@attrs ||= []).concat(attr_names)
|
15
|
+
attr_names.each { |attr| define_attribute_method(attr) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def inherited(cls)
|
22
|
+
return if self == Serializr
|
23
|
+
|
24
|
+
attrs_copy = [].concat(@attrs ||= [])
|
25
|
+
cls.instance_variable_set(:@attrs, attrs_copy)
|
26
|
+
end
|
27
|
+
|
28
|
+
def collection_class_cache
|
29
|
+
@collection_class_cache ||= Hash.new do |hash, cls|
|
30
|
+
hash[cls] = Class.new(cls) do
|
31
|
+
def as_json
|
32
|
+
serializer = self.class.superclass
|
33
|
+
object.map { |obj| serializer.new(obj, options).as_json }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def define_attribute_method(attr_name)
|
40
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
41
|
+
def #{attr_name}() object.#{attr_name} end
|
42
|
+
RUBY
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def initialize(object, options = {})
|
47
|
+
@object = object
|
48
|
+
@options = options
|
49
|
+
end
|
50
|
+
|
51
|
+
def as_json
|
52
|
+
Hash[self.class.attributes.map do |attr|
|
53
|
+
[attr, public_send(attr).as_json]
|
54
|
+
end]
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
attr_reader :object, :options
|
60
|
+
end
|
61
|
+
|
62
|
+
# Let's get the constant, even if we don't explicitly `require 'serializer'`.
|
63
|
+
Serializer = Serializr
|
64
|
+
|
65
|
+
require 'serializr/railtie' if defined?(Rails)
|
data/serializr.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'serializr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "serializr"
|
8
|
+
spec.version = Serializr::VERSION
|
9
|
+
spec.authors = ["Genadi Samokovarov"]
|
10
|
+
spec.email = ["gsamokovarov@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Plain and simple JSON serializer."
|
13
|
+
spec.description = "Plain and simple JSON serializer."
|
14
|
+
spec.homepage = "https://github.com/gsamokovarov/serializr"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "rails", "~> 5.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serializr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genadi Samokovarov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
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: Plain and simple JSON serializer.
|
70
|
+
email:
|
71
|
+
- gsamokovarov@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/generators/serializer/USAGE
|
86
|
+
- lib/generators/serializer/serializer_generator.rb
|
87
|
+
- lib/generators/serializr/USAGE
|
88
|
+
- lib/generators/serializr/serializr_generator.rb
|
89
|
+
- lib/generators/serializr/templates/application_serializr.rb
|
90
|
+
- lib/generators/serializr/templates/serializr.rb
|
91
|
+
- lib/serializer.rb
|
92
|
+
- lib/serializr.rb
|
93
|
+
- lib/serializr/integration.rb
|
94
|
+
- lib/serializr/railtie.rb
|
95
|
+
- lib/serializr/version.rb
|
96
|
+
- serializr.gemspec
|
97
|
+
homepage: https://github.com/gsamokovarov/serializr
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.5.1
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Plain and simple JSON serializer.
|
121
|
+
test_files: []
|