rails_sorbet_enum 0.1.0 → 0.3.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 +4 -4
- data/.rubocop.yml +6 -0
- data/README.md +31 -15
- data/lib/rails_sorbet_enum/concern.rb +34 -10
- data/lib/rails_sorbet_enum/require.rb +1 -0
- data/lib/rails_sorbet_enum/serializer.rb +31 -0
- data/lib/rails_sorbet_enum/version.rb +1 -1
- data/lib/tapioca/dsl/compilers/rails_sorbet_enum_compiler.rb +12 -8
- 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: cdef0b96b3a3eae6bd045fe68a35e277ddc318bb59a4053319001fc94726174b
|
|
4
|
+
data.tar.gz: 5444e0c5b93bd459c86183d8f2ee50a4548e5163d8e6bcb67a4cc81d9f34a401
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6a694bb027560e17856b7837e7c303762e623b6434dbffb028276a13ea052c7d4599511208e4d3f9dd8aa1aaf9f4ceda1f95be4b88f2635f8caa0c3ecdeb1726
|
|
7
|
+
data.tar.gz: f0c4512d3cf449bd88cc2afbf26ef5af0fb46a695363687d1313dff244e6c37eda646f18f15ed3ebaa2cfc714d3d0db6c0ae7cfcef70a5475934a77633995ce0
|
data/.rubocop.yml
CHANGED
|
@@ -5,9 +5,15 @@ AllCops:
|
|
|
5
5
|
NewCops: enable
|
|
6
6
|
TargetRubyVersion: 3.1
|
|
7
7
|
|
|
8
|
+
Layout/ArgumentAlignment:
|
|
9
|
+
EnforcedStyle: with_fixed_indentation
|
|
10
|
+
|
|
8
11
|
Metrics/AbcSize:
|
|
9
12
|
Enabled: false
|
|
10
13
|
|
|
14
|
+
Metrics/BlockLength:
|
|
15
|
+
Enabled: false
|
|
16
|
+
|
|
11
17
|
Metrics/MethodLength:
|
|
12
18
|
Enabled: false
|
|
13
19
|
|
data/README.md
CHANGED
|
@@ -1,28 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rails_sorbet_enum
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails_sorbet_enum`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
3
|
+
A gem that adds support for using typed [Sorbet T::Enums](https://sorbet.org/docs/tenum)
|
|
4
|
+
as Rails ActiveRecord enum columns.
|
|
6
5
|
|
|
7
6
|
## Installation
|
|
8
7
|
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
-
|
|
11
8
|
Install the gem and add to the application's Gemfile by executing:
|
|
12
9
|
|
|
13
10
|
```bash
|
|
14
|
-
bundle add
|
|
11
|
+
bundle add rails_sorbet_enum
|
|
15
12
|
```
|
|
16
13
|
|
|
17
|
-
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Declare the enum by including `RailsSorbetEnum::Concern` and calling `sorbet_enum`.
|
|
18
17
|
|
|
19
|
-
```
|
|
20
|
-
|
|
18
|
+
```ruby
|
|
19
|
+
# app/models/my_model.rb
|
|
20
|
+
class MyModel < ApplicationModel
|
|
21
|
+
include RailsSorbetEnum::Concern
|
|
22
|
+
|
|
23
|
+
class Status < T::Enum
|
|
24
|
+
enums do
|
|
25
|
+
Pending = new('pending')
|
|
26
|
+
Done = new('done')
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# :status is the column name with `string` type
|
|
31
|
+
sorbet_enum(:status, Status)
|
|
32
|
+
end
|
|
21
33
|
```
|
|
22
34
|
|
|
23
|
-
|
|
35
|
+
The `status` field will be of the enum type, rather than a string.
|
|
24
36
|
|
|
25
|
-
|
|
37
|
+
```ruby
|
|
38
|
+
my_model.status #=> MyModel::Status
|
|
39
|
+
my_model.status = MyModel::Status::Done
|
|
40
|
+
|
|
41
|
+
# If the enum field is set to a non-enum value, the assignment will raise
|
|
42
|
+
# rather than failing validation.
|
|
43
|
+
my_model.status = 'done' #=> raises ActiveRecord::SerializationTypeMismatch
|
|
44
|
+
```
|
|
26
45
|
|
|
27
46
|
## Development
|
|
28
47
|
|
|
@@ -30,6 +49,3 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
|
30
49
|
|
|
31
50
|
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).
|
|
32
51
|
|
|
33
|
-
## Contributing
|
|
34
|
-
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_sorbet_enum.
|
|
@@ -7,7 +7,6 @@ require "sorbet-runtime"
|
|
|
7
7
|
|
|
8
8
|
module RailsSorbetEnum
|
|
9
9
|
# Concern allows ActiveRecord models to use typechecked Sorbet T::Enums
|
|
10
|
-
# with backing ActiveRecord::Enum fields
|
|
11
10
|
module Concern
|
|
12
11
|
extend ActiveSupport::Concern
|
|
13
12
|
|
|
@@ -18,22 +17,47 @@ module RailsSorbetEnum
|
|
|
18
17
|
|
|
19
18
|
include ::ActiveRecord::Enum
|
|
20
19
|
|
|
21
|
-
sig
|
|
22
|
-
|
|
20
|
+
sig do
|
|
21
|
+
params(
|
|
22
|
+
name: Symbol,
|
|
23
|
+
type: T.class_of(T::Enum),
|
|
24
|
+
presence: T::Boolean
|
|
25
|
+
).void
|
|
26
|
+
end
|
|
27
|
+
def sorbet_enum(name, type, presence: true)
|
|
23
28
|
sorbet_enum_attributes << [name, type]
|
|
24
29
|
|
|
25
30
|
# The Rails enum values are the serialized T::Enum ones
|
|
26
|
-
|
|
31
|
+
serialize(name, coder: Serializer.new(type))
|
|
32
|
+
|
|
33
|
+
validates name,
|
|
34
|
+
presence: presence,
|
|
35
|
+
inclusion: { in: type.values, allow_blank: !presence }
|
|
27
36
|
|
|
28
|
-
T.unsafe(self).define_method("#{name}
|
|
29
|
-
|
|
30
|
-
return unless val
|
|
37
|
+
T.unsafe(self).define_method("#{name}=") do |arg|
|
|
38
|
+
raise ArgumentError, "expected #{type.name} but got #{arg.class}" if arg && arg.class != type
|
|
31
39
|
|
|
32
|
-
|
|
40
|
+
super(arg)
|
|
33
41
|
end
|
|
34
42
|
|
|
35
|
-
|
|
36
|
-
|
|
43
|
+
# Defines helpers to check if the field is equal to each enum value.
|
|
44
|
+
# For the following enum definition:
|
|
45
|
+
# class Status < T::Enum
|
|
46
|
+
# enums do
|
|
47
|
+
# Pending = new
|
|
48
|
+
# Done
|
|
49
|
+
# end
|
|
50
|
+
# end
|
|
51
|
+
# status_enum(:status, Status)
|
|
52
|
+
#
|
|
53
|
+
# The following helpers are generated:
|
|
54
|
+
# * status_pending?
|
|
55
|
+
# * status_done?
|
|
56
|
+
type.each_value do |enum_value|
|
|
57
|
+
value_name = enum_value.instance_variable_get(:@const_name).to_s.downcase
|
|
58
|
+
T.unsafe(self).define_method("#{name}_#{value_name}?") do
|
|
59
|
+
send(name) == enum_value
|
|
60
|
+
end
|
|
37
61
|
end
|
|
38
62
|
end
|
|
39
63
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module RailsSorbetEnum
|
|
5
|
+
class Serializer
|
|
6
|
+
extend T::Sig
|
|
7
|
+
|
|
8
|
+
sig { params(object_class: T.class_of(T::Enum)).void }
|
|
9
|
+
def initialize(object_class)
|
|
10
|
+
@object_class = object_class
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
sig { params(obj: T.untyped).returns(String) }
|
|
14
|
+
def dump(obj)
|
|
15
|
+
case obj
|
|
16
|
+
when T::Enum
|
|
17
|
+
obj.serialize
|
|
18
|
+
else
|
|
19
|
+
raise ActiveRecord::SerializationTypeMismatch,
|
|
20
|
+
"expected #{@object_class.name} and got #{obj.class}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
sig { params(str: T.nilable(String)).returns(T.nilable(T::Enum)) }
|
|
25
|
+
def load(str)
|
|
26
|
+
return if str.nil?
|
|
27
|
+
|
|
28
|
+
@object_class.try_deserialize(str)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -19,7 +19,7 @@ module Tapioca
|
|
|
19
19
|
class << self
|
|
20
20
|
extend T::Sig
|
|
21
21
|
|
|
22
|
-
sig { override.returns(T::Enumerable[Module]) }
|
|
22
|
+
sig { override.returns(T::Enumerable[T::Module[T.anything]]) }
|
|
23
23
|
def gather_constants
|
|
24
24
|
all_classes
|
|
25
25
|
.select { |c| c < RailsSorbetEnum::Concern }
|
|
@@ -31,7 +31,7 @@ module Tapioca
|
|
|
31
31
|
root.create_path(constant) do |klass|
|
|
32
32
|
klass.create_module(RBI_MODULE_NAME) do |methods_mod|
|
|
33
33
|
T.unsafe(constant).sorbet_enum_attributes.each do |enum_name, enum_type|
|
|
34
|
-
|
|
34
|
+
type_with_nilability =
|
|
35
35
|
if T.unsafe(constant).column_for_attribute(enum_name).null
|
|
36
36
|
"T.nilable(#{enum_type.name})"
|
|
37
37
|
else
|
|
@@ -39,16 +39,20 @@ module Tapioca
|
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
methods_mod.create_method(
|
|
42
|
-
"#{enum_name}
|
|
43
|
-
return_type: return_type
|
|
44
|
-
)
|
|
45
|
-
methods_mod.create_method(
|
|
46
|
-
"#{enum_name}_enum=",
|
|
42
|
+
"#{enum_name}=",
|
|
47
43
|
parameters: [
|
|
48
|
-
create_param("value", type:
|
|
44
|
+
create_param("value", type: type_with_nilability)
|
|
49
45
|
],
|
|
50
46
|
return_type: "void"
|
|
51
47
|
)
|
|
48
|
+
|
|
49
|
+
enum_type.each_value do |enum_value|
|
|
50
|
+
value_name = enum_value.instance_variable_get(:@const_name).to_s.downcase
|
|
51
|
+
methods_mod.create_method(
|
|
52
|
+
"#{enum_name}_#{value_name}?",
|
|
53
|
+
return_type: "T::Boolean"
|
|
54
|
+
)
|
|
55
|
+
end
|
|
52
56
|
end
|
|
53
57
|
end
|
|
54
58
|
klass.create_include(RBI_MODULE_NAME)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_sorbet_enum
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Franklin Hu
|
|
@@ -81,6 +81,7 @@ files:
|
|
|
81
81
|
- lib/rails_sorbet_enum.rb
|
|
82
82
|
- lib/rails_sorbet_enum/concern.rb
|
|
83
83
|
- lib/rails_sorbet_enum/require.rb
|
|
84
|
+
- lib/rails_sorbet_enum/serializer.rb
|
|
84
85
|
- lib/rails_sorbet_enum/version.rb
|
|
85
86
|
- lib/tapioca/dsl/compilers/rails_sorbet_enum_compiler.rb
|
|
86
87
|
- sig/rails_sorbet_enum.rbs
|