seri 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/.circleci/config.yml +55 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +182 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/serializer/group_serializer.rb +20 -0
- data/lib/serializer/version.rb +3 -0
- data/lib/serializer.rb +69 -0
- data/seri.gemspec +32 -0
- metadata +130 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 235047fde67dba9419cb66b80ae8bf6ab57f167dd56221b6deaace66967d2997
|
|
4
|
+
data.tar.gz: a2a7b26b30531f9633feec79dde2ae10f64aad7eb49926078d0b925fb5d28a61
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0d86a1667787e7e807d87675196b254bfd686e18da9e9cba6636c0a6c6345771295edab70645456c70e80418c184dc0570ab2095c8cb57d52b3af91e4b13a367
|
|
7
|
+
data.tar.gz: d9e9fb1aed4e27f70674e6795035b36428f5a6ab5ac15341fff02a883a0db25a8a584ee6243f192d913cd1b3515f867761361a44bf20a39e0579c703c0fd8ab8
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
|
2
|
+
#
|
|
3
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
|
4
|
+
#
|
|
5
|
+
version: 2
|
|
6
|
+
jobs:
|
|
7
|
+
build:
|
|
8
|
+
docker:
|
|
9
|
+
# specify the version you desire here
|
|
10
|
+
- image: circleci/ruby:2.4.1-node-browsers
|
|
11
|
+
|
|
12
|
+
# Specify service dependencies here if necessary
|
|
13
|
+
# CircleCI maintains a library of pre-built images
|
|
14
|
+
# documented at https://circleci.com/docs/2.0/circleci-images/
|
|
15
|
+
# - image: circleci/postgres:9.4
|
|
16
|
+
|
|
17
|
+
working_directory: ~/repo
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- checkout
|
|
21
|
+
|
|
22
|
+
# Download and cache dependencies
|
|
23
|
+
- restore_cache:
|
|
24
|
+
keys:
|
|
25
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
|
26
|
+
# fallback to using the latest cache if no exact match is found
|
|
27
|
+
- v1-dependencies-
|
|
28
|
+
|
|
29
|
+
- run:
|
|
30
|
+
name: install dependencies
|
|
31
|
+
command: |
|
|
32
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
|
33
|
+
|
|
34
|
+
- save_cache:
|
|
35
|
+
paths:
|
|
36
|
+
- ./vendor/bundle
|
|
37
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
|
38
|
+
|
|
39
|
+
# run tests!
|
|
40
|
+
- run:
|
|
41
|
+
name: run tests
|
|
42
|
+
command: |
|
|
43
|
+
mkdir /tmp/test-results
|
|
44
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
|
|
45
|
+
|
|
46
|
+
bundle exec rspec --format progress \
|
|
47
|
+
--out /tmp/test-results/rspec.xml \
|
|
48
|
+
--format progress \
|
|
49
|
+
$TEST_FILES
|
|
50
|
+
|
|
51
|
+
# collect reports
|
|
52
|
+
- store_test_results:
|
|
53
|
+
path: /tmp/test-results
|
|
54
|
+
- store_artifacts:
|
|
55
|
+
path: /tmp/test-results
|
data/.gitignore
ADDED
data/.rspec
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 gerard@wetransfer.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,41 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
serializer (0.1.0)
|
|
5
|
+
appsignal (~> 2.7)
|
|
6
|
+
oj (~> 3.7)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
appsignal (2.7.2)
|
|
12
|
+
rack
|
|
13
|
+
diff-lcs (1.3)
|
|
14
|
+
oj (3.7.4)
|
|
15
|
+
rack (2.0.6)
|
|
16
|
+
rake (10.5.0)
|
|
17
|
+
rspec (3.8.0)
|
|
18
|
+
rspec-core (~> 3.8.0)
|
|
19
|
+
rspec-expectations (~> 3.8.0)
|
|
20
|
+
rspec-mocks (~> 3.8.0)
|
|
21
|
+
rspec-core (3.8.0)
|
|
22
|
+
rspec-support (~> 3.8.0)
|
|
23
|
+
rspec-expectations (3.8.2)
|
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
25
|
+
rspec-support (~> 3.8.0)
|
|
26
|
+
rspec-mocks (3.8.0)
|
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
28
|
+
rspec-support (~> 3.8.0)
|
|
29
|
+
rspec-support (3.8.0)
|
|
30
|
+
|
|
31
|
+
PLATFORMS
|
|
32
|
+
ruby
|
|
33
|
+
|
|
34
|
+
DEPENDENCIES
|
|
35
|
+
bundler (~> 1.16)
|
|
36
|
+
rake (~> 10.0)
|
|
37
|
+
rspec (~> 3.0)
|
|
38
|
+
serializer!
|
|
39
|
+
|
|
40
|
+
BUNDLED WITH
|
|
41
|
+
1.16.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 grdw
|
|
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,182 @@
|
|
|
1
|
+
# Seri
|
|
2
|
+
|
|
3
|
+
[](https://circleci.com/gh/grdw/serializer)
|
|
4
|
+
|
|
5
|
+
A basic replacement for gems like `active_model_serializers`. Can turn any
|
|
6
|
+
basic Ruby object into a Hash or JSON string with features like:
|
|
7
|
+
|
|
8
|
+
- Aliasing attribute keys
|
|
9
|
+
- Overriding attributes
|
|
10
|
+
- Setting static values
|
|
11
|
+
- Creating conditional attributes
|
|
12
|
+
|
|
13
|
+
See [usage](#usage) for more details.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Add this line to your application's Gemfile:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
gem 'seri'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
And then execute:
|
|
24
|
+
|
|
25
|
+
$ bundle
|
|
26
|
+
|
|
27
|
+
Or install it yourself as:
|
|
28
|
+
|
|
29
|
+
$ gem install seri
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
A serializer can be used as such:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
# example class:
|
|
37
|
+
class Car
|
|
38
|
+
attr_accessor :mileage
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# example serializer:
|
|
42
|
+
class CarSerializer < Serializer
|
|
43
|
+
attribute :mileage
|
|
44
|
+
attribute :brand
|
|
45
|
+
attribute :mileage_alias, from: :mileage
|
|
46
|
+
attribute :honk, static_value: 'honk honk'
|
|
47
|
+
attribute :some_method
|
|
48
|
+
attribute :some_conditional_method, condition: :a_condition
|
|
49
|
+
attribute :some_other_conditional_method, condition: :b_condition
|
|
50
|
+
|
|
51
|
+
def some_method
|
|
52
|
+
object.mileage * 25
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def brand
|
|
56
|
+
'mercedes'
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def some_conditional_method
|
|
60
|
+
'visible condition'
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def a_condition
|
|
64
|
+
true
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def some_other_conditional_method
|
|
68
|
+
'non visible condition'
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def b_condition
|
|
72
|
+
!a_condition
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# example implementation:
|
|
77
|
+
car = Car.new
|
|
78
|
+
car.mileage = 25
|
|
79
|
+
|
|
80
|
+
serializer = CarSerializer.new(car)
|
|
81
|
+
serializer.to_json
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Result from `#to_json`:
|
|
85
|
+
|
|
86
|
+
```json
|
|
87
|
+
{
|
|
88
|
+
"mileage": 25,
|
|
89
|
+
"brand": "mercedes",
|
|
90
|
+
"mileage_alias": 25,
|
|
91
|
+
"honk": "honk_honk",
|
|
92
|
+
"some_method": "625",
|
|
93
|
+
"some_conditional_method": "visible_condition"
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
In turn there's also a `GroupSerializer` available which can take a group of
|
|
98
|
+
cars and turn them into a serialized Array. If we extend the example from
|
|
99
|
+
earlier we can do:
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
# example:
|
|
103
|
+
cars = [car, car]
|
|
104
|
+
group_serializer = GroupSerializer.new(cars, serializer: CarSerializer)
|
|
105
|
+
|
|
106
|
+
group_serializer.to_json
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Result from `#to_json`:
|
|
110
|
+
|
|
111
|
+
```json
|
|
112
|
+
[
|
|
113
|
+
{
|
|
114
|
+
"mileage": 25,
|
|
115
|
+
"brand": "mercedes",
|
|
116
|
+
"mileage_alias": 25,
|
|
117
|
+
"honk": "honk_honk",
|
|
118
|
+
"some_method": "625",
|
|
119
|
+
"some_conditional_method": "visible_condition"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"mileage": 25,
|
|
123
|
+
"brand": "mercedes",
|
|
124
|
+
"mileage_alias": 25,
|
|
125
|
+
"honk": "honk_honk",
|
|
126
|
+
"some_method": "625",
|
|
127
|
+
"some_conditional_method": "visible_condition"
|
|
128
|
+
}
|
|
129
|
+
]
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Q&A
|
|
133
|
+
|
|
134
|
+
**Q: This looks cool and all but how do I do a `has_many`?**
|
|
135
|
+
|
|
136
|
+
Answer:
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
class A
|
|
140
|
+
attr_accessor :some_amazing_attribute
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
class B
|
|
144
|
+
attr_accessor :some_attribute
|
|
145
|
+
|
|
146
|
+
def aaa
|
|
147
|
+
[A.new, A.new, A.new]
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
class ASerializer < Serializer
|
|
152
|
+
attribute :some_amazing_attribute
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
class BSerializer < Serializer
|
|
156
|
+
attribute :lots_of_a, from: :aaa, serializer: ASerializer
|
|
157
|
+
end
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The result from `BSerializer.new(b)#to_json`:
|
|
161
|
+
|
|
162
|
+
```json
|
|
163
|
+
{
|
|
164
|
+
"lots_of_a": [
|
|
165
|
+
{ "some_amazing_attribute": null },
|
|
166
|
+
{ "some_amazing_attribute": null },
|
|
167
|
+
{ "some_amazing_attribute": null }
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Contributing
|
|
173
|
+
|
|
174
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/grdw/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.
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
179
|
+
|
|
180
|
+
## Code of Conduct
|
|
181
|
+
|
|
182
|
+
Everyone interacting in the Serializer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/grdw/seri/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "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
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class GroupSerializer
|
|
2
|
+
def initialize(objects, serializer: nil)
|
|
3
|
+
raise ArgumentError, 'serializer needs to be specified' if serializer.nil?
|
|
4
|
+
|
|
5
|
+
@objects = objects
|
|
6
|
+
@serializer = serializer
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_json(*)
|
|
10
|
+
Appsignal.instrument(
|
|
11
|
+
'json.serialize',
|
|
12
|
+
'Group serializer',
|
|
13
|
+
@serializer.to_s
|
|
14
|
+
) do
|
|
15
|
+
result = @objects.map { |object| @serializer.new(object).to_h }
|
|
16
|
+
|
|
17
|
+
Oj.dump(result, mode: :json)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
data/lib/serializer.rb
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'serializer/version'
|
|
2
|
+
require 'serializer/group_serializer'
|
|
3
|
+
|
|
4
|
+
class Serializer
|
|
5
|
+
class SerializerError < StandardError; end
|
|
6
|
+
|
|
7
|
+
Attribute = Struct.new(:key, :condition, :from, :serializer, :options)
|
|
8
|
+
|
|
9
|
+
def self.attributes
|
|
10
|
+
@attributes ||= []
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.attribute(key, condition: nil, from: nil, serializer: nil, **options)
|
|
14
|
+
attributes.push(Attribute.new(key, condition, from, serializer, options))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
attr_accessor :object, :scope
|
|
18
|
+
|
|
19
|
+
def initialize(object, scope: {})
|
|
20
|
+
@object = object
|
|
21
|
+
@scope = scope
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Loops over all attributes and skips if a condition is defined and falsey
|
|
25
|
+
def to_h
|
|
26
|
+
self.class.attributes.each_with_object({}) do |attribute, obj|
|
|
27
|
+
next if attribute.condition && !public_send(attribute.condition)
|
|
28
|
+
|
|
29
|
+
extraction_key = attribute.from || attribute.key
|
|
30
|
+
|
|
31
|
+
# Fetches a value from an attribute by checking if there's a ..
|
|
32
|
+
# .. static value set, or a ..
|
|
33
|
+
# .. method defined in the serializer, or a ..
|
|
34
|
+
# .. method/attribute defined in the object or ..
|
|
35
|
+
# .. it raises an error
|
|
36
|
+
value = if attribute.options.has_key?(:static_value)
|
|
37
|
+
attribute.options.fetch(:static_value)
|
|
38
|
+
elsif respond_to?(extraction_key)
|
|
39
|
+
public_send(extraction_key)
|
|
40
|
+
elsif object.respond_to?(extraction_key) && attribute.serializer
|
|
41
|
+
value = object.public_send(extraction_key)
|
|
42
|
+
|
|
43
|
+
serialize_value(value, attribute.serializer)
|
|
44
|
+
elsif object.respond_to?(extraction_key)
|
|
45
|
+
object.public_send(extraction_key)
|
|
46
|
+
else
|
|
47
|
+
raise SerializerError, "unknown attribute '#{extraction_key}'"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
obj[attribute.key] = value
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def to_json(*)
|
|
55
|
+
Appsignal.instrument('json.serialize', 'Serializer', self.class.to_s) do
|
|
56
|
+
Oj.dump(to_h, mode: :json)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def serialize_value(value, serializer)
|
|
63
|
+
if value.is_a?(Array)
|
|
64
|
+
value.map { |v| serializer.new(v).to_h }
|
|
65
|
+
else
|
|
66
|
+
serializer.new(value).to_h
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
data/seri.gemspec
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "serializer/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "seri"
|
|
8
|
+
spec.version = Serializer::VERSION
|
|
9
|
+
spec.authors = ["grdw"]
|
|
10
|
+
spec.email = ["gerard@wetransfer.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{A basic serializer}
|
|
13
|
+
spec.description = %q{A basic serializer which can suite as a replacement to other gems}
|
|
14
|
+
spec.homepage = "https://github.com/grdw/seri"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
21
|
+
end
|
|
22
|
+
spec.bindir = "exe"
|
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
24
|
+
spec.require_paths = ["lib"]
|
|
25
|
+
|
|
26
|
+
spec.add_dependency "oj", "~> 3.7"
|
|
27
|
+
spec.add_dependency "appsignal", "~> 2.7"
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: seri
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- grdw
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-11-30 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: oj
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.7'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.7'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: appsignal
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '2.7'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '2.7'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.16'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.16'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.0'
|
|
83
|
+
description: A basic serializer which can suite as a replacement to other gems
|
|
84
|
+
email:
|
|
85
|
+
- gerard@wetransfer.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- ".circleci/config.yml"
|
|
91
|
+
- ".gitignore"
|
|
92
|
+
- ".rspec"
|
|
93
|
+
- ".travis.yml"
|
|
94
|
+
- CODE_OF_CONDUCT.md
|
|
95
|
+
- Gemfile
|
|
96
|
+
- Gemfile.lock
|
|
97
|
+
- LICENSE.txt
|
|
98
|
+
- README.md
|
|
99
|
+
- Rakefile
|
|
100
|
+
- bin/console
|
|
101
|
+
- bin/setup
|
|
102
|
+
- lib/serializer.rb
|
|
103
|
+
- lib/serializer/group_serializer.rb
|
|
104
|
+
- lib/serializer/version.rb
|
|
105
|
+
- seri.gemspec
|
|
106
|
+
homepage: https://github.com/grdw/seri
|
|
107
|
+
licenses:
|
|
108
|
+
- MIT
|
|
109
|
+
metadata: {}
|
|
110
|
+
post_install_message:
|
|
111
|
+
rdoc_options: []
|
|
112
|
+
require_paths:
|
|
113
|
+
- lib
|
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
|
+
requirements:
|
|
116
|
+
- - ">="
|
|
117
|
+
- !ruby/object:Gem::Version
|
|
118
|
+
version: '0'
|
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - ">="
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: '0'
|
|
124
|
+
requirements: []
|
|
125
|
+
rubyforge_project:
|
|
126
|
+
rubygems_version: 2.7.3
|
|
127
|
+
signing_key:
|
|
128
|
+
specification_version: 4
|
|
129
|
+
summary: A basic serializer
|
|
130
|
+
test_files: []
|