Merge pull request #14917 from dduugg/string-inflections

Remove ActiveSupport String inflections
This commit is contained in:
Mike McQuaid 2023-03-08 12:39:54 +00:00 committed by GitHub
commit b1801333cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 10 deletions

View File

@ -199,13 +199,13 @@ module Kernel
if s > 59
m = s / 60
s %= 60
res = +"#{m} #{"minute".pluralize(m)}"
res = +"#{m} #{Utils.pluralize("minute", m)}"
return res.freeze if s.zero?
res << " "
end
res << "#{s} #{"second".pluralize(s)}"
res << "#{s} #{Utils.pluralize("second", s)}"
res.freeze
end

View File

@ -16,7 +16,8 @@ require "active_support/core_ext/object/blank"
require "active_support/core_ext/string/filters"
require "active_support/core_ext/object/try"
require "active_support/core_ext/array/access"
require "active_support/core_ext/string/inflections"
require "i18n"
require "active_support/core_ext/hash/except"
require "active_support/core_ext/kernel/reporting"
require "active_support/core_ext/hash/keys"
require "active_support/core_ext/hash/deep_merge"
@ -28,12 +29,6 @@ require "active_support/core_ext/string/indent"
I18n.backend.available_locales # Initialize locales so they can be overwritten.
I18n.backend.store_translations :en, support: { array: { last_word_connector: " and " } }
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular "formula", "formulae"
inflect.irregular "is", "are"
inflect.irregular "it", "they"
end
HOMEBREW_API_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_API_DEFAULT_DOMAIN").freeze
HOMEBREW_BOTTLE_DEFAULT_DOMAIN = ENV.fetch("HOMEBREW_BOTTLE_DEFAULT_DOMAIN").freeze
HOMEBREW_BREW_DEFAULT_GIT_REMOTE = ENV.fetch("HOMEBREW_BREW_DEFAULT_GIT_REMOTE").freeze

View File

@ -111,7 +111,7 @@ module Homebrew
constant = Strategy.const_get(const_symbol)
next unless constant.is_a?(Class)
key = const_symbol.to_s.underscore.to_sym
key = Utils.underscore(const_symbol).to_sym
@strategies[key] = constant
end
@strategies

View File

@ -58,4 +58,41 @@ describe Utils do
expect(described_class.pluralize("foo", 2, singular: "o", plural: "es")).to eq("fooes")
end
end
describe ".underscore" do
# commented out entries require acronyms inflections
let(:words) {
[
["API", "api"],
["APIController", "api_controller"],
["Nokogiri::HTML", "nokogiri/html"],
# ["HTTPAPI", "http_api"],
["HTTP::Get", "http/get"],
["SSLError", "ssl_error"],
# ["RESTful", "restful"],
# ["RESTfulController", "restful_controller"],
# ["Nested::RESTful", "nested/restful"],
# ["IHeartW3C", "i_heart_w3c"],
# ["PhDRequired", "phd_required"],
# ["IRoRU", "i_ror_u"],
# ["RESTfulHTTPAPI", "restful_http_api"],
# ["HTTP::RESTful", "http/restful"],
# ["HTTP::RESTfulAPI", "http/restful_api"],
# ["APIRESTful", "api_restful"],
["Capistrano", "capistrano"],
["CapiController", "capi_controller"],
["HttpsApis", "https_apis"],
["Html5", "html5"],
["Restfully", "restfully"],
["RoRails", "ro_rails"],
]
}
it "converts strings to underscore case" do
words.each do |camel, under|
expect(described_class.underscore(camel)).to eq(under)
expect(described_class.underscore(under)).to eq(under)
end
end
end
end

View File

@ -131,4 +131,26 @@ module Utils
suffix = (count == 1) ? singular : plural
"#{stem}#{suffix}"
end
# Makes an underscored, lowercase form from the expression in the string.
#
# Changes '::' to '/' to convert namespaces to paths.
#
# underscore('ActiveModel') # => "active_model"
# underscore('ActiveModel::Errors') # => "active_model/errors"
#
# @see https://github.com/rails/rails/blob/v6.1.7.2/activesupport/lib/active_support/inflector/methods.rb#L81-L100
# `ActiveSupport::Inflector.underscore`
sig { params(camel_cased_word: T.any(String, Symbol)).returns(String) }
def self.underscore(camel_cased_word)
return camel_cased_word.to_s unless /[A-Z-]|::/.match?(camel_cased_word)
word = camel_cased_word.to_s.gsub("::", "/")
word.gsub!(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) do
T.must(::Regexp.last_match(1) || ::Regexp.last_match(2)) << "_"
end
word.tr!("-", "_")
word.downcase!
word
end
end