Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum: Define enum mapping methods #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/rbs_rails/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def generate
#{delegated_type_instance}
#{delegated_type_scope(singleton: true)}
#{enum_instance_methods}
#{enum_mapping_methods}
#{enum_scope_methods(singleton: true)}
#{scopes(singleton: true)}

Expand Down Expand Up @@ -333,6 +334,16 @@ def authenticate_#{attribute}: (String) -> (#{klass_name} | false)
methods.join("\n")
end

private def enum_mapping_methods
enum_definitions.flat_map do |hash|
hash.keys.filter_map do |name|
next if IGNORED_ENUM_KEYS.include?(name)

"def self.#{name.to_s.pluralize}: () -> ActiveSupport::HashWithIndifferentAccess[Symbol, untyped]"
end
end.join("\n")
end

private def enum_scope_methods(singleton:)
# @type var methods: Array[String]
methods = []
Expand Down
2 changes: 2 additions & 0 deletions sig/rbs_rails/active_record.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class RbsRails::ActiveRecord::Generator

def enum_instance_methods: () -> String

def enum_mapping_methods: () -> String

def enum_scope_methods: (singleton: untyped `singleton`) -> String

def enum_definitions: () -> Array[Hash[Symbol, untyped]]
Expand Down
1 change: 1 addition & 0 deletions test/expectations/user.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class User < ::ApplicationRecord
def temporary?: () -> bool
def accepted!: () -> bool
def accepted?: () -> bool
def self.statuses: () -> ActiveSupport::HashWithIndifferentAccess[Symbol, untyped]
def self.temporary: () -> ::User::ActiveRecord_Relation
def self.accepted: () -> ::User::ActiveRecord_Relation
def self.all_kind_args: (untyped type, ?untyped m, ?untyped n, *untyped rest, untyped x, ?k: untyped, **untyped untyped) { (*untyped) -> untyped } -> ::User::ActiveRecord_Relation
Expand Down
Loading