lib/active_support/inflector/methods.rb in activesupport-7.0.8.6 vs lib/active_support/inflector/methods.rb in activesupport-7.1.0.beta1
- old
+ new
@@ -1,17 +1,18 @@
# frozen_string_literal: true
require "active_support/inflections"
-require "active_support/core_ext/object/blank"
module ActiveSupport
+ # = Active Support \Inflector
+ #
# The Inflector transforms words from singular to plural, class names to table
# names, modularized class names to ones without, and class names to foreign
# keys. The default inflections for pluralization, singularization, and
# uncountable words are kept in inflections.rb.
#
- # The Rails core team has stated patches for the inflections library will not
+ # The \Rails core team has stated patches for the inflections library will not
# be accepted in order to avoid breaking legacy applications which may be
# relying on errant inflections. If you discover an incorrect inflection and
# require it for your application or wish to define rules for languages other
# than English, please correct or add them yourself (explained below).
module Inflector
@@ -69,10 +70,12 @@
def camelize(term, uppercase_first_letter = true)
string = term.to_s
# String#camelize takes a symbol (:upper or :lower), so here we also support :lower to keep the methods consistent.
if !uppercase_first_letter || uppercase_first_letter == :lower
string = string.sub(inflections.acronyms_camelize_regex) { |match| match.downcase! || match }
+ elsif string.match?(/\A[a-z\d]*\z/)
+ return inflections.acronyms[string]&.dup || string.capitalize
else
string = string.sub(/^[a-z\d]*/) { |match| inflections.acronyms[match] || match.capitalize! || match }
end
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) do
word = $2
@@ -92,14 +95,14 @@
# As a rule of thumb you can think of +underscore+ as the inverse of
# #camelize, though there are cases where that does not hold:
#
# camelize(underscore('SSLError')) # => "SslError"
def underscore(camel_cased_word)
- return camel_cased_word.to_s unless /[A-Z-]|::/.match?(camel_cased_word)
+ return camel_cased_word.to_s.dup unless /[A-Z-]|::/.match?(camel_cased_word)
word = camel_cased_word.to_s.gsub("::", "/")
word.gsub!(inflections.acronyms_underscore_regex) { "#{$1 && '_' }#{$2.downcase}" }
- word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << "_" }
+ word.gsub!(/(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-z\d])(?=[A-Z])/, "_")
word.tr!("-", "_")
word.downcase!
word
end
@@ -153,22 +156,31 @@
end
result
end
- # Converts just the first character to uppercase.
+ # Converts the first character in the string to uppercase.
#
# upcase_first('what a Lovely Day') # => "What a Lovely Day"
# upcase_first('w') # => "W"
# upcase_first('') # => ""
def upcase_first(string)
string.length > 0 ? string[0].upcase.concat(string[1..-1]) : ""
end
+ # Converts the first character in the string to lowercase.
+ #
+ # downcase_first('If they enjoyed The Matrix') # => "if they enjoyed The Matrix"
+ # downcase_first('I') # => "i"
+ # downcase_first('') # => ""
+ def downcase_first(string)
+ string.length > 0 ? string[0].downcase.concat(string[1..-1]) : ""
+ end
+
# 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.
+ # 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.
#
@@ -181,21 +193,21 @@
humanize(underscore(word), keep_id_suffix: keep_id_suffix).gsub(/\b(?<!\w['’`()])[a-z]/) do |match|
match.capitalize
end
end
- # Creates the name of a table like Rails does for models to table names.
+ # 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.
#
# tableize('RawScaledScorer') # => "raw_scaled_scorers"
# tableize('ham_and_egg') # => "ham_and_eggs"
# tableize('fancyCategory') # => "fancy_categories"
def tableize(class_name)
pluralize(underscore(class_name))
end
- # Creates a class name from a plural table name like Rails does for table
+ # 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.)
#
# classify('ham_and_eggs') # => "HamAndEgg"
# classify('posts') # => "Post"
@@ -224,11 +236,11 @@
#
# See also #deconstantize.
def demodulize(path)
path = path.to_s
if i = path.rindex("::")
- path[(i + 2)..-1]
+ path[(i + 2), path.length]
else
path
end
end
@@ -343,10 +355,10 @@
# const_regexp("Foo::Bar::Baz") # => "Foo(::Bar(::Baz)?)?"
# const_regexp("::") # => "::"
def const_regexp(camel_cased_word)
parts = camel_cased_word.split("::")
- return Regexp.escape(camel_cased_word) if parts.blank?
+ return Regexp.escape(camel_cased_word) if parts.empty?
last = parts.pop
parts.reverse!.inject(last) do |acc, part|
part.empty? ? acc : "#{part}(::#{acc})?"