adequate_serialization 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +7 -0
- data/.travis.yml +12 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +154 -0
- data/LICENSE +21 -0
- data/README.md +190 -0
- data/Rakefile +12 -0
- data/adequate_serialization.gemspec +29 -0
- data/bin/console +8 -0
- data/bin/setup +7 -0
- data/lib/adequate_serialization.rb +34 -0
- data/lib/adequate_serialization/attribute.rb +116 -0
- data/lib/adequate_serialization/decorator.rb +31 -0
- data/lib/adequate_serialization/options.rb +23 -0
- data/lib/adequate_serialization/rails/cache_key.rb +19 -0
- data/lib/adequate_serialization/rails/cache_step.rb +20 -0
- data/lib/adequate_serialization/rails/hook.rb +21 -0
- data/lib/adequate_serialization/rails/relation_serializer.rb +62 -0
- data/lib/adequate_serialization/serializable.rb +19 -0
- data/lib/adequate_serialization/serializer.rb +31 -0
- data/lib/adequate_serialization/steps.rb +37 -0
- data/lib/adequate_serialization/steps/passthrough_step.rb +29 -0
- data/lib/adequate_serialization/steps/serialize_step.rb +14 -0
- data/lib/adequate_serialization/version.rb +5 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 344b36d7176362ee9a050bff405cd6a71d56971d6f6fe9f2dce5ed48a4aa9616
|
4
|
+
data.tar.gz: 79cacfe1d48763618a46d1ef342f49ae96b0c239589ee593260aad0f65acb781
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4bdb02b1d09a567d965857bd152f3846614f7b7ff2080431aa0c3360940009d4a459e68576d295ba9af8fae452be48bd2ad0fb8a3610d54543ec701177792e9a
|
7
|
+
data.tar.gz: 149976b0d5f6bf2eafa3742ea3f7bc0ad4315b6a759da1d320a29e739ca7cae334e79a492a2415e459c21e52f94900d41649cf8db602bd1eb6a5ce6890d9e2ae
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
adequate_serialization (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actioncable (5.2.0)
|
10
|
+
actionpack (= 5.2.0)
|
11
|
+
nio4r (~> 2.0)
|
12
|
+
websocket-driver (>= 0.6.1)
|
13
|
+
actionmailer (5.2.0)
|
14
|
+
actionpack (= 5.2.0)
|
15
|
+
actionview (= 5.2.0)
|
16
|
+
activejob (= 5.2.0)
|
17
|
+
mail (~> 2.5, >= 2.5.4)
|
18
|
+
rails-dom-testing (~> 2.0)
|
19
|
+
actionpack (5.2.0)
|
20
|
+
actionview (= 5.2.0)
|
21
|
+
activesupport (= 5.2.0)
|
22
|
+
rack (~> 2.0)
|
23
|
+
rack-test (>= 0.6.3)
|
24
|
+
rails-dom-testing (~> 2.0)
|
25
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
26
|
+
actionview (5.2.0)
|
27
|
+
activesupport (= 5.2.0)
|
28
|
+
builder (~> 3.1)
|
29
|
+
erubi (~> 1.4)
|
30
|
+
rails-dom-testing (~> 2.0)
|
31
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
32
|
+
activejob (5.2.0)
|
33
|
+
activesupport (= 5.2.0)
|
34
|
+
globalid (>= 0.3.6)
|
35
|
+
activemodel (5.2.0)
|
36
|
+
activesupport (= 5.2.0)
|
37
|
+
activerecord (5.2.0)
|
38
|
+
activemodel (= 5.2.0)
|
39
|
+
activesupport (= 5.2.0)
|
40
|
+
arel (>= 9.0)
|
41
|
+
activestorage (5.2.0)
|
42
|
+
actionpack (= 5.2.0)
|
43
|
+
activerecord (= 5.2.0)
|
44
|
+
marcel (~> 0.3.1)
|
45
|
+
activesupport (5.2.0)
|
46
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
47
|
+
i18n (>= 0.7, < 2)
|
48
|
+
minitest (~> 5.1)
|
49
|
+
tzinfo (~> 1.1)
|
50
|
+
arel (9.0.0)
|
51
|
+
ast (2.4.0)
|
52
|
+
builder (3.2.3)
|
53
|
+
concurrent-ruby (1.0.5)
|
54
|
+
crass (1.0.4)
|
55
|
+
docile (1.3.0)
|
56
|
+
erubi (1.7.1)
|
57
|
+
globalid (0.4.1)
|
58
|
+
activesupport (>= 4.2.0)
|
59
|
+
i18n (1.0.1)
|
60
|
+
concurrent-ruby (~> 1.0)
|
61
|
+
json (2.1.0)
|
62
|
+
loofah (2.2.2)
|
63
|
+
crass (~> 1.0.2)
|
64
|
+
nokogiri (>= 1.5.9)
|
65
|
+
mail (2.7.0)
|
66
|
+
mini_mime (>= 0.1.1)
|
67
|
+
marcel (0.3.2)
|
68
|
+
mimemagic (~> 0.3.2)
|
69
|
+
method_source (0.9.0)
|
70
|
+
mimemagic (0.3.2)
|
71
|
+
mini_mime (1.0.0)
|
72
|
+
mini_portile2 (2.3.0)
|
73
|
+
minitest (5.11.3)
|
74
|
+
nio4r (2.3.1)
|
75
|
+
nokogiri (1.8.2)
|
76
|
+
mini_portile2 (~> 2.3.0)
|
77
|
+
parallel (1.12.1)
|
78
|
+
parser (2.5.1.0)
|
79
|
+
ast (~> 2.4.0)
|
80
|
+
powerpack (0.1.1)
|
81
|
+
rack (2.0.5)
|
82
|
+
rack-test (1.0.0)
|
83
|
+
rack (>= 1.0, < 3)
|
84
|
+
rails (5.2.0)
|
85
|
+
actioncable (= 5.2.0)
|
86
|
+
actionmailer (= 5.2.0)
|
87
|
+
actionpack (= 5.2.0)
|
88
|
+
actionview (= 5.2.0)
|
89
|
+
activejob (= 5.2.0)
|
90
|
+
activemodel (= 5.2.0)
|
91
|
+
activerecord (= 5.2.0)
|
92
|
+
activestorage (= 5.2.0)
|
93
|
+
activesupport (= 5.2.0)
|
94
|
+
bundler (>= 1.3.0)
|
95
|
+
railties (= 5.2.0)
|
96
|
+
sprockets-rails (>= 2.0.0)
|
97
|
+
rails-dom-testing (2.0.3)
|
98
|
+
activesupport (>= 4.2.0)
|
99
|
+
nokogiri (>= 1.6)
|
100
|
+
rails-html-sanitizer (1.0.4)
|
101
|
+
loofah (~> 2.2, >= 2.2.2)
|
102
|
+
railties (5.2.0)
|
103
|
+
actionpack (= 5.2.0)
|
104
|
+
activesupport (= 5.2.0)
|
105
|
+
method_source
|
106
|
+
rake (>= 0.8.7)
|
107
|
+
thor (>= 0.18.1, < 2.0)
|
108
|
+
rainbow (3.0.0)
|
109
|
+
rake (12.3.1)
|
110
|
+
rubocop (0.56.0)
|
111
|
+
parallel (~> 1.10)
|
112
|
+
parser (>= 2.5)
|
113
|
+
powerpack (~> 0.1)
|
114
|
+
rainbow (>= 2.2.2, < 4.0)
|
115
|
+
ruby-progressbar (~> 1.7)
|
116
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
117
|
+
ruby-progressbar (1.9.0)
|
118
|
+
simplecov (0.16.1)
|
119
|
+
docile (~> 1.1)
|
120
|
+
json (>= 1.8, < 3)
|
121
|
+
simplecov-html (~> 0.10.0)
|
122
|
+
simplecov-html (0.10.2)
|
123
|
+
sprockets (3.7.1)
|
124
|
+
concurrent-ruby (~> 1.0)
|
125
|
+
rack (> 1, < 3)
|
126
|
+
sprockets-rails (3.2.1)
|
127
|
+
actionpack (>= 4.0)
|
128
|
+
activesupport (>= 4.0)
|
129
|
+
sprockets (>= 3.0.0)
|
130
|
+
sqlite3 (1.3.13)
|
131
|
+
thor (0.20.0)
|
132
|
+
thread_safe (0.3.6)
|
133
|
+
tzinfo (1.2.5)
|
134
|
+
thread_safe (~> 0.1)
|
135
|
+
unicode-display_width (1.3.2)
|
136
|
+
websocket-driver (0.7.0)
|
137
|
+
websocket-extensions (>= 0.1.0)
|
138
|
+
websocket-extensions (0.1.3)
|
139
|
+
|
140
|
+
PLATFORMS
|
141
|
+
ruby
|
142
|
+
|
143
|
+
DEPENDENCIES
|
144
|
+
adequate_serialization!
|
145
|
+
bundler (~> 1)
|
146
|
+
minitest (~> 5)
|
147
|
+
rails (~> 5, >= 5.2)
|
148
|
+
rake (~> 12)
|
149
|
+
rubocop (~> 0.52)
|
150
|
+
simplecov (~> 0.16)
|
151
|
+
sqlite3
|
152
|
+
|
153
|
+
BUNDLED WITH
|
154
|
+
1.16.1
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Kevin Deisz
|
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,190 @@
|
|
1
|
+
# AdequateSerialization
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/kddeisz/adequate_serialization.svg?branch=master)](https://travis-ci.org/kddeisz/adequate_serialization)
|
4
|
+
|
5
|
+
Serializes objects adequately. `AdequateSerialization` allows you to define serializers that will convert your objects into simple hashes that are suitable for variable purposes such as caching or using in an HTTP response. It stems from the simple idea of giving slightly more control over the `as_json` method that gets called when objects are serialized using Rails' default controller serialization.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'adequate_serialization'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install adequate_serialization
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
First, include `AdequateSerialization::Serializable` in any object that you want to be able to serialize. Then, define a serializer matching the name of that object, postfixed with `"Serializer"`. Use the `AdequateSerialization` DSL to define the attributes that are available to the serializer. You can then call `as_json` on any instance of that object to get the resultant hash. Below is an example:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class User
|
29
|
+
include AdequateSerialization::Serializable
|
30
|
+
|
31
|
+
attr_reader :id, :name, :title
|
32
|
+
|
33
|
+
...
|
34
|
+
end
|
35
|
+
|
36
|
+
class UserSerializer < AdequateSerialization::Serializer
|
37
|
+
attribute :id, :name, :title
|
38
|
+
end
|
39
|
+
|
40
|
+
User.new(id: 1, name: 'Clark Kent', title: 'Superman').as_json
|
41
|
+
# => {:id=>1, :name=>"Clark Kent", :title=>"Superman"}
|
42
|
+
```
|
43
|
+
|
44
|
+
### Defining attributes
|
45
|
+
|
46
|
+
The `AdequateSerialization::Serializer` DSL is just the one `attribute` method. You can pass as many names as you want, and each attribute will become a key in the resultant serialized hash. If you need to build a "synthesized" attribute (one that is defined in the serializer), you can do so with a block that receives the object as an argument, as in:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class UserSerializer < AdequateSerialization::Serializer
|
50
|
+
attribute :double_name do |user|
|
51
|
+
user.name * 2
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
There are also a couple of options that you can pass to the `attribute` method as the last argument that modify the serializer's behavior, listed below.
|
57
|
+
|
58
|
+
#### :if
|
59
|
+
|
60
|
+
If you pass an `:if` condition, that method will be called on the serializable object to determine whether or not that attribute should be included in the resultant hash, as in:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class UserSerializer < AdequateSerialization::Serializer
|
64
|
+
attribute :title, if: :manager?
|
65
|
+
end
|
66
|
+
|
67
|
+
user = User.new(...)
|
68
|
+
user.as_json
|
69
|
+
# => {:id=>1, :name=>"Clark Kent"}
|
70
|
+
|
71
|
+
user.update(manager: true)
|
72
|
+
user.as_json
|
73
|
+
# => {:id=>1, :name=>"Clark Kent", :title=>"Superman"}
|
74
|
+
```
|
75
|
+
|
76
|
+
#### :unless
|
77
|
+
|
78
|
+
This is the same as the `:if` option, but will result in the opposite behavior (the attribute will be present if the predicate is not met).
|
79
|
+
|
80
|
+
#### :optional
|
81
|
+
|
82
|
+
There are times when you want to include an attribute that you normally wouldn't. For example, if you have both `Post` and `Comment` objects, normally you wouldn't include the `post` attribute on the child `comment` objects. However, if you're serializing just the comment, it might be useful to have the `post` attached. In this case, you could mark the attribute as `optional` and it would only be included if it was listed in the `:includes` option passed to the `as_json` method, as in:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class PostSerializer < AdequateSerialization::Serializer
|
86
|
+
attribute :id, :title, :body
|
87
|
+
attribute :comments, optional: true
|
88
|
+
end
|
89
|
+
|
90
|
+
class CommentSerializer < AdequateSerialization::Serializer
|
91
|
+
attribute :id, :body
|
92
|
+
attribute :post, optional: true
|
93
|
+
end
|
94
|
+
|
95
|
+
comment = Comment.new(...)
|
96
|
+
comment.as_json
|
97
|
+
# => {:id=>1, :body=>"This is a great gem!"}
|
98
|
+
|
99
|
+
comment.as_json(includes: :post)
|
100
|
+
# => {:id=>1, :body=>"This is a great gem!", :post=>{:id=>1, :title=>"Introducing Adequate Serializer", :body=>"This is adequate serializer."}}
|
101
|
+
```
|
102
|
+
|
103
|
+
The `includes` key can take either a single name or an array of names.
|
104
|
+
|
105
|
+
### Attaching objects
|
106
|
+
|
107
|
+
There are times where it's more performant to serialize the objects using normal serialization and to attach an additional attribute later. For instance, you could serialize all of the posts and then attach whether or not a user had upvoted them. In that case, there's a special syntax that looks like the below:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
upvotes =
|
111
|
+
User.upvotes.each_with_object({}) do |post, votes|
|
112
|
+
votes[post.id] = true
|
113
|
+
end
|
114
|
+
# => {1=>true}
|
115
|
+
|
116
|
+
Post.all.map(&:as_json)
|
117
|
+
# => [{:id=>1}, {:id=>2}]
|
118
|
+
|
119
|
+
posts = Post.all.map { |post| post.as_json(attach: { upvoted: upvotes }) }
|
120
|
+
# => [{:id=>1, :upvoted=>true}, {:id=>2, :upvoted=>false}]
|
121
|
+
```
|
122
|
+
|
123
|
+
This relies on the objects to which you are attaching having an `id` attribute and the attachable hash being an index of `id` pointing to the attribute value.
|
124
|
+
|
125
|
+
### Usage with Rails
|
126
|
+
|
127
|
+
To get `AdequateSerializer` working with Rails, you can call `AdequateSerializion.hook_into_rails!` in an initializer. This will do three things:
|
128
|
+
|
129
|
+
1. Introduce caching behavior so that when serializing objects they will by default be stored in the Rails cache.
|
130
|
+
1. Include `AdequateSerializer::Serializable` in `ActiveRecord::Base` so that all of your models will be serializable.
|
131
|
+
2. Overwrite `ActiveRecord::Relation`'s `as_json` method to use the `AdequateSerializer::Rails::RelationSerializer` object, which by default will use the `Rails.cache.fetch_multi` method in order to more efficiently serialize all of the records in the relation.
|
132
|
+
|
133
|
+
You can still use plain objects to be serialized, and if you want to take advantage of the caching behavior, you can define a `cache_key` method on the objects that you're serializing. This will cause `AdequateSerialization` to start putting them into the Rails cache.
|
134
|
+
|
135
|
+
The result is that you can now this in your controllers:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
class UsersController
|
139
|
+
def show
|
140
|
+
user = User.find(params[:id])
|
141
|
+
|
142
|
+
render json: { user: user }
|
143
|
+
end
|
144
|
+
end
|
145
|
+
```
|
146
|
+
|
147
|
+
and the response will be the serialized user. You can pass additional options that will get forwarded on to the serializer as well, as in:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
class UsersController
|
151
|
+
def show
|
152
|
+
user = User.find(params[:id])
|
153
|
+
|
154
|
+
render json: { user: user }, includes: :title
|
155
|
+
end
|
156
|
+
end
|
157
|
+
```
|
158
|
+
|
159
|
+
and the result will now contain the `title` attribute (provided it was configured as an optional attribute). All options that previously were passed in to the `as_json` method get forwarded appropriately.
|
160
|
+
|
161
|
+
### Advanced
|
162
|
+
|
163
|
+
The serialization process happens through a series of `AdequateSerialization::Steps`. The caching behavior mentioned in the `Usage with Rails` section is one such step that gets introduced. You can introduce more yourself like so:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
class LoggingStep < AdequateSerialization::Steps::PassthroughStep
|
167
|
+
def apply(response)
|
168
|
+
Logger.log("#{response.object} is being serialized with #{response.opts} options")
|
169
|
+
apply_next(response)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
AdequateSerialization.prepend(LoggingStep)
|
174
|
+
```
|
175
|
+
|
176
|
+
This will cause this object to be placed into the list of steps taken to serialize objects, and can be used for much more powerful and advanced workflows.
|
177
|
+
|
178
|
+
## Development
|
179
|
+
|
180
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
181
|
+
|
182
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
183
|
+
|
184
|
+
## Contributing
|
185
|
+
|
186
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kddeisz/adequate_serialization.
|
187
|
+
|
188
|
+
## License
|
189
|
+
|
190
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'adequate_serialization/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'adequate_serialization'
|
9
|
+
spec.version = AdequateSerialization::VERSION
|
10
|
+
spec.authors = ['Kevin Deisz']
|
11
|
+
spec.email = ['kevin.deisz@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Serializes objects adequately'
|
14
|
+
spec.homepage = 'https://github.com/kddeisz/adequate_serialization'
|
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'
|
25
|
+
spec.add_development_dependency 'minitest', '~> 5'
|
26
|
+
spec.add_development_dependency 'rake', '~> 12'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.52'
|
28
|
+
spec.add_development_dependency 'simplecov', '~> 0.16'
|
29
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'adequate_serialization/attribute'
|
4
|
+
require 'adequate_serialization/decorator'
|
5
|
+
require 'adequate_serialization/options'
|
6
|
+
require 'adequate_serialization/serializable'
|
7
|
+
require 'adequate_serialization/serializer'
|
8
|
+
require 'adequate_serialization/steps'
|
9
|
+
require 'adequate_serialization/version'
|
10
|
+
|
11
|
+
require 'adequate_serialization/steps/passthrough_step'
|
12
|
+
require 'adequate_serialization/steps/serialize_step'
|
13
|
+
|
14
|
+
module AdequateSerialization
|
15
|
+
class << self
|
16
|
+
def dump(object)
|
17
|
+
if object.is_a?(Hash)
|
18
|
+
object
|
19
|
+
elsif object.respond_to?(:as_json)
|
20
|
+
object.as_json
|
21
|
+
else
|
22
|
+
object
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def hook_into_rails!
|
27
|
+
@hook_into_rails ||=
|
28
|
+
begin
|
29
|
+
require 'adequate_serialization/rails/hook'
|
30
|
+
Rails.hook_in!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Attribute
|
5
|
+
class Simple
|
6
|
+
attr_reader :name
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def serialize_to(response, model, _includes)
|
13
|
+
response[name] = AdequateSerialization.dump(model.public_send(name))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Synthesized
|
18
|
+
attr_reader :name, :block
|
19
|
+
|
20
|
+
def initialize(name, &block)
|
21
|
+
@name = name
|
22
|
+
@block = block
|
23
|
+
end
|
24
|
+
|
25
|
+
def serialize_to(response, model, _includes)
|
26
|
+
response[name] = AdequateSerialization.dump(block.call(model))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class IfCondition
|
31
|
+
attr_reader :attribute, :condition
|
32
|
+
|
33
|
+
def initialize(attribute, condition)
|
34
|
+
@attribute = attribute
|
35
|
+
@condition = condition
|
36
|
+
end
|
37
|
+
|
38
|
+
def serialize_to(response, model, includes)
|
39
|
+
return unless model.public_send(condition)
|
40
|
+
attribute.serialize_to(response, model, includes)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class UnlessCondition
|
45
|
+
attr_reader :attribute, :condition
|
46
|
+
|
47
|
+
def initialize(attribute, condition)
|
48
|
+
@attribute = attribute
|
49
|
+
@condition = condition
|
50
|
+
end
|
51
|
+
|
52
|
+
def serialize_to(response, model, includes)
|
53
|
+
return if model.public_send(condition)
|
54
|
+
attribute.serialize_to(response, model, includes)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Optional
|
59
|
+
attr_reader :attribute
|
60
|
+
|
61
|
+
def initialize(attribute)
|
62
|
+
@attribute = attribute
|
63
|
+
end
|
64
|
+
|
65
|
+
def serialize_to(response, model, includes)
|
66
|
+
return unless includes.include?(attribute.name)
|
67
|
+
attribute.serialize_to(response, model, includes)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Config
|
72
|
+
attr_reader :attribute, :options
|
73
|
+
|
74
|
+
def initialize(attribute, options)
|
75
|
+
@attribute = attribute
|
76
|
+
@options = options
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_attribute
|
80
|
+
nested = nested_attribute_from(attribute, options)
|
81
|
+
nested ? Config.new(nested, options).to_attribute : attribute
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def nested_attribute_from(attribute, options)
|
87
|
+
if options.delete(:optional)
|
88
|
+
Optional.new(attribute)
|
89
|
+
elsif if_option
|
90
|
+
IfCondition.new(attribute, if_option)
|
91
|
+
elsif unless_option
|
92
|
+
UnlessCondition.new(attribute, unless_option)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def if_option
|
97
|
+
@if_option ||= options.delete(:if)
|
98
|
+
end
|
99
|
+
|
100
|
+
def unless_option
|
101
|
+
@unless_option ||= options.delete(:unless)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.from(name, options = {}, &block)
|
106
|
+
attribute =
|
107
|
+
if block
|
108
|
+
Synthesized.new(name, &block)
|
109
|
+
else
|
110
|
+
Simple.new(name)
|
111
|
+
end
|
112
|
+
|
113
|
+
Config.new(attribute, options).to_attribute
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Decorator
|
5
|
+
class Null
|
6
|
+
def decorate(result)
|
7
|
+
result
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Attachments
|
12
|
+
attr_reader :attachments
|
13
|
+
|
14
|
+
def initialize(attachments)
|
15
|
+
@attachments = attachments
|
16
|
+
end
|
17
|
+
|
18
|
+
def decorate(result)
|
19
|
+
attachments.each do |name, attachment|
|
20
|
+
result[name] = attachment[result[:id]]
|
21
|
+
end
|
22
|
+
|
23
|
+
result
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.from(attachments)
|
28
|
+
attachments.empty? ? Null.new : Attachments.new(attachments)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Options
|
5
|
+
class Opts
|
6
|
+
attr_reader :includes, :attachments, :options
|
7
|
+
|
8
|
+
def initialize(includes: [], attach: {}, **options)
|
9
|
+
@includes = Array(includes)
|
10
|
+
@attachments = attach
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.from(*opts)
|
16
|
+
Opts.new(opts[0] || {})
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.null
|
20
|
+
Opts.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Rails
|
5
|
+
module CacheKey
|
6
|
+
def self.cacheable?(object)
|
7
|
+
if object.class < ActiveRecord::Base
|
8
|
+
object.has_attribute?(:updated_at)
|
9
|
+
else
|
10
|
+
object.respond_to?(:cache_key)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.for(object, includes = [])
|
15
|
+
includes.empty? ? object : [object, *includes]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Rails
|
5
|
+
class CacheStep < Steps::PassthroughStep
|
6
|
+
def apply(response)
|
7
|
+
object = response.object
|
8
|
+
opts = response.opts
|
9
|
+
|
10
|
+
if opts.options[:multi_caching] || !CacheKey.cacheable?(object)
|
11
|
+
return apply_next(response)
|
12
|
+
end
|
13
|
+
|
14
|
+
::Rails.cache.fetch(CacheKey.for(object, opts.includes)) do
|
15
|
+
apply_next(response)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'adequate_serialization/rails/cache_key'
|
4
|
+
require 'adequate_serialization/rails/cache_step'
|
5
|
+
require 'adequate_serialization/rails/relation_serializer'
|
6
|
+
|
7
|
+
module AdequateSerialization
|
8
|
+
module Rails
|
9
|
+
module RelationHook
|
10
|
+
def as_json(*options)
|
11
|
+
RelationSerializer.new(self).as_json(*options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.hook_in!
|
16
|
+
ActiveRecord::Base.include(Serializable)
|
17
|
+
ActiveRecord::Relation.include(RelationHook)
|
18
|
+
AdequateSerialization.prepend(CacheStep)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Rails
|
5
|
+
class RelationSerializer
|
6
|
+
attr_reader :relation
|
7
|
+
|
8
|
+
def initialize(relation)
|
9
|
+
@relation = relation
|
10
|
+
end
|
11
|
+
|
12
|
+
def as_json(*options)
|
13
|
+
return [] if relation.empty?
|
14
|
+
|
15
|
+
opts = Options.from(*options, multi_caching: true)
|
16
|
+
decorator = Decorator.from(opts.attachments)
|
17
|
+
|
18
|
+
response_for(opts, decorator)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def response_for(opts, decorator)
|
24
|
+
cache_keys = cache_keys_for(opts)
|
25
|
+
|
26
|
+
if cache_keys
|
27
|
+
cacheable_response_for(opts, decorator, cache_keys)
|
28
|
+
else
|
29
|
+
uncacheable_response_for(opts, decorator)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def cache_keys_for(opts)
|
34
|
+
relation.map do |record|
|
35
|
+
return nil unless CacheKey.cacheable?(record)
|
36
|
+
CacheKey.for(record, opts.includes)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def cacheable_response_for(opts, decorator, cache_keys)
|
41
|
+
results =
|
42
|
+
::Rails.cache.fetch_multi(*cache_keys) do |(record, *)|
|
43
|
+
serialize(record, opts)
|
44
|
+
end
|
45
|
+
|
46
|
+
cache_keys.map do |cache_key|
|
47
|
+
decorator.decorate(results[cache_key])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def uncacheable_response_for(opts, decorator)
|
52
|
+
relation.map do |record|
|
53
|
+
decorator.decorate(serialize(record, opts))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def serialize(record, opts)
|
58
|
+
record.class.serializer.serialize(record, opts)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Serializable
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def serializer
|
11
|
+
@serializer ||= const_get("#{name}Serializer").new
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def as_json(*opts)
|
16
|
+
Steps.apply(self, *opts)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
class Serializer
|
5
|
+
class << self
|
6
|
+
def attributes
|
7
|
+
@attributes ||= []
|
8
|
+
end
|
9
|
+
|
10
|
+
def attribute(*names, &block)
|
11
|
+
options =
|
12
|
+
if names.last.is_a?(Hash)
|
13
|
+
names.pop
|
14
|
+
else
|
15
|
+
{}
|
16
|
+
end
|
17
|
+
|
18
|
+
additions =
|
19
|
+
names.map! { |name| Attribute.from(name, options.dup, &block) }
|
20
|
+
|
21
|
+
@attributes = attributes + additions
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def serialize(model, opts = Options.null)
|
26
|
+
self.class.attributes.each_with_object({}) do |attribute, response|
|
27
|
+
attribute.serialize_to(response, model, opts.includes)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Steps
|
5
|
+
def self.apply(object, *options)
|
6
|
+
opts = Options.from(*options)
|
7
|
+
|
8
|
+
response = Response.new(object, opts)
|
9
|
+
decorator = Decorator.from(opts.attachments)
|
10
|
+
|
11
|
+
decorator.decorate(AdequateSerialization.steps.apply(response))
|
12
|
+
end
|
13
|
+
|
14
|
+
class Response
|
15
|
+
attr_reader :object, :opts
|
16
|
+
|
17
|
+
def initialize(object, opts = {})
|
18
|
+
@object = object
|
19
|
+
@opts = opts
|
20
|
+
end
|
21
|
+
|
22
|
+
def mutate(new_object)
|
23
|
+
self.class.new(new_object, opts)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class << self
|
29
|
+
def prepend(step)
|
30
|
+
@steps = step.new(steps)
|
31
|
+
end
|
32
|
+
|
33
|
+
def steps
|
34
|
+
@steps ||= Steps::SerializeStep.new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Steps
|
5
|
+
class LastStep
|
6
|
+
def apply(response)
|
7
|
+
response.object
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class PassthroughStep
|
12
|
+
attr_reader :next_step
|
13
|
+
|
14
|
+
def initialize(next_step = LastStep.new)
|
15
|
+
@next_step = next_step
|
16
|
+
end
|
17
|
+
|
18
|
+
def apply(response)
|
19
|
+
apply_next(response)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def apply_next(response)
|
25
|
+
next_step.apply(response)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdequateSerialization
|
4
|
+
module Steps
|
5
|
+
class SerializeStep < PassthroughStep
|
6
|
+
def apply(response)
|
7
|
+
object = response.object
|
8
|
+
serialized = object.class.serializer.serialize(object, response.opts)
|
9
|
+
|
10
|
+
apply_next(response.mutate(serialized))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: adequate_serialization
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Deisz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-17 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'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5'
|
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'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.52'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.52'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.16'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.16'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- kevin.deisz@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rubocop.yml"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- adequate_serialization.gemspec
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/adequate_serialization.rb
|
102
|
+
- lib/adequate_serialization/attribute.rb
|
103
|
+
- lib/adequate_serialization/decorator.rb
|
104
|
+
- lib/adequate_serialization/options.rb
|
105
|
+
- lib/adequate_serialization/rails/cache_key.rb
|
106
|
+
- lib/adequate_serialization/rails/cache_step.rb
|
107
|
+
- lib/adequate_serialization/rails/hook.rb
|
108
|
+
- lib/adequate_serialization/rails/relation_serializer.rb
|
109
|
+
- lib/adequate_serialization/serializable.rb
|
110
|
+
- lib/adequate_serialization/serializer.rb
|
111
|
+
- lib/adequate_serialization/steps.rb
|
112
|
+
- lib/adequate_serialization/steps/passthrough_step.rb
|
113
|
+
- lib/adequate_serialization/steps/serialize_step.rb
|
114
|
+
- lib/adequate_serialization/version.rb
|
115
|
+
homepage: https://github.com/kddeisz/adequate_serialization
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.7.6
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Serializes objects adequately
|
139
|
+
test_files: []
|