-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So far, enum inspector has been implemented by static code analysis. But it is a bit fragile and difficult to maintain. This reimplements the inspector by spy technique. The new inspector collects the enums definitions on loading ActiveRecord models. Note: The new enum inspector must be installed before ActiveRecord models loading.
- Loading branch information
Showing
8 changed files
with
100 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'active_support/lazy_load_hooks' | ||
|
||
module RbsRails | ||
module ActiveRecord | ||
module Enum | ||
IGNORED_ENUM_KEYS = %i[_prefix _suffix _default _scopes] | ||
|
||
def enum(*args, **options) | ||
super # steep:ignore | ||
|
||
if args.empty? | ||
definitions = options.slice!(*IGNORED_ENUM_KEYS) | ||
@enum_definitions ||= [] | ||
@enum_definitions&.append([definitions, options]) | ||
end | ||
end | ||
|
||
def enum_definitions | ||
@enum_definitions&.flat_map do |(definitions, options)| | ||
definitions.flat_map do |name, values| | ||
values.map do |label, value| | ||
[name, enum_method_name(name, label, options)] | ||
end | ||
end | ||
end.to_a | ||
end | ||
|
||
private def enum_method_name(name, label, options) | ||
enum_prefix = options[:_prefix] | ||
enum_suffix = options[:_suffix] | ||
|
||
if enum_prefix == true | ||
prefix = "#{name}_" | ||
elsif enum_prefix | ||
prefix = "#{enum_prefix}_" | ||
end | ||
if enum_suffix == true | ||
suffix = "_#{name}" | ||
elsif enum_suffix | ||
suffix = "_#{enum_suffix}" | ||
end | ||
|
||
"#{prefix}#{label}#{suffix}" | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:active_record) do | ||
# @type self: singleton(ActiveRecord::Base) | ||
extend RbsRails::ActiveRecord::Enum | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module RbsRails | ||
module ActiveRecord | ||
module Enum | ||
IGNORED_ENUM_KEYS: Array[Symbol] | ||
|
||
@enum_definitions: Array[[Hash[Symbol, untyped], Hash[Symbol, untyped]]]? | ||
|
||
def enum: (*untyped, **untyped) -> void | ||
def enum_definitions: () -> Array[[Symbol, String]] | ||
|
||
private def enum_method_name: (Symbol name, Symbol label, Hash[Symbol, untyped] options) -> String | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters