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

ActiveSupport String inflections #593

Merged
merged 2 commits into from
Jun 10, 2024
Merged
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
9 changes: 9 additions & 0 deletions gems/activesupport/6.0/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class TestAttrInternal
attr_internal_writer 'internal_variable_accessor_2'
end

"yukihiro_matz".constantize
"yukihiro_matz".safe_constantize
"string_ending_with_id".titleize(keep_id_suffix: true).titleize
"::Net::HTTP".deconstantize.demodulize
"Donald E. Knuth".parameterize(separator: ":", preserve_case: true, locale: :en).parameterize
"ham_and_egg".tableize.classify
"author_id".humanize(capitalize: false, keep_id_suffix: true).humanize
"iD".upcase_first
"User".foreign_key(false).foreign_key
"yukihiro_matz".camelize.underscore.dasherize
"rabbit".pluralize.singularize
"rabbit".pluralize(2).pluralize(:en).pluralize(2, :en)
Expand Down
150 changes: 0 additions & 150 deletions gems/activesupport/6.0/activesupport-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5263,156 +5263,6 @@ class String
def indent: (untyped amount, ?untyped? indent_string, ?bool indent_empty_lines) -> untyped
end

# String inflections define new methods on the String class to transform names for different purposes.
# For instance, you can figure out the name of a table from the name of a class.
#
# 'ScaleScore'.tableize # => "scale_scores"
#
class String
# +constantize+ tries to find a declared constant with the name specified
# in the string. It raises a NameError when the name is not in CamelCase
# or is not initialized. See ActiveSupport::Inflector.constantize
#
# 'Module'.constantize # => Module
# 'Class'.constantize # => Class
# 'blargle'.constantize # => NameError: wrong constant name blargle
def constantize: () -> untyped

# +safe_constantize+ tries to find a declared constant with the name specified
# in the string. It returns +nil+ when the name is not in CamelCase
# or is not initialized. See ActiveSupport::Inflector.safe_constantize
#
# 'Module'.safe_constantize # => Module
# 'Class'.safe_constantize # => Class
# 'blargle'.safe_constantize # => nil
def safe_constantize: () -> untyped

alias camelcase camelize

# Capitalizes all the words and replaces some characters in the string to create
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
# used in the Rails internals.
#
# The trailing '_id','Id'.. can be kept and capitalized by setting the
# optional parameter +keep_id_suffix+ to true.
# By default, this parameter is false.
#
# +titleize+ is also aliased as +titlecase+.
#
# 'man from the boondocks'.titleize # => "Man From The Boondocks"
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
# 'string_ending_with_id'.titleize(keep_id_suffix: true) # => "String Ending With Id"
def titleize: (?keep_id_suffix: bool keep_id_suffix) -> untyped

alias titlecase titleize

# Removes the module part from the constant expression in the string.
#
# 'ActiveSupport::Inflector::Inflections'.demodulize # => "Inflections"
# 'Inflections'.demodulize # => "Inflections"
# '::Inflections'.demodulize # => "Inflections"
# ''.demodulize # => ''
#
# See also +deconstantize+.
def demodulize: () -> untyped

# Removes the rightmost segment from the constant expression in the string.
#
# 'Net::HTTP'.deconstantize # => "Net"
# '::Net::HTTP'.deconstantize # => "::Net"
# 'String'.deconstantize # => ""
# '::String'.deconstantize # => ""
# ''.deconstantize # => ""
#
# See also +demodulize+.
def deconstantize: () -> untyped

# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
#
# If the optional parameter +locale+ is specified,
# the word will be parameterized as a word of that language.
# By default, this parameter is set to <tt>nil</tt> and it will use
# the configured <tt>I18n.locale</tt>.
#
# class Person
# def to_param
# "#{id}-#{name.parameterize}"
# end
# end
#
# @person = Person.find(1)
# # => #<Person id: 1, name: "Donald E. Knuth">
#
# <%= link_to(@person.name, person_path) %>
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
#
# To preserve the case of the characters in a string, use the +preserve_case+ argument.
#
# class Person
# def to_param
# "#{id}-#{name.parameterize(preserve_case: true)}"
# end
# end
#
# @person = Person.find(1)
# # => #<Person id: 1, name: "Donald E. Knuth">
#
# <%= link_to(@person.name, person_path) %>
# # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>
def parameterize: (?locale: untyped? locale, ?preserve_case: bool preserve_case, ?separator: ::String separator) -> untyped

# Creates the name of a table like Rails does for models to table names. This method
# uses the +pluralize+ method on the last word in the string.
#
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
# 'ham_and_egg'.tableize # => "ham_and_eggs"
# 'fancyCategory'.tableize # => "fancy_categories"
def tableize: () -> untyped

# Creates a class name from a plural table name like Rails does for table names to models.
# Note that this returns a string and not a class. (To convert to an actual class
# follow +classify+ with +constantize+.)
#
# 'ham_and_eggs'.classify # => "HamAndEgg"
# 'posts'.classify # => "Post"
def classify: () -> untyped

# Capitalizes the first word, turns underscores into spaces, and (by default)strips a
# trailing '_id' if present.
# Like +titleize+, this is meant for creating pretty output.
#
# The capitalization of the first word can be turned off by setting the
# optional parameter +capitalize+ to false.
# By default, this parameter is true.
#
# The trailing '_id' can be kept and capitalized by setting the
# optional parameter +keep_id_suffix+ to true.
# By default, this parameter is false.
#
# 'employee_salary'.humanize # => "Employee salary"
# 'author_id'.humanize # => "Author"
# 'author_id'.humanize(capitalize: false) # => "author"
# '_id'.humanize # => "Id"
# 'author_id'.humanize(keep_id_suffix: true) # => "Author Id"
def humanize: (?keep_id_suffix: bool keep_id_suffix, ?capitalize: bool capitalize) -> untyped

# Converts just the first character to uppercase.
#
# 'what a Lovely Day'.upcase_first # => "What a Lovely Day"
# 'w'.upcase_first # => "W"
# ''.upcase_first # => ""
def upcase_first: () -> untyped

# Creates a foreign key name from a class name.
# +separate_class_name_and_id_with_underscore+ sets whether
# the method should put '_' between the name and 'id'.
#
# 'Message'.foreign_key # => "message_id"
# 'Message'.foreign_key(false) # => "messageid"
# 'Admin::Post'.foreign_key # => "post_id"
def foreign_key: (?bool separate_class_name_and_id_with_underscore) -> untyped
end

class String
# Wraps the current string in the <tt>ActiveSupport::StringInquirer</tt> class,
# which gives you a prettier way to test for equality.
Expand Down
13 changes: 13 additions & 0 deletions gems/activesupport/6.0/activesupport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,23 @@ end

class String
def camelize: (?Symbol first_letter) -> String
alias camelcase camelize
def underscore: () -> String
def dasherize: () -> String
def pluralize: (?Integer | Symbol? count, ?Symbol locale) -> String
def singularize: (?Symbol locale) -> String
def constantize: () -> untyped
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ksss Thanks for taking a look Yuki san! Yeah, I thought about it as well 👍 but noticed that it actually could be anything. For example, 'RUBY_VERSION'.constantize will be a string. Also, its value should basically the same as the value of Module#const_get. That's why I left these return values alone.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense!

def safe_constantize: () -> untyped
def titleize: (?keep_id_suffix: boolish) -> String
alias titlecase titleize
def demodulize: () -> String
def deconstantize: () -> String
def parameterize: (?separator: String, ?preserve_case: boolish, ?locale: Symbol) -> String
def tableize: () -> String
def classify: () -> String
def humanize: (?capitalize: boolish, ?keep_id_suffix: boolish) -> String
def upcase_first: () -> String
def foreign_key: (?boolish separate_class_name_and_id_with_underscore) -> String
end

module ActiveSupport
Expand Down
2 changes: 2 additions & 0 deletions gems/activesupport/7.0/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
nil.try(:tap) { |n| p n }
nil.try { p 'hello' }
nil.try { |n| p n }

"Id".downcase_first
5 changes: 5 additions & 0 deletions gems/activesupport/7.0/activesupport-7.0.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ class Array[unchecked out Elem]
# # => [1, 2, true]
def compact_blank!: () -> Array[Elem]
end

# active_support/core_ext/string/inflections.rb
class String
def downcase_first: () -> String
end