transmutation 0.2.1 → 0.2.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +152 -10
- data/lib/transmutation/version.rb +1 -1
- data/transmutation.gemspec +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93679644e2df5418371d258fb0ede26d51bc9e29c4f835990ce5fa52f38dface
|
4
|
+
data.tar.gz: 64c956a59e385145d1870e949eadbe078db170e950ef711f1c4e46e735a8462c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 704c1832843d5d5d19a8ec26c672ed9d5f7cae9716f789f6622d0f3bf38aefb97d7ca9a7290ef954e7dc47824f1173e4c823b52272f4f3363b53e14c61f212c1
|
7
|
+
data.tar.gz: 3eaaf2c39d9c3e6f3ac448ce529932751b23edd43675e4eb04623e985e69ef53f20d95e5862d0e97d543919f690d3f8eff96ccc90d83e7ff43002c0ffe24c0ae
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,19 +2,29 @@
|
|
2
2
|
|
3
3
|
Transmutation is a Ruby gem that provides a simple way to serialize Ruby objects into JSON.
|
4
4
|
|
5
|
-
It takes inspiration from the [Active Model Serializers](https://github.com/rails-api/active_model_serializers) gem, but strips away adapters.
|
5
|
+
It also adds an opinionated way to automatically find and use serializer classes based on the object's class name and the caller's namespace - it takes inspiration from the [Active Model Serializers](https://github.com/rails-api/active_model_serializers) gem, but strips away adapters.
|
6
6
|
|
7
7
|
It aims to be a performant and elegant solution for serializing Ruby objects into JSON, with a touch of opinionated "magic" :sparkles:.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
|
-
Install the gem and add to
|
11
|
+
Install the gem and add to your application's Gemfile by executing:
|
12
12
|
|
13
|
-
|
13
|
+
```bash
|
14
|
+
bundle add transmutation
|
15
|
+
```
|
16
|
+
|
17
|
+
or manually add the following to your Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem "transmutation"
|
21
|
+
```
|
14
22
|
|
15
23
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
24
|
|
17
|
-
|
25
|
+
```bash
|
26
|
+
gem install transmutation
|
27
|
+
```
|
18
28
|
|
19
29
|
## Usage
|
20
30
|
|
@@ -48,13 +58,16 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
48
58
|
|
49
59
|
As long as your object responds to the attributes defined in the serializer, it can be serialized.
|
50
60
|
|
51
|
-
|
61
|
+
<details>
|
62
|
+
<summary>Struct</summary>
|
52
63
|
|
53
64
|
```ruby
|
54
65
|
User = Struct.new(:id, :name, :email)
|
55
66
|
```
|
67
|
+
</details>
|
56
68
|
|
57
|
-
|
69
|
+
<details>
|
70
|
+
<summary>Class</summary>
|
58
71
|
|
59
72
|
```ruby
|
60
73
|
class User
|
@@ -67,8 +80,10 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
67
80
|
end
|
68
81
|
end
|
69
82
|
```
|
83
|
+
</details>
|
70
84
|
|
71
|
-
|
85
|
+
<details>
|
86
|
+
<summary>ActiveRecord</summary>
|
72
87
|
|
73
88
|
```ruby
|
74
89
|
# == Schema Information
|
@@ -81,15 +96,142 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
81
96
|
class User < ApplicationRecord
|
82
97
|
end
|
83
98
|
```
|
99
|
+
</details>
|
100
|
+
|
101
|
+
### The `#serialize` method
|
102
|
+
|
103
|
+
When you include the `Transmutation::Serialization` module in your class, you can use the `#serialize` method to serialize an object.
|
104
|
+
|
105
|
+
It will attempt to find a serializer class based on the object's class name along with the caller's namespace.
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
include Transmutation::Serialization
|
109
|
+
|
110
|
+
serialize(User.new) # => UserSerializer.new(User.new)
|
111
|
+
```
|
112
|
+
|
113
|
+
If no serializer class is found, it will return the object as is.
|
114
|
+
|
115
|
+
### With Ruby on Rails
|
116
|
+
|
117
|
+
When then `Transmutation::Serialization` module is included in a Rails controller, it also extends your `render` calls.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
class Api::V1::UsersController < ApplicationController
|
121
|
+
include Transmutation::Serialization
|
122
|
+
|
123
|
+
def show
|
124
|
+
user = User.find(params[:id])
|
84
125
|
|
85
|
-
|
126
|
+
render json: user
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
This will attempt to bubble up the controller namespaces to find a defined serializer class:
|
132
|
+
|
133
|
+
- `Api::V1::UserSerializer`
|
134
|
+
- `Api::UserSerializer`
|
135
|
+
- `UserSerializer`
|
136
|
+
|
137
|
+
This calls the `#serialize` method under the hood.
|
138
|
+
|
139
|
+
If no serializer class is found, it will fall back to the default behavior of rendering the object as JSON.
|
140
|
+
|
141
|
+
You can disable this behaviour by passing `serialize: false` to the `render` method.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
render json: user, serialize: false # => user.to_json
|
145
|
+
```
|
146
|
+
|
147
|
+
## Configuration
|
148
|
+
|
149
|
+
You can override the serialization lookup by passing the following options:
|
150
|
+
|
151
|
+
- `namespace`: The namespace to use when looking up the serializer class.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
render json: user, namespace: "V1" # => Api::V1::V1::UserSerializer
|
155
|
+
```
|
156
|
+
|
157
|
+
To prevent caller namespaces from being appended to the provided namespace, prefix the namespace with `::`.
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
render json: user, namespace: "::V1" # => V1::UserSerializer
|
161
|
+
```
|
162
|
+
|
163
|
+
The `namespace` key is forwarded to the `#serialize` method.
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
render json: user, namespace: "V1" # => serialize(user, namespace: "V1")
|
167
|
+
```
|
168
|
+
|
169
|
+
- `serializer`: The serializer class to use.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
render json: user, serializer: "SuperUserSerializer" # => Api::V1::SuperUserSerializer
|
173
|
+
```
|
174
|
+
|
175
|
+
To prevent all namespaces from being appended to the serializer class, prefix the serializer class with `::`.
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
render json: user, serializer: "::SuperUserSerializer" # => SuperUserSerializer
|
179
|
+
```
|
180
|
+
|
181
|
+
The `serializer` key is forwarded to the `#serialize` method.
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
render json: user, serializer: "SuperUserSerializer" # => serialize(user, serializer: "SuperUserSerializer")
|
185
|
+
```
|
186
|
+
|
187
|
+
## Opinionated Architecture
|
188
|
+
|
189
|
+
If you follow the pattern outlined below, you can take full advantage of the automatic serializer lookup.
|
190
|
+
|
191
|
+
### File Structure
|
192
|
+
|
193
|
+
```
|
194
|
+
.
|
195
|
+
└── app/
|
196
|
+
├── controllers/
|
197
|
+
│ └── api/
|
198
|
+
│ ├── v1/
|
199
|
+
│ │ └── users_controller.rb
|
200
|
+
│ └── v2
|
201
|
+
│ └── users_controller.rb
|
202
|
+
├── models/
|
203
|
+
│ └── user.rb
|
204
|
+
└── serializers/
|
205
|
+
└── api/
|
206
|
+
├── v1/
|
207
|
+
│ └── user_serializer.rb
|
208
|
+
├── v2/
|
209
|
+
│ └── user_serializer.rb
|
210
|
+
└── user_serializer.rb
|
211
|
+
```
|
212
|
+
|
213
|
+
### Serializers
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
class Api::UserSerializer < Transmutation::Serializer
|
217
|
+
attributes :id, :name, :email
|
218
|
+
end
|
219
|
+
|
220
|
+
class Api::V1::UserSerializer < Api::UserSerializer
|
221
|
+
attributes :phone # Added in V1
|
222
|
+
end
|
223
|
+
|
224
|
+
class Api::V2::UserSerializer < Api::UserSerializer
|
225
|
+
attributes :avatar # Added in V2
|
226
|
+
end
|
227
|
+
```
|
228
|
+
|
229
|
+
To remove attributes, it is recommended to redefine all attributes and start anew. This acts as a reset and makes serializer inheritance much easier to follow.
|
86
230
|
|
87
231
|
## Development
|
88
232
|
|
89
233
|
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.
|
90
234
|
|
91
|
-
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
92
|
-
|
93
235
|
## Contributing
|
94
236
|
|
95
237
|
Bug reports and pull requests are welcome on GitHub at https://github.com/spellbook-technology/transmutation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/spellbook-technology/transmutation/blob/main/CODE_OF_CONDUCT.md).
|
data/transmutation.gemspec
CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
17
17
|
spec.metadata["source_code_uri"] = "https://github.com/spellbook-technology/transmutation"
|
18
18
|
spec.metadata["changelog_uri"] = "https://github.com/spellbook-technology/transmutation/CHANGELOG.md"
|
19
|
+
spec.metadata["documentation_uri"] = "https://rubydoc.info/gems/transmutation"
|
19
20
|
|
20
21
|
# Specify which files should be added to the gem when it is released.
|
21
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transmutation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nitemaeric
|
@@ -62,6 +62,7 @@ metadata:
|
|
62
62
|
homepage_uri: https://github.com/spellbook-technology/transmutation
|
63
63
|
source_code_uri: https://github.com/spellbook-technology/transmutation
|
64
64
|
changelog_uri: https://github.com/spellbook-technology/transmutation/CHANGELOG.md
|
65
|
+
documentation_uri: https://rubydoc.info/gems/transmutation
|
65
66
|
post_install_message:
|
66
67
|
rdoc_options: []
|
67
68
|
require_paths:
|