rails_sorbet_enum 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0613df17774d270cebd8ded39b2791dada2c9ea7af876336f5abc124c4d7aa1e
4
- data.tar.gz: e984af52994438d8fe2242f615b0e2b46aec4dbbabf0369f49945750de32b19a
3
+ metadata.gz: cdef0b96b3a3eae6bd045fe68a35e277ddc318bb59a4053319001fc94726174b
4
+ data.tar.gz: 5444e0c5b93bd459c86183d8f2ee50a4548e5163d8e6bcb67a4cc81d9f34a401
5
5
  SHA512:
6
- metadata.gz: e19cc7054f62b63dd31bc2f4f08ccaa49b6a7f4596a4c20cdf7106484aa15d88fae8f82ec6efcd0c4143e97960a51c2f2318759798026aff4c1c564a12a2ba83
7
- data.tar.gz: 64baa1e994b0a4623d8c03e7a31e84cd2f4459b4e37f758da1e63387d2cde134476c0b9fa1cfd8c81e24e03f1e1451c69c6d31d8f78cd5c50f0cfe383eb9b60e
6
+ metadata.gz: 6a694bb027560e17856b7837e7c303762e623b6434dbffb028276a13ea052c7d4599511208e4d3f9dd8aa1aaf9f4ceda1f95be4b88f2635f8caa0c3ecdeb1726
7
+ data.tar.gz: f0c4512d3cf449bd88cc2afbf26ef5af0fb46a695363687d1313dff244e6c37eda646f18f15ed3ebaa2cfc714d3d0db6c0ae7cfcef70a5475934a77633995ce0
data/README.md CHANGED
@@ -1,28 +1,47 @@
1
- # RailsSorbetEnum
1
+ # rails_sorbet_enum
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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 UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
11
+ bundle add rails_sorbet_enum
15
12
  ```
16
13
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
14
+ ## Usage
15
+
16
+ Declare the enum by including `RailsSorbetEnum::Concern` and calling `sorbet_enum`.
18
17
 
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
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
- ## Usage
35
+ The `status` field will be of the enum type, rather than a string.
24
36
 
25
- TODO: Write usage instructions here
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.
@@ -39,6 +39,26 @@ module RailsSorbetEnum
39
39
 
40
40
  super(arg)
41
41
  end
42
+
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
61
+ end
42
62
  end
43
63
 
44
64
  sig { returns(T::Array[[Symbol, T.class_of(T::Enum)]]) }
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module RailsSorbetEnum
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  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 }
@@ -45,6 +45,14 @@ module Tapioca
45
45
  ],
46
46
  return_type: "void"
47
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
48
56
  end
49
57
  end
50
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu