Skip to content

Commit

Permalink
Add enum mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
IBazylchuk committed Dec 6, 2024
1 parent 1ba6df1 commit 4e9acaf
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/rbs_rails/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def generate
#{delegated_type_scope(singleton: true)}
#{enum_instance_methods}
#{enum_scope_methods(singleton: true)}
#{enum_mapping}
#{scopes(singleton: true)}
#{generated_relation_methods_decl}
Expand Down Expand Up @@ -351,6 +352,23 @@ def authenticate_#{attribute}: (String) -> (#{klass_name} | false)
methods.join("\n")
end

private def enum_mapping
methods = []
enum_definitions.each do |hash|
hash.each do |name, values|
next if IGNORED_ENUM_KEYS.include?(name)

col = klass.columns.find { |col| col.name == name.to_s }
class_name = sql_type_to_class(col.type)
class_name_opt = optional(class_name)
column_type = col.null ? class_name_opt : class_name

methods << "def self.#{name.to_s.pluralize}: () -> ::Hash[(::String | ::Symbol), #{column_type}]"
end
end
methods.join("\n")
end

private def enum_definitions
@enum_definitions ||= build_enum_definitions
end
Expand Down

0 comments on commit 4e9acaf

Please sign in to comment.