Move inflection utils to Utils module

This commit is contained in:
Douglas Eichelberger 2023-02-27 20:20:56 -08:00
parent 0eccc0e987
commit 325994a60c

View File

@ -12,7 +12,6 @@ require "utils/git"
require "utils/git_repository" require "utils/git_repository"
require "utils/github" require "utils/github"
require "utils/gzip" require "utils/gzip"
require "utils/inflection"
require "utils/inreplace" require "utils/inreplace"
require "utils/link" require "utils/link"
require "utils/popen" require "utils/popen"
@ -86,3 +85,50 @@ module Homebrew
end end
# rubocop:enable Style/GlobalVars # rubocop:enable Style/GlobalVars
end end
module Utils
extend T::Sig
# Removes the rightmost segment from the constant expression in the string.
#
# deconstantize('Net::HTTP') # => "Net"
# deconstantize('::Net::HTTP') # => "::Net"
# deconstantize('String') # => ""
# deconstantize('::String') # => ""
# deconstantize('') # => ""
#
# See also #demodulize.
# @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L247-L258
# `ActiveSupport::Inflector.deconstantize`
sig { params(path: String).returns(String) }
def self.deconstantize(path)
T.must(path[0, path.rindex("::") || 0]) # implementation based on the one in facets' Module#spacename
end
# Removes the module part from the expression in the string.
#
# demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
# demodulize('Inflections') # => "Inflections"
# demodulize('::Inflections') # => "Inflections"
# demodulize('') # => ""
#
# See also #deconstantize.
# @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L230-L245
# `ActiveSupport::Inflector.demodulize`
sig { params(path: String).returns(String) }
def self.demodulize(path)
if (i = path.rindex("::"))
T.must(path[(i + 2)..])
else
path
end
end
# A lightweight alternative to `ActiveSupport::Inflector.pluralize`:
# Combines `stem` with the `singular` or `plural` suffix based on `count`.
sig { params(stem: String, count: Integer, plural: String, singular: String).returns(String) }
def self.pluralize(stem, count, plural: "s", singular: "")
suffix = (count == 1) ? singular : plural
"#{stem}#{suffix}"
end
end