alba_migration 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5d0d4f72523fe8a34222de3cfe8a36d66aac6c5435f5efa1838899d0c70dec70
4
+ data.tar.gz: 1a1c8a0d42928db2f3bda02ad2f0202153f2706b95ef3c84d56348e47c575800
5
+ SHA512:
6
+ metadata.gz: a6fb7aec6e909fdeeee455f04bf0a3fe8b4ae7596673d995f7288311fca94c19b5682260696d66b1b3a5b60ba2ca561d4b50eec30f6e59a01a0821a356629d79
7
+ data.tar.gz: 1157bfbcf57f3ef83d22aa2b1896c125c550b3744cab656312f1220d2083303ff16f84191b067afe310eda19182c874d20d1a9ed6e389965893976c3068f5780
@@ -0,0 +1,28 @@
1
+ FROM ruby:3.3-slim
2
+
3
+ # Install required packages
4
+ RUN apt-get update -qq && apt-get install -y \
5
+ build-essential \
6
+ git \
7
+ libyaml-dev \
8
+ && apt-get clean \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ # Set working directory
12
+ WORKDIR /app
13
+
14
+ # Copy Gemfile and gemspec
15
+ COPY Gemfile* ../alba_migration.gemspec ./
16
+ COPY lib/alba_migration/version.rb ./lib/alba_migration/version.rb
17
+
18
+ # Install gems
19
+ RUN bundle install
20
+
21
+ # Copy application code
22
+ COPY . .
23
+
24
+ # Set default directory for command execution
25
+ WORKDIR /app
26
+
27
+ # Default command
28
+ CMD ["bash"]
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "Alba Migration Development",
3
+ "build": {
4
+ "dockerfile": "Dockerfile",
5
+ "context": ".."
6
+ },
7
+ "customizations": {
8
+ "vscode": {
9
+ "extensions": [
10
+ "Shopify.ruby-extensions-pack"
11
+ ],
12
+ "settings": {
13
+ "rubyLsp.rubyVersionManager": {
14
+ "identifier": "auto"
15
+ },
16
+ "ruby.interpreter.commandPath": "/usr/local/bin/ruby",
17
+ "ruby.useLanguageServer": true,
18
+ "[ruby]": {
19
+ "editor.defaultFormatter": "Shopify.ruby-lsp"
20
+ },
21
+ "rubyLsp.formatter": "standard",
22
+ "rubyLsp.linters": [
23
+ "standard"
24
+ ],
25
+ "editor.formatOnSave": true,
26
+ "editor.tabSize": 2,
27
+ "editor.insertSpaces": true,
28
+ "editor.semanticHighlighting.enabled": true,
29
+ "editor.formatOnType": true,
30
+ "rubyLsp.enabledFeatures": {
31
+ "codeActions": true,
32
+ "diagnostics": true,
33
+ "documentHighlights": true,
34
+ "documentLink": true,
35
+ "documentSymbols": true,
36
+ "foldingRanges": true,
37
+ "formatting": true,
38
+ "hover": true,
39
+ "inlayHint": true,
40
+ "onTypeFormatting": true,
41
+ "selectionRanges": true,
42
+ "semanticHighlighting": true,
43
+ "completion": true
44
+ }
45
+ }
46
+ }
47
+ },
48
+ "features": {
49
+ "ghcr.io/devcontainers/features/git:1": {},
50
+ "ghcr.io/devcontainers/features/github-cli:1": {}
51
+ }
52
+ }
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # GHANGELOG
2
+
3
+ ## 0.0.1 (2025-05-19)
4
+
5
+ - Initial release
6
+ - Provides CLI tool to migrate Ruby code from ActiveModelSerializers (AMS) to Alba syntax
7
+ - Supports conversion of class definitions and attributes from AMS to Alba
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 ShoheiMitani
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,88 @@
1
+ # AlbaMigration
2
+
3
+ ## What is AlbaMigration?
4
+ AlbaMigration is a Ruby gem that helps you migrate your Ruby code from [ActiveModelSerializers (AMS)](https://github.com/rails-api/active_model_serializers) to [Alba](https://github.com/okuramasafumi/alba) syntax automatically. It provides a command-line tool to convert AMS serializer classes to Alba resource classes, making your migration process easier and less error-prone.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "alba_migration"
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```bash
17
+ bundle install
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### Command Line Tool
23
+
24
+ After installing the gem, you can use the CLI to migrate your Ruby files:
25
+
26
+ ```bash
27
+ alba_migration path/to/your/serializer.rb
28
+ ```
29
+
30
+ You can also use glob patterns to migrate multiple files at once:
31
+
32
+ ```bash
33
+ alba_migration app/serializers/**/*.rb
34
+ ```
35
+
36
+ If you are using Bundler:
37
+
38
+ ```bash
39
+ bundle exec alba_migration app/serializers/**/*.rb
40
+ ```
41
+
42
+ #### Example
43
+
44
+ **Before:**
45
+ ```ruby
46
+ class AttributesResource < ActiveModel::Serializer
47
+ attributes :id, :name
48
+ end
49
+ ```
50
+
51
+ **After:**
52
+ ```ruby
53
+ class AttributesResource
54
+ include Alba::Resource
55
+
56
+ attributes :id, :name
57
+ end
58
+ ```
59
+
60
+ ### Error Handling
61
+ - If no files match the given pattern, an error message will be shown and the process will exit with status 1.
62
+
63
+ ## Supported Syntax
64
+ Currently, AlbaMigration supports the following AMS syntax:
65
+ - `class ... < ActiveModel::Serializer` with `attributes ...` inside the class body
66
+ - Only the conversion of the class definition and `attributes` is supported at this time
67
+ - Other AMS features (e.g., associations, custom methods) are **not** yet supported
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/ShoheiMitani/alba_migration](https://github.com/ShoheiMitani/alba_migration).
72
+
73
+ To get started with development:
74
+
75
+ 1. Clone the repository
76
+ 2. Run `bin/setup` to install dependencies
77
+ 3. Run tests with `bin/rspec spec/`
78
+ 4. Check/fix code style with `bin/standardrb --fix`
79
+
80
+ If you find a bug, please [open an issue](https://github.com/ShoheiMitani/alba_migration/issues) with detailed steps to reproduce it.
81
+
82
+ ## License
83
+
84
+ This project is licensed under the MIT License. See [LICENSE.txt](LICENSE.txt) for details.
85
+
86
+ ## Code of Conduct
87
+
88
+ Everyone interacting in the AlbaMigration project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [Code of Conduct](CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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 "standard/rake"
9
+
10
+ task default: %i[spec standard]
data/Steepfile ADDED
@@ -0,0 +1,32 @@
1
+ # D = Steep::Diagnostic
2
+ #
3
+ # target :lib do
4
+ # signature "sig"
5
+ # ignore_signature "sig/test"
6
+ #
7
+ # check "lib" # Directory name
8
+ # check "path/to/source.rb" # File name
9
+ # check "app/models/**/*.rb" # Glob
10
+ # # ignore "lib/templates/*.rb"
11
+ #
12
+ # # library "pathname" # Standard libraries
13
+ # # library "strong_json" # Gems
14
+ #
15
+ # # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
16
+ # # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
17
+ # # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
18
+ # # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
19
+ # # configure_code_diagnostics do |hash| # You can setup everything yourself
20
+ # # hash[D::Ruby::NoMethod] = :information
21
+ # # end
22
+ # end
23
+
24
+ # target :test do
25
+ # unreferenced! # Skip type checking the `lib` code when types in `test` target is changed
26
+ # signature "sig/test" # Put RBS files for tests under `sig/test`
27
+ # check "test" # Type check Ruby scripts under `test`
28
+ #
29
+ # configure_code_diagnostics(D::Ruby.lenient) # Weak type checking for test code
30
+ #
31
+ # # library "pathname" # Standard libraries
32
+ # end
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "alba_migration"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/standardrb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ load Gem.bin_path("standard", "standardrb")
@@ -0,0 +1,293 @@
1
+ # Synvert Usage Guideline
2
+
3
+ This document explains how to use the Synvert CLI to automate migration from ActiveModelSerializers to Alba, based on the actual implementation and DSL of synvert-core v2.2.2.
4
+
5
+ ---
6
+
7
+ ## 1. Overview
8
+
9
+ Synvert is an automated Ruby code refactoring tool. You define code transformation rules, and Synvert rewrites code based on AST (Abstract Syntax Tree) analysis for syntactically correct transformations.
10
+
11
+ Synvert consists of two main gems:
12
+ - `synvert-core` (v2.2.2): Provides core features (AST analysis, code transformation engine)
13
+ - `synvert` (v1.10.4): Provides the CLI interface and runs predefined or custom snippets
14
+
15
+ This project uses the `synvert` CLI for code transformation.
16
+
17
+ ---
18
+
19
+ ## 2. Installation and Basic Usage
20
+
21
+ ### 2.1 Installation
22
+
23
+ ```bash
24
+ gem install synvert
25
+ ```
26
+
27
+ Or add to your Gemfile:
28
+
29
+ ```ruby
30
+ gem 'synvert'
31
+ ```
32
+
33
+ ### 2.2 Basic Usage
34
+
35
+ ```bash
36
+ # List available snippets
37
+ synvert -l
38
+
39
+ # Run a predefined snippet
40
+ synvert -r snippet_name
41
+
42
+ # Run a custom snippet file
43
+ synvert -s path/to/snippet.rb
44
+
45
+ # Run on a specific directory or file
46
+ synvert -s path/to/snippet.rb --path app/serializers
47
+
48
+ # Dry-run (no actual changes)
49
+ synvert -s path/to/snippet.rb --path app/serializers --dry-run
50
+ ```
51
+
52
+ ---
53
+
54
+ ## 3. Writing Custom Snippets
55
+
56
+ ### 3.1 Parser Selection
57
+
58
+ **Important:**
59
+ - Specify the parser as a string: `'parser'` (default), `'prism'`, or `'syntax_tree'`.
60
+ - Do **not** use constants like `Synvert::PRISM_PARSER` in the DSL.
61
+
62
+ #### Example:
63
+ ```ruby
64
+ configure(parser: 'prism')
65
+ ```
66
+
67
+ ### 3.2 Example: AMS to Alba Migration Snippet
68
+
69
+ ```ruby
70
+ # ams_to_alba.rb
71
+ Synvert::Rewriter.new 'ams_to_alba', 'Convert ActiveModelSerializers to Alba' do
72
+ configure(parser: 'prism')
73
+
74
+ within_files('app/serializers/**/*.rb') do
75
+ # Class name conversion (XXXSerializer → XXXResource)
76
+ with_node(type: 'class', name: /Serializer$/) do
77
+ replace_with node.to_source.gsub(/(\w+)Serializer/, '\1Resource').gsub(/ < ActiveModel::Serializer/, ' < Alba::Resource')
78
+ end
79
+
80
+ # include conversion
81
+ with_node(type: 'send', message: 'include', arguments: ['ActiveModel::Serializer']) do
82
+ replace_with 'include Alba::Resource'
83
+ end
84
+
85
+ # attributes conversion
86
+ with_node(type: 'send', message: 'attributes') do
87
+ args = node.arguments.map(&:to_source)
88
+ if args.length > 1
89
+ new_code = args.map { |arg| "attribute #{arg}" }.join("\n ")
90
+ replace_with new_code
91
+ else
92
+ replace_with "attribute #{args.first}"
93
+ end
94
+ end
95
+
96
+ # Association conversion
97
+ with_node(type: 'send', message: ['has_many', 'has_one', 'belongs_to']) do
98
+ relation_type = node.message
99
+ name = node.arguments.first.to_source.delete(':')
100
+ options = node.arguments[1..-1].map(&:to_source).join(', ')
101
+
102
+ if %w[has_one belongs_to].include?(relation_type)
103
+ if options.empty?
104
+ replace_with "one :#{name}"
105
+ else
106
+ replace_with "one :#{name}, #{options}"
107
+ end
108
+ elsif relation_type == 'has_many'
109
+ if options.empty?
110
+ replace_with "many :#{name}"
111
+ else
112
+ replace_with "many :#{name}, #{options}"
113
+ end
114
+ end
115
+ end
116
+
117
+ # serialize method conversion
118
+ with_node(type: 'send', message: 'serialize', arguments: { size: 2 }) do
119
+ attribute_name = node.arguments[0].to_source
120
+ serializer_name = node.arguments[1].to_source.gsub(/Serializer/, 'Resource')
121
+ replace_with "attribute #{attribute_name}, resource: #{serializer_name}"
122
+ end
123
+
124
+ # Custom method conversion
125
+ with_node(type: 'def') do
126
+ method_name = node.name.to_s
127
+ method_body = node.body.to_source
128
+ if method_body.include?('object.')
129
+ new_body = method_body.gsub(/object\./, 'resource.')
130
+ replace_with <<~RUBY
131
+ attribute :#{method_name} do |resource|
132
+ #{new_body}
133
+ end
134
+ RUBY
135
+ end
136
+ end
137
+
138
+ # Add require statement at the top
139
+ goto_node(:root) do
140
+ prepend "require 'alba'\n\n"
141
+ end
142
+ end
143
+ end
144
+ ```
145
+
146
+ ### 3.3 Indentation Helpers
147
+ - `add_leading_spaces` is **removed** as of v2.1.0. Use `indent` and `dedent` helpers for indentation adjustment.
148
+
149
+ ---
150
+
151
+ ## 4. Advanced Usage
152
+
153
+ ### 4.1 Conditional Transformation
154
+ ```ruby
155
+ with_node(type: 'send', message: 'attributes') do
156
+ if node.arguments.length > 1
157
+ args = node.arguments.map { |arg| "attribute #{arg.to_source}" }.join("\n ")
158
+ replace_with args
159
+ else
160
+ replace_with "attribute #{node.arguments.first.to_source}"
161
+ end
162
+ end
163
+ ```
164
+
165
+ ### 4.2 Nested Node Processing
166
+ ```ruby
167
+ with_node(type: 'class') do
168
+ with_node(type: 'send', message: 'has_many') do
169
+ relation_name = node.arguments.first.to_source.delete(':')
170
+ replace_with "many :#{relation_name}"
171
+ end
172
+ end
173
+ ```
174
+
175
+ ### 4.3 Adding and Removing Code
176
+ ```ruby
177
+ with_node(type: 'class', name: /Resource$/) do
178
+ append <<~RUBY
179
+ def serializable_hash(options = nil)
180
+ to_h
181
+ end
182
+ RUBY
183
+ end
184
+
185
+ with_node(type: 'def', name: 'active_model_serializer') do
186
+ remove
187
+ end
188
+ ```
189
+
190
+ ### 4.4 Import and Require Conversion
191
+ ```ruby
192
+ with_node(type: 'class', name: /Resource$/) do
193
+ goto_node(:root) do
194
+ prepend "require 'alba'\n"
195
+ end
196
+ end
197
+ ```
198
+
199
+ ### 4.5 Custom Method Conversion
200
+ ```ruby
201
+ with_node(type: 'def') do
202
+ method_name = node.name.to_s
203
+ method_body = node.body.to_source
204
+ if method_body.include?('object.')
205
+ new_body = method_body.gsub(/object\./, 'resource.')
206
+ replace_with <<~RUBY
207
+ attribute :#{method_name} do |resource|
208
+ #{new_body}
209
+ end
210
+ RUBY
211
+ end
212
+ end
213
+ ```
214
+
215
+ ---
216
+
217
+ ## 5. File Discovery and Exclusion
218
+
219
+ - If you set `Configuration.respect_gitignore = true`, `.gitignore` will be respected when searching files.
220
+ - You can use `Configuration.only_paths` and `Configuration.skip_paths` to fine-tune which files are included or excluded.
221
+
222
+ ---
223
+
224
+ ## 6. Loading Snippets
225
+
226
+ - You can load snippets from local files, GitHub, or Gist using `Utils.eval_snippet`.
227
+ - If the snippet name is a URL, it will be automatically converted to a raw URL if needed.
228
+
229
+ ---
230
+
231
+ ## 7. CLI Options
232
+
233
+ ```
234
+ Usage: synvert [options]
235
+ -d, --dry-run Dry-run (no actual file changes)
236
+ -s, --snippet SNIPPET Path to custom snippet file
237
+ -r, --run SNIPPET Name of predefined snippet to run
238
+ -l, --list List available snippets
239
+ -o, --only ONLY Process only specific files (comma-separated)
240
+ -p, --path PATH Path to process (default: current directory)
241
+ -q, --query QUERY Search for snippets by query
242
+ -v, --version Show version
243
+ -h, --help Show help message
244
+ ```
245
+
246
+ ---
247
+
248
+ ## 8. AMS → Alba Main Migration Patterns
249
+
250
+ | ActiveModelSerializers | Alba |
251
+ |------------------------|------|
252
+ | `class UserSerializer < ActiveModel::Serializer` | `class UserResource` or `class UserResource < Alba::Resource` |
253
+ | `include ActiveModel::Serializer` | `include Alba::Resource` |
254
+ | `attributes :id, :name, :email` | `attribute :id` <br> `attribute :name` <br> `attribute :email` |
255
+ | `has_many :posts` | `many :posts` |
256
+ | `has_one :profile` | `one :profile` |
257
+ | `belongs_to :organization` | `one :organization` |
258
+ | `has_many :posts, serializer: PostSerializer` | `many :posts, resource: PostResource` |
259
+ | `serialize :metadata, MetadataSerializer` | `attribute :metadata, resource: MetadataResource` |
260
+ | `def full_name; "#{object.first_name} #{object.last_name}"; end` | `attribute :full_name do |resource|` <br> ` "#{resource.first_name} #{resource.last_name}"` <br> `end` |
261
+ | `root :user` | `key :user` |
262
+
263
+ ---
264
+
265
+ ## 9. Deprecated or Removed DSLs
266
+
267
+ - `add_leading_spaces` is **removed** as of v2.1.0. Use `indent`/`dedent` helpers instead.
268
+ - `save_data` / `load_data` DSLs are **removed** as of v2.2.0.
269
+ - `if_only_exist_node` DSL is **removed** as of v1.35.0.
270
+ - `redo_until_no_change` DSL is **removed** as of v1.31.1.
271
+
272
+ ---
273
+
274
+ ## 10. Other Notes
275
+
276
+ - Use `process_with_sandbox` to run a rewriter without changing files (for dry-run purposes).
277
+ - Use the `test` method to test a rewriter's effect.
278
+ - Always refer to the [official API reference](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html) and [CHANGELOG](https://github.com/synvert-hq/synvert-core-ruby/blob/main/CHANGELOG.md) for the latest information and breaking changes.
279
+ - Not all edge cases can be covered by automated transformation. Always review and test your code after migration.
280
+
281
+ ---
282
+
283
+ ## 11. References
284
+
285
+ - [Synvert Ruby Docs](https://synvert.net/ruby/docs/)
286
+ - [Synvert Core Ruby GitHub](https://github.com/synvert-hq/synvert-core-ruby)
287
+ - [Synvert Core Ruby Documentation](https://synvert-hq.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html)
288
+ - [Synvert Architecture](https://synvert.net/architecture/)
289
+ - [Synvert Ruby Snippets](https://github.com/synvert-hq/synvert-snippets-ruby)
290
+ - [Rewriting Ruby with Synvert](https://thomasleecopeland.com/2016/11/17/rewriting-ruby-with-synvert.html)
291
+ - [Decimating Deprecated Finders with Synvert](https://thoughtbot.com/blog/decimating-deprecated-finders)
292
+ - [Alba Documentation](https://github.com/okuramasafumi/alba)
293
+ - [ActiveModelSerializers Documentation](https://github.com/rails-api/active_model_serializers)
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "alba_migration"
5
+ require "alba_migration/cli"
6
+
7
+ AlbaMigration::CLI.start(ARGV)
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rbs_inline: enabled
4
+
5
+ require "fileutils"
6
+ require "alba_migration/snippet"
7
+
8
+ module AlbaMigration
9
+ class CLI
10
+ # @rbs arg: Array[String]
11
+ # @rbs return: void
12
+ def self.start(args)
13
+ new.execute(args)
14
+ end
15
+
16
+ # @rbs file_patterns: Array[String]
17
+ # @rbs return: void
18
+ def execute(file_patterns = [])
19
+ matched_files = file_patterns.flat_map { |pattern| Dir.glob(pattern) }.uniq
20
+ if matched_files.empty?
21
+ puts "Error: No files matched the given pattern(s): #{file_patterns.join(", ")}"
22
+ exit(1)
23
+ end
24
+
25
+ missing_patterns = file_patterns.select { |pattern| Dir.glob(pattern).empty? }
26
+ unless missing_patterns.empty?
27
+ puts "Error: No files found for pattern(s): #{missing_patterns.join(", ")}"
28
+ exit(1)
29
+ end
30
+
31
+ rewriter = AlbaMigration::Snippet.create(migrate_file_path: matched_files)
32
+ rewriter.process
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rbs_inline: enabled
4
+
5
+ require "synvert/core"
6
+
7
+ module AlbaMigration
8
+ class Snippet
9
+ # @rbs file_patterns: Array[String]
10
+ # @rbs return: Synvert::Core::Rewriter
11
+ def self.create(migrate_file_path:)
12
+ Synvert::Core::Rewriter.new "alba", "convert_ams_to_alba" do
13
+ description <<~EOS
14
+ It migrates ActiveModelSerializers syntax to Alba syntax.
15
+
16
+ Example:
17
+
18
+ ```ruby
19
+ class AttributesResource < ActiveModel::Serializer
20
+ attributes :id, :name
21
+ end
22
+ ```
23
+
24
+ =>
25
+
26
+ ```ruby
27
+ class AttributesResource
28
+ include Alba::Resource
29
+
30
+ attributes :id, :name
31
+ end
32
+ ```
33
+ EOS
34
+
35
+ configure(parser: "prism")
36
+
37
+ within_files migrate_file_path do
38
+ with_node node_type: "class_node", superclass: "ActiveModel::Serializer" do
39
+ body = node.body ? node.body.to_source : ""
40
+ if body.strip.empty?
41
+ indented_body = ""
42
+ else
43
+ min_indent = body.lines.reject { |l| l.strip.empty? }.map { |l| l[/^\s*/].size }.min || 0
44
+ indented_body = body.lines.map { |l| " " + l[min_indent..] }.join
45
+ end
46
+ replace_with "class #{node.constant_path.to_source}\n include Alba::Resource\n\n#{indented_body}\nend"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rbs_inline: enabled
4
+
5
+ module AlbaMigration
6
+ VERSION = "0.0.1"
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rbs_inline: enabled
4
+
5
+ require_relative "alba_migration/version"
6
+ require_relative "alba_migration/cli"
7
+
8
+ module AlbaMigration; end
@@ -0,0 +1,13 @@
1
+ # Generated from lib/alba_migration/cli.rb with RBS::Inline
2
+
3
+ module AlbaMigration
4
+ class CLI
5
+ # @rbs arg: Array[String]
6
+ # @rbs return: void
7
+ def self.start: (untyped args) -> void
8
+
9
+ # @rbs file_patterns: Array[String]
10
+ # @rbs return: void
11
+ def execute: (?Array[String] file_patterns) -> void
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ # Generated from lib/alba_migration/snippet.rb with RBS::Inline
2
+
3
+ module AlbaMigration
4
+ class Snippet
5
+ # @rbs file_patterns: Array[String]
6
+ # @rbs return: Synvert::Core::Rewriter
7
+ def self.create: (migrate_file_path: untyped) -> Synvert::Core::Rewriter
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # Generated from lib/alba_migration/version.rb with RBS::Inline
2
+
3
+ module AlbaMigration
4
+ VERSION: ::String
5
+ end
@@ -0,0 +1,4 @@
1
+ # Generated from lib/alba_migration.rb with RBS::Inline
2
+
3
+ module AlbaMigration
4
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alba_migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ShoheiMitani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: synvert-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: AlbaMigration is a CLI tool and library to help you convert your Ruby
28
+ code from ActiveModelSerializers (AMS) to Alba syntax. It rewrites AMS serializer
29
+ classes to Alba resource classes, supporting the migration of class definitions
30
+ and attributes.
31
+ email:
32
+ - mitani49@gmail.com
33
+ executables:
34
+ - alba_migration
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ".devcontainer/Dockerfile"
39
+ - ".devcontainer/devcontainer.json"
40
+ - ".rspec"
41
+ - ".standard.yml"
42
+ - CHANGELOG.md
43
+ - CODE_OF_CONDUCT.md
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - Steepfile
48
+ - bin/console
49
+ - bin/rspec
50
+ - bin/setup
51
+ - bin/standardrb
52
+ - docs/synvert_usage_guide.md
53
+ - exe/alba_migration
54
+ - lib/alba_migration.rb
55
+ - lib/alba_migration/cli.rb
56
+ - lib/alba_migration/snippet.rb
57
+ - lib/alba_migration/version.rb
58
+ - sig/generated/alba_migration.rbs
59
+ - sig/generated/alba_migration/cli.rbs
60
+ - sig/generated/alba_migration/snippet.rbs
61
+ - sig/generated/alba_migration/version.rbs
62
+ homepage: https://github.com/ShoheiMitani/alba_migration
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ homepage_uri: https://github.com/ShoheiMitani/alba_migration
67
+ source_code_uri: https://github.com/ShoheiMitani/alba_migration
68
+ changelog_uri: https://github.com/ShoheiMitani/alba_migration/blob/main/CHANGELOG.md
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 3.1.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.5.22
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Migrate ActiveModelSerializers (AMS) code to Alba syntax automatically.
88
+ test_files: []