transmutation 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfdba62ded204b2a1007b3f4194ada3d25cfdb1851d21a23d3d0d8baa4b69c78
4
- data.tar.gz: 97c1a41fb6a6550adce185f402a9d9db738cdc912bbd15a7ab13f133fa1a83e8
3
+ metadata.gz: c0c0f36f54d912c5198176956044bf36157135f0ca1a5230c09b892663e1fcb0
4
+ data.tar.gz: da166b70d0c35dc701d83f7016364d8c52cbb2aef9102045e8d6260235383485
5
5
  SHA512:
6
- metadata.gz: db4415229791b74647ffd4441d4b930f3ab98a2a7c9e1fb84de94537f6eec1a370116745860fe1bf2a98bb3b8dc6541d7c6ac5933779a022e97e96bbe0113ebc
7
- data.tar.gz: ea012d8cc51843103d938a1953619f33bc603a0a51b70787cbaebcc653bae9e83eded5a3807407ad143ffd50300f063ae4b15d254c03c079674a64701a9781fd
6
+ metadata.gz: 8357eb90811b77c0069a97393b091b81d6b95c80cf75b6d9d9d1bf7d5437f14ff9aaa8a35aadf32c4767238ec6da9a08d5e326d66a6e49fe4536c382dd3b0549
7
+ data.tar.gz: ecad287a5c638eb67c249723ef0aa36e4cc5cac5f9aa59a7428746e9a9f9bcc5edf12d271eb53b7aa6d2108f24e0d7f557e82c6d0261292a02f53d11e9aece09
@@ -18,13 +18,13 @@ module Transmutation
18
18
  # namespace: Api::V1::Admin::Detailed
19
19
  # serializer: Chat::User
20
20
  #
21
- # This method will attempt to find a serializer defined in the following order:
21
+ # # This method will attempt to find a serializer defined in the following order:
22
22
  #
23
- # - Api::V1::Admin::Detailed::Chat::UserSerializer
24
- # - Api::V1::Admin::Chat::UserSerializer
25
- # - Api::V1::Chat::UserSerializer
26
- # - Api::Chat::UserSerializer
27
- # - Chat::UserSerializer
23
+ # # - Api::V1::Admin::Detailed::Chat::UserSerializer
24
+ # # - Api::V1::Admin::Chat::UserSerializer
25
+ # # - Api::V1::Chat::UserSerializer
26
+ # # - Api::Chat::UserSerializer
27
+ # # - Chat::UserSerializer
28
28
  def serializer_for(object, serializer: nil)
29
29
  serializer_name = serializer_name_for(object, serializer:)
30
30
 
@@ -65,14 +65,10 @@ module Transmutation
65
65
  end
66
66
 
67
67
  def serializer_namespace
68
- return caller_namespace if @namespace.nil?
69
- return @namespace if @namespace.start_with?("::")
68
+ return @caller.namespace if @namespace.nil?
69
+ return @namespace if @namespace.start_with?("::")
70
70
 
71
- "#{caller_namespace}::#{@namespace}"
72
- end
73
-
74
- def caller_namespace
75
- @caller_namespace ||= @caller.class.name.split("::")[...-1].join("::")
71
+ "#{@caller.namespace}::#{@namespace}"
76
72
  end
77
73
 
78
74
  def constantize_serializer!(namespace, name, object:)
@@ -3,6 +3,7 @@
3
3
  module Transmutation
4
4
  module Serialization
5
5
  module Rendering
6
+ # Serializes the value of the `json` parameter before calling the existing render method.
6
7
  def render(json: nil, serialize: true, namespace: nil, serializer: nil, max_depth: 1, **args)
7
8
  return super(**args) unless json
8
9
  return super(**args, json:) unless serialize
@@ -10,21 +10,20 @@ module Transmutation
10
10
  # @param max_depth [Integer] The maximum depth of nested associations to serialize.
11
11
  #
12
12
  # @return [Transmutation::Serializer] The serialized object. This will respond to `#as_json` and `#to_json`.
13
- def serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: 1)
13
+ def serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: Transmutation.max_depth)
14
14
  if object.respond_to?(:map) && !object.respond_to?(:to_hash)
15
- return object.map do |item|
16
- serialize(item, namespace:, serializer:, depth:, max_depth:)
17
- end
15
+ return object.map { |item| serialize(item, namespace:, serializer:, depth:, max_depth:) }
18
16
  end
19
17
 
20
- lookup_serializer(object, namespace:, serializer:)
21
- .new(object, depth:, max_depth:)
18
+ lookup_serializer(object, namespace:, serializer:).new(object, depth:, max_depth:)
22
19
  end
23
20
 
24
21
  # Lookup the serializer for the given object.
25
22
  #
26
23
  # This calls {Transmutation::Serialization::Lookup#serializer_for} to find the serializer for the given object.
27
24
  #
25
+ # This also caches the result for future lookups.
26
+ #
28
27
  # @param object [Object] The object to lookup the serializer for.
29
28
  # @param namespace [String, Symbol, Module] The namespace to lookup the serializer in.
30
29
  # @param serializer [String, Symbol, Class] The serializer to use.
@@ -32,7 +31,23 @@ module Transmutation
32
31
  # @return [Class<Transmutation::Serializer>] The serializer for the given object.
33
32
  #
34
33
  def lookup_serializer(object, namespace: nil, serializer: nil)
35
- Lookup.new(self, namespace:).serializer_for(object, serializer:)
34
+ Serialization.cache[[self.namespace, object.class, namespace, serializer]] ||=
35
+ Lookup.new(self, namespace:).serializer_for(object, serializer:)
36
+ end
37
+
38
+ # @api private
39
+ def self.cache
40
+ @cache ||= {}
41
+ end
42
+
43
+ # Returns the namespace of this class.
44
+ #
45
+ # @example
46
+ # Api::V1::UsersController.namespace #=> "Api::V1"
47
+ #
48
+ # @return [String] The namespace of this class.
49
+ def namespace
50
+ @namespace ||= self.class.name.to_s[0, self.class.name.rindex("::") || 0]
36
51
  end
37
52
 
38
53
  private_class_method def self.included(base)
@@ -88,10 +88,6 @@ module Transmutation
88
88
  attributes_config[association_name] = { block:, association: true }
89
89
  end
90
90
 
91
- alias belongs_to association
92
- alias has_one association
93
- alias has_many association
94
-
95
91
  # Shorthand for defining multiple attributes
96
92
  #
97
93
  # @param attribute_names [Array<Symbol>] The names of the attributes to serialize
@@ -100,9 +96,9 @@ module Transmutation
100
96
  # class UserSerializer < Transmutation::Serializer
101
97
  # attributes :first_name, :last_name
102
98
  # end
103
- def attributes(*attribute_names)
99
+ def attributes(*attribute_names, **, &)
104
100
  attribute_names.each do |attribute_name|
105
- attribute(attribute_name)
101
+ attribute(attribute_name, **, &)
106
102
  end
107
103
  end
108
104
 
@@ -114,11 +110,15 @@ module Transmutation
114
110
  # class UserSerializer < Transmutation::Serializer
115
111
  # associations :posts, :comments
116
112
  # end
117
- def associations(*association_names)
113
+ def associations(*association_names, **, &)
118
114
  association_names.each do |association_name|
119
- association(association_name)
115
+ association(association_name, **, &)
120
116
  end
121
117
  end
118
+
119
+ alias belongs_to associations
120
+ alias has_one associations
121
+ alias has_many associations
122
122
  end
123
123
 
124
124
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Transmutation
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/transmutation.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "transmutation/class_attributes"
4
+
3
5
  # A performant and expressive solution for serializing Ruby objects into JSON, with a touch of opinionated "magic" ✨.
4
6
  #
5
7
  # @example Basic usage
@@ -38,12 +40,16 @@
38
40
  # end
39
41
  # end
40
42
  module Transmutation
43
+ extend ClassAttributes
44
+
45
+ class_attribute :max_depth, default: 1
46
+
41
47
  class Error < StandardError; end
42
48
  end
43
49
 
44
50
  require "zeitwerk"
45
- loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
46
- loader.ignore("#{__dir__}/transmutation/core_ext")
51
+
52
+ loader = Zeitwerk::Loader.for_gem
47
53
  loader.setup
48
54
 
49
55
  require "active_support"
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.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nitemaeric
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2025-04-11 00:00:00.000000000 Z
12
+ date: 2025-11-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -47,16 +47,6 @@ executables: []
47
47
  extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
- - ".rspec"
51
- - ".rubocop.yml"
52
- - ".rubocop_todo.yml"
53
- - ".ruby-version"
54
- - CHANGELOG.md
55
- - CODE_OF_CONDUCT.md
56
- - Gemfile
57
- - LICENSE
58
- - README.md
59
- - Rakefile
60
50
  - lib/transmutation.rb
61
51
  - lib/transmutation/class_attributes.rb
62
52
  - lib/transmutation/object_serializer.rb
@@ -66,7 +56,6 @@ files:
66
56
  - lib/transmutation/serialization/rendering.rb
67
57
  - lib/transmutation/serializer.rb
68
58
  - lib/transmutation/version.rb
69
- - sig/transmutation.rbs
70
59
  homepage: https://github.com/spellbook-technology/transmutation
71
60
  licenses:
72
61
  - MIT
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,26 +0,0 @@
1
- inherit_from: .rubocop_todo.yml
2
-
3
- AllCops:
4
- TargetRubyVersion: 3.2.5
5
- NewCops: disable
6
- SuggestExtensions: false
7
-
8
- Style/StringLiterals:
9
- Enabled: true
10
- EnforcedStyle: double_quotes
11
-
12
- Style/StringLiteralsInInterpolation:
13
- Enabled: true
14
- EnforcedStyle: double_quotes
15
-
16
- Layout/LineLength:
17
- Max: 120
18
-
19
- Metrics/AbcSize:
20
- Max: 18
21
-
22
- Metrics/ParameterLists:
23
- Enabled: true
24
- CountKeywordArgs: false
25
-
26
- require: rubocop-rspec
data/.rubocop_todo.yml DELETED
@@ -1,16 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2024-06-05 00:21:36 UTC using RuboCop version 1.63.4.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 2
10
- # Configuration parameters: AllowedConstants.
11
- Style/Documentation:
12
- Exclude:
13
- - 'spec/**/*'
14
- - 'test/**/*'
15
- - 'lib/transmutation/serialization.rb'
16
- - 'lib/transmutation/serialization/rendering.rb'
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 3.2.5
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2024-04-30
4
-
5
- - Initial release
data/CODE_OF_CONDUCT.md DELETED
@@ -1,84 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
-
7
- We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
-
9
- ## Our Standards
10
-
11
- Examples of behavior that contributes to a positive environment for our community include:
12
-
13
- * Demonstrating empathy and kindness toward other people
14
- * Being respectful of differing opinions, viewpoints, and experiences
15
- * Giving and gracefully accepting constructive feedback
16
- * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
- * Focusing on what is best not just for us as individuals, but for the overall community
18
-
19
- Examples of unacceptable behavior include:
20
-
21
- * The use of sexualized language or imagery, and sexual attention or
22
- advances of any kind
23
- * Trolling, insulting or derogatory comments, and personal or political attacks
24
- * Public or private harassment
25
- * Publishing others' private information, such as a physical or email
26
- address, without their explicit permission
27
- * Other conduct which could reasonably be considered inappropriate in a
28
- professional setting
29
-
30
- ## Enforcement Responsibilities
31
-
32
- Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
-
34
- Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
-
36
- ## Scope
37
-
38
- This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
-
40
- ## Enforcement
41
-
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at borrabeer@outlook.com. All complaints will be reviewed and investigated promptly and fairly.
43
-
44
- All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
-
46
- ## Enforcement Guidelines
47
-
48
- Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
-
50
- ### 1. Correction
51
-
52
- **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
-
54
- **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
-
56
- ### 2. Warning
57
-
58
- **Community Impact**: A violation through a single incident or series of actions.
59
-
60
- **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
-
62
- ### 3. Temporary Ban
63
-
64
- **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
-
66
- **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
-
68
- ### 4. Permanent Ban
69
-
70
- **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
-
72
- **Consequence**: A permanent ban from any sort of public interaction within the community.
73
-
74
- ## Attribution
75
-
76
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
- available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
-
79
- Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
-
81
- [homepage]: https://www.contributor-covenant.org
82
-
83
- For answers to common questions about this code of conduct, see the FAQ at
84
- https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile DELETED
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in transmutation.gemspec
6
- gemspec
7
-
8
- gem "rake", "~> 13.0"
9
-
10
- gem "rspec", "~> 3.0"
11
-
12
- gem "rubocop", "~> 1.21"
13
- gem "rubocop-rspec", require: false
14
-
15
- gem "simplecov"
16
- gem "simplecov-lcov"
17
- gem "undercover"
18
-
19
- gem "pry"
20
-
21
- gem "gem-release", require: false
data/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Spellbook Technology
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 all
13
- 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 THE
21
- SOFTWARE.
data/README.md DELETED
@@ -1,245 +0,0 @@
1
- # Transmutation
2
-
3
- Transmutation is a Ruby gem that provides a simple way to serialize Ruby objects into JSON.
4
-
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
-
7
- It aims to be a performant and elegant solution for serializing Ruby objects into JSON, with a touch of opinionated "magic" :sparkles:.
8
-
9
- ## Installation
10
-
11
- Install the gem and add to your application's Gemfile by executing:
12
-
13
- ```bash
14
- bundle add transmutation
15
- ```
16
-
17
- or manually add the following to your Gemfile:
18
-
19
- ```ruby
20
- gem "transmutation"
21
- ```
22
-
23
- If bundler is not being used to manage dependencies, install the gem by executing:
24
-
25
- ```bash
26
- gem install transmutation
27
- ```
28
-
29
- ## Usage
30
-
31
- ### Basic Usage
32
-
33
- - Define a serializer class that inherits from `Transmutation::Serializer` and define the attributes to be serialized.
34
-
35
- ```ruby
36
- class UserSerializer < Transmutation::Serializer
37
- attributes :id, :name, :email
38
- end
39
- ```
40
-
41
- - Serialize an object using the serializer class.
42
-
43
- ```ruby
44
- class User
45
- attr_reader :id, :name, :email
46
-
47
- def initialize(id:, name:, email:)
48
- @id = id
49
- @name = name
50
- @email = email
51
- end
52
- end
53
-
54
- user = User.new(id: 1, name: "John Doe", email: "john@example.com")
55
-
56
- UserSerializer.new(user).to_json # => "{\"id\":1,\"name\":\"John Doe\",\"email\":\"john@example.com\"}"
57
- ```
58
-
59
- As long as your object responds to the attributes defined in the serializer, it can be serialized.
60
-
61
- <details>
62
- <summary>Struct</summary>
63
-
64
- ```ruby
65
- User = Struct.new(:id, :name, :email)
66
- ```
67
- </details>
68
-
69
- <details>
70
- <summary>Class</summary>
71
-
72
- ```ruby
73
- class User
74
- attr_reader :id, :name, :email
75
-
76
- def initialize(id:, name:, email:)
77
- @id = id
78
- @name = name
79
- @email = email
80
- end
81
- end
82
- ```
83
- </details>
84
-
85
- <details>
86
- <summary>ActiveRecord</summary>
87
-
88
- ```ruby
89
- # == Schema Information
90
- #
91
- # Table name: users
92
- #
93
- # id :bigint
94
- # name :string
95
- # email :string
96
- class User < ApplicationRecord
97
- end
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])
125
-
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.
230
-
231
- ## Development
232
-
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.
234
-
235
- ## Contributing
236
-
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).
238
-
239
- ## License
240
-
241
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
242
-
243
- ## Code of Conduct
244
-
245
- Everyone interacting in the Transmutation project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/spellbook-technology/transmutation/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile DELETED
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new do |task|
11
- task.requires << "rubocop-rspec"
12
- end
13
-
14
- task default: %i[spec rubocop]
@@ -1,4 +0,0 @@
1
- module Transmutation
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end