Module: ActiveRecord::Enum
- Extended by:
- T::Sig
- Includes:
- Kernel
- Defined in:
- lib/sorbet-rails/rails_mixins/active_record_overrides.rb
Defined Under Namespace
Classes: ConflictTypedEnumNameError, MultipleEnumsDefinedError
Constant Summary
collapse
- SR_ENUM_KEYWORDS =
[
:_prefix,
:_suffix,
:_scopes
]
Instance Method Summary
collapse
Instance Method Details
#_define_enum(definitions) ⇒ Object
56
57
58
59
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 56
def _define_enum(definitions)
ActiveRecordOverrides.instance.store_enum_call(self, definitions)
old_enum(definitions)
end
|
#_define_typed_enum(enum_name, enum_values, strict_mode: false) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 117
def _define_typed_enum(
enum_name,
enum_values,
strict_mode: false
)
enum_klass_name = enum_name.to_s.camelize
typed_enum_values = gen_typed_enum_values(enum_values.map(&:to_s))
if const_defined?(enum_klass_name)
enum_klass_name = "#{enum_klass_name}Enum"
if const_defined?(enum_klass_name)
raise ConflictTypedEnumNameError.new(
"Unable to define enum class #{enum_klass_name} because
it's already defined".squish!
)
end
end
enum_klass = Class.new(T::Enum) do
enums do
typed_enum_values.each do |enum_key_name, typed_enum_value|
const_set(typed_enum_value, new(enum_key_name))
end
end
end
const_set(enum_klass_name, enum_klass)
typed_enum_getter_name = "typed_#{enum_name}"
detect_enum_conflict!(enum_name, typed_enum_getter_name)
define_method(typed_enum_getter_name) do
T.unsafe(enum_klass).try_deserialize(send(enum_name))
end
enum_setter_name = "#{enum_name}="
typed_enum_setter_name = "typed_#{enum_name}="
detect_enum_conflict!(enum_name, typed_enum_setter_name)
define_method(typed_enum_setter_name) do |value|
send(enum_setter_name, value&.serialize)
end
typed_enum_reflections[enum_name.to_s] = SorbetRails::TypedEnumConfig.new(
strict_mode: strict_mode || false,
class_name: enum_klass_name,
)
end
|
#enum(definitions) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 62
def enum(definitions)
_define_enum(definitions)
definitions.each do |enum_name, values|
begin
next if SR_ENUM_KEYWORDS.include?(enum_name)
_define_typed_enum(enum_name, (values))
rescue ArgumentError, ConflictTypedEnumNameError, TypeError => ex
puts "warning: #{ex.message}"
rescue => ex
puts "warning: #{ex.message}"
end
end
end
|
186
187
188
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 186
def (enum_def)
enum_def.is_a?(Array) ? enum_def.map(&:to_sym) : enum_def.keys.map(&:to_sym)
end
|
#gen_typed_enum_values(enum_values) ⇒ Object
171
172
173
174
175
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 171
def gen_typed_enum_values(enum_values)
Hash[enum_values.map do |val|
[val, val.to_s.gsub(/[^0-9a-z_]/i, '').camelize]
end]
end
|
#old_enum ⇒ Object
46
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 46
alias old_enum enum
|
#typed_enum(definitions) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 83
def typed_enum(definitions)
enum_names = definitions.keys - SR_ENUM_KEYWORDS
if enum_names.size != 1
raise MultipleEnumsDefinedError.new(
"typed_enum only supports 1 enum defined at a time,
given #{enum_names.count}: #{enum_names.join(', ')}".squish!
)
end
enum_name = enum_names[0]
_define_enum(definitions)
_define_typed_enum(
T.must(enum_name),
(definitions[enum_name]),
strict_mode: true,
)
end
|
#typed_enum_reflections ⇒ Object
105
106
107
|
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 105
def typed_enum_reflections
@typed_enum_reflections ||= {}
end
|