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

Remove ActiveSupport String inflections #14917

Merged
merged 4 commits into from
Mar 8, 2023
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
4 changes: 2 additions & 2 deletions Library/Homebrew/extend/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ def pretty_duration(s)
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

Expand Down
9 changes: 2 additions & 7 deletions Library/Homebrew/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
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"
Expand All @@ -28,12 +29,6 @@
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
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/livecheck/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def strategies
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
Expand Down
37 changes: 37 additions & 0 deletions Library/Homebrew/test/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,41 @@
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"],
Copy link
Member Author

Choose a reason for hiding this comment

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

I believe these examples already don't work (trying them in brew irb on master), so this shouldn't be a breaking change, just highlighting where we aren't matching the ActiveSupport test suite.

# ["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
22 changes: 22 additions & 0 deletions Library/Homebrew/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,26 @@ def self.pluralize(stem, count, plural: "s", singular: "")
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