fast_serializer_ruby 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +39 -0
- data/CHANGELOG.md +0 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +128 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/Rakefile +6 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/fast_serializer.gemspec +46 -0
- data/lib/fast_serializer.rb +9 -0
- data/lib/fast_serializer/configuration.rb +19 -0
- data/lib/fast_serializer/json_model.rb +14 -0
- data/lib/fast_serializer/json_model/array.rb +21 -0
- data/lib/fast_serializer/json_model/attribute.rb +27 -0
- data/lib/fast_serializer/json_model/has_many_relationship.rb +16 -0
- data/lib/fast_serializer/json_model/has_one_relationship.rb +11 -0
- data/lib/fast_serializer/json_model/node.rb +23 -0
- data/lib/fast_serializer/json_model/object.rb +35 -0
- data/lib/fast_serializer/json_model/relationship.rb +22 -0
- data/lib/fast_serializer/schema.rb +165 -0
- data/lib/fast_serializer/version.rb +3 -0
- metadata +196 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7dbf448b48b08ceaf77ded99aa271a3e7286ca89a22291ce04a2b5dc5ec91163
|
4
|
+
data.tar.gz: e80488a2d02146eb91f875975d71ca8445a77e67ef25dc3c7ab86f46ac9f8e29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8a92983e1d8d8554e70221d3ee5419782c6719c9ecbebe64cfad15cdd68e88816b29fe540596c2aa644a82e739bda5957ac0fd2eceae034ed845d64e4e12006
|
7
|
+
data.tar.gz: 8c01ef66cb38709110527ff7df02942a6a9fbda4385e2accc07caac2c44b49487a84390b0a68bec70836d4cc07a61b6d4e86d8c6cd5fba54e637bb8a6e1d76ac
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
sudo: false
|
2
|
+
|
3
|
+
language: ruby
|
4
|
+
|
5
|
+
rvm:
|
6
|
+
- 2.3
|
7
|
+
- 2.4
|
8
|
+
- 2.5
|
9
|
+
- 2.6
|
10
|
+
- jruby-9.2.0.0
|
11
|
+
- ruby-head
|
12
|
+
- jruby-head
|
13
|
+
|
14
|
+
matrix:
|
15
|
+
allow_failures:
|
16
|
+
- rvm: jruby-9.2.0.0
|
17
|
+
- rvm: ruby-head
|
18
|
+
- rvm: jruby-head
|
19
|
+
|
20
|
+
before_install:
|
21
|
+
- gem update --system -N
|
22
|
+
- gem install bundler:2.0.2 -N
|
23
|
+
|
24
|
+
before_script:
|
25
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
26
|
+
- chmod +x ./cc-test-reporter
|
27
|
+
- ./cc-test-reporter before-build
|
28
|
+
|
29
|
+
script: bundle exec rspec --tag ~performance:true
|
30
|
+
|
31
|
+
after_script:
|
32
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
33
|
+
|
34
|
+
cache: bundler
|
35
|
+
|
36
|
+
env:
|
37
|
+
global:
|
38
|
+
CC_TEST_REPORTER_ID=a051d3d3560aa2e58cc5efb9f7b8eaafe87f983a1c4d6cb1573fda50ede5b685
|
39
|
+
COVERAGE=1
|
data/CHANGELOG.md
ADDED
File without changes
|
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 evgeny.stepanov@jetruby.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/Gemfile.lock
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fast_serializer_ruby (0.1.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (5.2.3)
|
10
|
+
actionview (= 5.2.3)
|
11
|
+
activesupport (= 5.2.3)
|
12
|
+
rack (~> 2.0)
|
13
|
+
rack-test (>= 0.6.3)
|
14
|
+
rails-dom-testing (~> 2.0)
|
15
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
16
|
+
actionview (5.2.3)
|
17
|
+
activesupport (= 5.2.3)
|
18
|
+
builder (~> 3.1)
|
19
|
+
erubi (~> 1.4)
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
22
|
+
active_model_serializers (0.10.10)
|
23
|
+
actionpack (>= 4.1, < 6.1)
|
24
|
+
activemodel (>= 4.1, < 6.1)
|
25
|
+
case_transform (>= 0.2)
|
26
|
+
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
|
27
|
+
activemodel (5.2.3)
|
28
|
+
activesupport (= 5.2.3)
|
29
|
+
activesupport (5.2.3)
|
30
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
|
+
i18n (>= 0.7, < 2)
|
32
|
+
minitest (~> 5.1)
|
33
|
+
tzinfo (~> 1.1)
|
34
|
+
benchmark-malloc (0.1.0)
|
35
|
+
benchmark-perf (0.5.0)
|
36
|
+
benchmark-trend (0.3.0)
|
37
|
+
builder (3.2.3)
|
38
|
+
case_transform (0.2)
|
39
|
+
activesupport
|
40
|
+
coderay (1.1.2)
|
41
|
+
concurrent-ruby (1.1.5)
|
42
|
+
crass (1.0.4)
|
43
|
+
diff-lcs (1.3)
|
44
|
+
docile (1.3.2)
|
45
|
+
erubi (1.8.0)
|
46
|
+
factory_bot (5.0.2)
|
47
|
+
activesupport (>= 4.2.0)
|
48
|
+
faker (1.9.6)
|
49
|
+
i18n (>= 0.7)
|
50
|
+
ffi (1.11.1-java)
|
51
|
+
i18n (1.6.0)
|
52
|
+
concurrent-ruby (~> 1.0)
|
53
|
+
json (2.2.0)
|
54
|
+
json (2.2.0-java)
|
55
|
+
jsonapi-renderer (0.2.2)
|
56
|
+
loofah (2.2.3)
|
57
|
+
crass (~> 1.0.2)
|
58
|
+
nokogiri (>= 1.5.9)
|
59
|
+
method_source (0.9.2)
|
60
|
+
mini_portile2 (2.4.0)
|
61
|
+
minitest (5.11.3)
|
62
|
+
nokogiri (1.10.3)
|
63
|
+
mini_portile2 (~> 2.4.0)
|
64
|
+
nokogiri (1.10.3-java)
|
65
|
+
pry (0.12.2)
|
66
|
+
coderay (~> 1.1.0)
|
67
|
+
method_source (~> 0.9.0)
|
68
|
+
pry (0.12.2-java)
|
69
|
+
coderay (~> 1.1.0)
|
70
|
+
method_source (~> 0.9.0)
|
71
|
+
spoon (~> 0.0)
|
72
|
+
rack (2.0.7)
|
73
|
+
rack-test (1.1.0)
|
74
|
+
rack (>= 1.0, < 3)
|
75
|
+
rails-dom-testing (2.0.3)
|
76
|
+
activesupport (>= 4.2.0)
|
77
|
+
nokogiri (>= 1.6)
|
78
|
+
rails-html-sanitizer (1.0.4)
|
79
|
+
loofah (~> 2.2, >= 2.2.2)
|
80
|
+
rake (10.5.0)
|
81
|
+
rspec (3.8.0)
|
82
|
+
rspec-core (~> 3.8.0)
|
83
|
+
rspec-expectations (~> 3.8.0)
|
84
|
+
rspec-mocks (~> 3.8.0)
|
85
|
+
rspec-benchmark (0.5.0)
|
86
|
+
benchmark-malloc (~> 0.1.0)
|
87
|
+
benchmark-perf (~> 0.5.0)
|
88
|
+
benchmark-trend (~> 0.3.0)
|
89
|
+
rspec (>= 3.0.0, < 4.0.0)
|
90
|
+
rspec-core (3.8.2)
|
91
|
+
rspec-support (~> 3.8.0)
|
92
|
+
rspec-expectations (3.8.4)
|
93
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
94
|
+
rspec-support (~> 3.8.0)
|
95
|
+
rspec-mocks (3.8.1)
|
96
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
97
|
+
rspec-support (~> 3.8.0)
|
98
|
+
rspec-support (3.8.2)
|
99
|
+
simplecov (0.17.0)
|
100
|
+
docile (~> 1.1)
|
101
|
+
json (>= 1.8, < 3)
|
102
|
+
simplecov-html (~> 0.10.0)
|
103
|
+
simplecov-html (0.10.2)
|
104
|
+
spoon (0.0.6)
|
105
|
+
ffi
|
106
|
+
thread_safe (0.3.6)
|
107
|
+
thread_safe (0.3.6-java)
|
108
|
+
tzinfo (1.2.5)
|
109
|
+
thread_safe (~> 0.1)
|
110
|
+
|
111
|
+
PLATFORMS
|
112
|
+
java
|
113
|
+
ruby
|
114
|
+
|
115
|
+
DEPENDENCIES
|
116
|
+
active_model_serializers (~> 0.10.0)
|
117
|
+
activesupport
|
118
|
+
factory_bot
|
119
|
+
faker
|
120
|
+
fast_serializer_ruby!
|
121
|
+
pry
|
122
|
+
rake (~> 10.0)
|
123
|
+
rspec (~> 3.0)
|
124
|
+
rspec-benchmark
|
125
|
+
simplecov
|
126
|
+
|
127
|
+
BUNDLED WITH
|
128
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Evgeny Stepanov
|
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,116 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/estepnv/fast_serializer.svg?branch=master)](https://travis-ci.org/estepnv/fast_serializer)
|
2
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/74cca93390234e619cf5/maintainability)](https://codeclimate.com/github/estepnv/fast_serializer/maintainability)
|
3
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/74cca93390234e619cf5/test_coverage)](https://codeclimate.com/github/estepnv/fast_serializer/test_coverage)
|
4
|
+
|
5
|
+
# fast_serializer
|
6
|
+
|
7
|
+
fast_serializer is a lightweight ruby objects serializer.
|
8
|
+
|
9
|
+
It has zero dependencies and written in pure ruby.
|
10
|
+
That's why it's so performant.
|
11
|
+
|
12
|
+
- running on ruby 2.6 is **at least 3 times faster** than AMS (benchmarks was borrowed from fast_jsonapi repository)
|
13
|
+
- running on jruby 9.2.7.0 **is at least 15 times faster** than AMS after warming up
|
14
|
+
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'fast_serializer'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install fast_serializer
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
fast_serializer supports default schema definition using class methods
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class ResourceSerializer
|
38
|
+
include FastSerializer::Schema::Mixin
|
39
|
+
root :resource
|
40
|
+
|
41
|
+
attributes :id, :email, :phone
|
42
|
+
|
43
|
+
attribute(:string_id, if: -> (resource, params) { params[:stringify] }) { |resource, params| resource.id.to_s }
|
44
|
+
attribute(:float_id, unless: -> (resource, params) { params[:stringify] }) { |resource, params| resource.id.to_f }
|
45
|
+
attribute(:full_name) { |resource, params| params[:only_first_name] ? resource.first_name : "#{resource.first_name} #{resource.last_name}" }
|
46
|
+
|
47
|
+
has_one :has_one_relationship, serializer: ResourceSerializer
|
48
|
+
has_many :has_many_relationship, serializer: ResourceSerializer
|
49
|
+
end
|
50
|
+
|
51
|
+
ResourceSerializer.new(resource, meta: {foo: "bar"}, only_first_name: false, stringify: true).serializable_hash
|
52
|
+
=> {
|
53
|
+
:resource => {
|
54
|
+
:id => 7873392581,
|
55
|
+
:email => "houston@luettgen.info",
|
56
|
+
:full_name => "Jamar Graham",
|
57
|
+
:phone => "627.051.6039 x1475",
|
58
|
+
:has_one_relationship => {
|
59
|
+
:id => 6218322696,
|
60
|
+
:email=>"terrellrobel@pagac.info",
|
61
|
+
:full_name => "Clay Kuphal",
|
62
|
+
:phone => "1-604-682-0732 x882"
|
63
|
+
}
|
64
|
+
},
|
65
|
+
meta: { foo: "bar" }
|
66
|
+
}
|
67
|
+
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
Also fast_serializer supports runtime schema definition
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
schema = FastSerializer::Schema.new(resource)
|
75
|
+
schema.attribute(:id)
|
76
|
+
schema.attribute(:email)
|
77
|
+
schema.attribute(:full_name) { |resource| "#{resource.first_name} #{resource.last_name}"}
|
78
|
+
schema.attribute(:phone)
|
79
|
+
schema.has_one(:has_one_relationship, serializer: schema)
|
80
|
+
|
81
|
+
schema.serializable_hash
|
82
|
+
=> {
|
83
|
+
:id => 7873392581,
|
84
|
+
:email => "houston@luettgen.info",
|
85
|
+
:full_name => "Jamar Graham",
|
86
|
+
:phone => "627.051.6039 x1475",
|
87
|
+
:has_one_relationship => {
|
88
|
+
:id => 6218322696,
|
89
|
+
:email=>"terrellrobel@pagac.info",
|
90
|
+
:full_name => "Clay Kuphal",
|
91
|
+
:phone => "1-604-682-0732 x882"
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
## Development
|
101
|
+
|
102
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
103
|
+
|
104
|
+
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).
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fast_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
113
|
+
|
114
|
+
## Code of Conduct
|
115
|
+
|
116
|
+
Everyone interacting in the FastSerializer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/fast_serializer/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fast_serializer"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "fast_serializer/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fast_serializer_ruby"
|
8
|
+
spec.version = FastSerializer::VERSION
|
9
|
+
spec.authors = ["Evgeny Stepanov"]
|
10
|
+
spec.email = ["estepnv@icloud.com"]
|
11
|
+
|
12
|
+
spec.summary = "Yet another serializer gem, but fast"
|
13
|
+
spec.description = "fast_serializer is a lightweight ruby objects serializer."
|
14
|
+
spec.homepage = "https://github.com/estepnv/fast_serializer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/estepnv/fast_serializer"
|
22
|
+
spec.metadata["changelog_uri"] = "https://github.com/estepnv/fast_serializer/CHANGELOG.md"
|
23
|
+
else
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
25
|
+
"public gem pushes."
|
26
|
+
end
|
27
|
+
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
30
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
31
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{(test|spec|features|git|rspec|travis)/}) }
|
32
|
+
end
|
33
|
+
spec.bindir = "exe"
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ["lib"]
|
36
|
+
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
39
|
+
spec.add_development_dependency "faker"
|
40
|
+
spec.add_development_dependency "simplecov"
|
41
|
+
spec.add_development_dependency "pry"
|
42
|
+
spec.add_development_dependency "activesupport"
|
43
|
+
spec.add_development_dependency "active_model_serializers", "~> 0.10.0"
|
44
|
+
spec.add_development_dependency "factory_bot"
|
45
|
+
spec.add_development_dependency "rspec-benchmark"
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module FastSerializer
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :coder
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@coder = JSON
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def config
|
12
|
+
@_config ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def configure(&block)
|
16
|
+
block.call(config) if !block.nil?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastSerializer
|
4
|
+
module JsonModel
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'fast_serializer/json_model/node'
|
9
|
+
require 'fast_serializer/json_model/object'
|
10
|
+
require 'fast_serializer/json_model/attribute'
|
11
|
+
require 'fast_serializer/json_model/relationship'
|
12
|
+
require 'fast_serializer/json_model/has_one_relationship'
|
13
|
+
require 'fast_serializer/json_model/has_many_relationship'
|
14
|
+
require 'fast_serializer/json_model/array'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastSerializer
|
4
|
+
module JsonModel
|
5
|
+
class Array < HasManyRelationship
|
6
|
+
|
7
|
+
def serialize(resources, params={})
|
8
|
+
return if resources.nil?
|
9
|
+
|
10
|
+
resources.map do |resource|
|
11
|
+
serialization_schema.serialize(resource, params)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def included?(resources, params = {})
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastSerializer
|
4
|
+
module JsonModel
|
5
|
+
class Attribute < Node
|
6
|
+
|
7
|
+
def serialize(resource, params={})
|
8
|
+
if method.is_a?(Proc)
|
9
|
+
method.arity.abs == 1 ? method.call(resource) : method.call(resource, params)
|
10
|
+
else
|
11
|
+
resource.public_send(method)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def included?(resource, params)
|
16
|
+
return true if @opts[:if].nil? && @opts[:unless].nil?
|
17
|
+
cond = @opts[:if] || @opts[:unless]
|
18
|
+
|
19
|
+
res = cond.call(resource, params)
|
20
|
+
res = !res if !@opts[:unless].nil?
|
21
|
+
|
22
|
+
res
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastSerializer
|
4
|
+
module JsonModel
|
5
|
+
class HasManyRelationship < Relationship
|
6
|
+
def serialize(resource, params={})
|
7
|
+
collection = resource.public_send(method)
|
8
|
+
return if collection.nil?
|
9
|
+
|
10
|
+
collection.map do |relation_resource|
|
11
|
+
serialization_schema.serialize(relation_resource, params)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastSerializer
|
4
|
+
module JsonModel
|
5
|
+
class Node
|
6
|
+
attr_accessor :key, :method
|
7
|
+
|
8
|
+
def initialize(key: nil, method: nil, opts: {}, **_)
|
9
|
+
@key = key
|
10
|
+
@method = method || key
|
11
|
+
@opts = opts || {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def serialize(resource, params={})
|
15
|
+
raise NotImplementedError
|
16
|
+
end
|
17
|
+
|
18
|
+
def included?(resource, params={})
|
19
|
+
raise NotImplementedError
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastSerializer
|
4
|
+
module JsonModel
|
5
|
+
class Object < Node
|
6
|
+
attr_accessor :attributes
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
super
|
10
|
+
@attributes = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_attribute(attribute)
|
14
|
+
attributes[attribute.key] = attribute
|
15
|
+
end
|
16
|
+
|
17
|
+
def serialize(resource, params = {})
|
18
|
+
return if resource.nil?
|
19
|
+
|
20
|
+
attributes.values.reduce({}) do |res, attribute|
|
21
|
+
next res unless attribute.included?(resource, params)
|
22
|
+
|
23
|
+
val = attribute.serialize(resource, params)
|
24
|
+
res[attribute.key] = val if val
|
25
|
+
res
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def included?(resource, params = {})
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module FastSerializer
|
2
|
+
module JsonModel
|
3
|
+
class Relationship < Attribute
|
4
|
+
attr_accessor :serialization_schema
|
5
|
+
|
6
|
+
def initialize(key: nil, method: nil, opts: {}, serialization_schema:)
|
7
|
+
super
|
8
|
+
@serialization_schema = serialization_schema
|
9
|
+
end
|
10
|
+
|
11
|
+
def included?(resource, params)
|
12
|
+
super(resource, params) && include_relation?(params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def include_relation?(params)
|
16
|
+
return true if params[:include].nil?
|
17
|
+
|
18
|
+
params[:include].include?(key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastSerializer
|
4
|
+
|
5
|
+
class Schema
|
6
|
+
|
7
|
+
module InheritanceSupport
|
8
|
+
|
9
|
+
def inherited(subclass)
|
10
|
+
subclass.instance_variable_set(:@_serialization_schema, _serialization_schema)
|
11
|
+
subclass.instance_variable_set(:@_root, _root)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
module SchemaInterface
|
17
|
+
|
18
|
+
def _serialization_schema
|
19
|
+
@_serialization_schema ||= JsonModel::Object.new
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_accessor :_root
|
23
|
+
|
24
|
+
# @param [Array] attribute_names
|
25
|
+
def attributes(*attribute_names)
|
26
|
+
attribute_names.each do |attribute_name|
|
27
|
+
_serialization_schema.add_attribute JsonModel::Attribute.new(
|
28
|
+
key: attribute_name,
|
29
|
+
method: attribute_name
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param [String] attribute_name
|
35
|
+
# @param [Hash] opts - attribute options
|
36
|
+
# @param [Proc] block - result is used as the attribute value
|
37
|
+
def attribute(attribute_name, opts = {}, &block)
|
38
|
+
_serialization_schema.add_attribute JsonModel::Attribute.new(
|
39
|
+
key: attribute_name,
|
40
|
+
method: block,
|
41
|
+
opts: opts
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param [String] attribute_name
|
46
|
+
# @param [Hash] opts - attribute options
|
47
|
+
def has_one(attribute_name, opts = {})
|
48
|
+
raise ArgumentError, "Serializer is not provided" unless opts[:serializer]
|
49
|
+
|
50
|
+
serialization_schema = opts.delete(:serializer)._serialization_schema
|
51
|
+
_serialization_schema.add_attribute JsonModel::HasOneRelationship.new(
|
52
|
+
key: attribute_name,
|
53
|
+
method: attribute_name,
|
54
|
+
opts: opts,
|
55
|
+
serialization_schema: serialization_schema
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
alias_method :belongs_to, :has_one
|
60
|
+
|
61
|
+
# @param [String] attribute_name
|
62
|
+
# @param [Hash] opts - attribute options
|
63
|
+
def has_many(attribute_name, opts = {})
|
64
|
+
raise ArgumentError, "Serializer is not provided" unless opts[:serializer]
|
65
|
+
|
66
|
+
serialization_schema = opts.delete(:serializer)._serialization_schema
|
67
|
+
_serialization_schema.add_attribute JsonModel::HasManyRelationship.new(
|
68
|
+
key: attribute_name,
|
69
|
+
method: attribute_name,
|
70
|
+
opts: opts,
|
71
|
+
serialization_schema: serialization_schema
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
75
|
+
# @param [String] attribute_name
|
76
|
+
# @param [Hash] opts - attribute options
|
77
|
+
def list(attribute_name, opts = {})
|
78
|
+
raise ArgumentError, "Serializer is not provided" unless opts[:serializer]
|
79
|
+
|
80
|
+
serialization_schema = opts.delete(:serializer)._serialization_schema
|
81
|
+
_serialization_schema.add_attribute JsonModel::Array.new(
|
82
|
+
key: attribute_name,
|
83
|
+
method: attribute_name,
|
84
|
+
opts: opts,
|
85
|
+
serialization_schema: serialization_schema
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
89
|
+
# @param [String] root_key - a key under which serialization result is nested
|
90
|
+
def root(root_key)
|
91
|
+
self._root = root_key
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
module Serialization
|
96
|
+
attr_accessor :resource, :params
|
97
|
+
|
98
|
+
def initialize(resource, params = {})
|
99
|
+
@resource = resource
|
100
|
+
@params = (params || {}).symbolize_keys
|
101
|
+
@params[:self] = self
|
102
|
+
|
103
|
+
if @params[:include]
|
104
|
+
if @params[:include].empty?
|
105
|
+
@params.delete(:include)
|
106
|
+
else
|
107
|
+
@params[:include] = @params[:include].map(&:to_sym)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def serializable_hash
|
113
|
+
meta = params.delete(:meta)
|
114
|
+
res = schema.serialize(resource, params)
|
115
|
+
|
116
|
+
root = (_root || params.delete(:root))
|
117
|
+
|
118
|
+
if root && root.size > 0
|
119
|
+
res = { root => res }
|
120
|
+
end
|
121
|
+
|
122
|
+
if res.is_a?(Hash) && meta
|
123
|
+
res[:meta] = meta
|
124
|
+
end
|
125
|
+
|
126
|
+
res
|
127
|
+
end
|
128
|
+
|
129
|
+
def serialized_json
|
130
|
+
FastSerializer.config.coder.dump(serializable_hash)
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def schema
|
136
|
+
is_collection = resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
|
137
|
+
|
138
|
+
if is_collection
|
139
|
+
JsonModel::Array.new(serialization_schema: _serialization_schema)
|
140
|
+
else
|
141
|
+
_serialization_schema
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
include SchemaInterface
|
147
|
+
include Serialization
|
148
|
+
|
149
|
+
module Mixin
|
150
|
+
def _serialization_schema
|
151
|
+
self.class._serialization_schema
|
152
|
+
end
|
153
|
+
|
154
|
+
def _root
|
155
|
+
self.class._root
|
156
|
+
end
|
157
|
+
|
158
|
+
def self.included(base)
|
159
|
+
base.include Serialization
|
160
|
+
base.extend InheritanceSupport
|
161
|
+
base.extend SchemaInterface
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
metadata
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fast_serializer_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evgeny Stepanov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faker
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: active_model_serializers
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.10.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.10.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: factory_bot
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rspec-benchmark
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: fast_serializer is a lightweight ruby objects serializer.
|
140
|
+
email:
|
141
|
+
- estepnv@icloud.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".travis.yml"
|
148
|
+
- CHANGELOG.md
|
149
|
+
- CODE_OF_CONDUCT.md
|
150
|
+
- Gemfile
|
151
|
+
- Gemfile.lock
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.md
|
154
|
+
- Rakefile
|
155
|
+
- bin/console
|
156
|
+
- bin/setup
|
157
|
+
- fast_serializer.gemspec
|
158
|
+
- lib/fast_serializer.rb
|
159
|
+
- lib/fast_serializer/configuration.rb
|
160
|
+
- lib/fast_serializer/json_model.rb
|
161
|
+
- lib/fast_serializer/json_model/array.rb
|
162
|
+
- lib/fast_serializer/json_model/attribute.rb
|
163
|
+
- lib/fast_serializer/json_model/has_many_relationship.rb
|
164
|
+
- lib/fast_serializer/json_model/has_one_relationship.rb
|
165
|
+
- lib/fast_serializer/json_model/node.rb
|
166
|
+
- lib/fast_serializer/json_model/object.rb
|
167
|
+
- lib/fast_serializer/json_model/relationship.rb
|
168
|
+
- lib/fast_serializer/schema.rb
|
169
|
+
- lib/fast_serializer/version.rb
|
170
|
+
homepage: https://github.com/estepnv/fast_serializer
|
171
|
+
licenses:
|
172
|
+
- MIT
|
173
|
+
metadata:
|
174
|
+
homepage_uri: https://github.com/estepnv/fast_serializer
|
175
|
+
source_code_uri: https://github.com/estepnv/fast_serializer
|
176
|
+
changelog_uri: https://github.com/estepnv/fast_serializer/CHANGELOG.md
|
177
|
+
post_install_message:
|
178
|
+
rdoc_options: []
|
179
|
+
require_paths:
|
180
|
+
- lib
|
181
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
182
|
+
requirements:
|
183
|
+
- - ">="
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: '0'
|
186
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
requirements: []
|
192
|
+
rubygems_version: 3.0.4
|
193
|
+
signing_key:
|
194
|
+
specification_version: 4
|
195
|
+
summary: Yet another serializer gem, but fast
|
196
|
+
test_files: []
|