From d62211d3afa2e5880d01373b217b8d23f9ef2b34 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Thu, 23 Feb 2023 12:22:34 -0800 Subject: [PATCH] Replace demodulize inflection with util --- Library/Homebrew/formulary.rb | 2 +- Library/Homebrew/livecheck/livecheck.rb | 2 +- .../livecheck/strategy/electron_builder.rb | 2 +- .../livecheck/strategy/extract_plist.rb | 4 ++-- .../Homebrew/livecheck/strategy/page_match.rb | 2 +- Library/Homebrew/livecheck/strategy/sparkle.rb | 2 +- Library/Homebrew/utils/inflection.rb | 18 ++++++++++++++++++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index 6d1b16708e..4ac857835b 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -55,7 +55,7 @@ module Formulary namespace = klass.name.deconstantize next if namespace.deconstantize != name - remove_const(namespace.demodulize) + remove_const(Utils::Inflection.demodulize(namespace)) end end diff --git a/Library/Homebrew/livecheck/livecheck.rb b/Library/Homebrew/livecheck/livecheck.rb index 9e2920a77c..cef2357f69 100644 --- a/Library/Homebrew/livecheck/livecheck.rb +++ b/Library/Homebrew/livecheck/livecheck.rb @@ -61,7 +61,7 @@ module Homebrew constant = Strategy.const_get(const_symbol) next unless constant.is_a?(Class) - @livecheck_strategy_names[constant] = T.must(constant.name).demodulize + @livecheck_strategy_names[constant] = Utils::Inflection.demodulize(T.must(constant.name)) end @livecheck_strategy_names.freeze end diff --git a/Library/Homebrew/livecheck/strategy/electron_builder.rb b/Library/Homebrew/livecheck/strategy/electron_builder.rb index 8bb862e1d4..29ea5d1c2d 100644 --- a/Library/Homebrew/livecheck/strategy/electron_builder.rb +++ b/Library/Homebrew/livecheck/strategy/electron_builder.rb @@ -73,7 +73,7 @@ module Homebrew } def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} only supports a regex when using a `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/extract_plist.rb b/Library/Homebrew/livecheck/strategy/extract_plist.rb index 2486a545bc..cde31a43c8 100644 --- a/Library/Homebrew/livecheck/strategy/extract_plist.rb +++ b/Library/Homebrew/livecheck/strategy/extract_plist.rb @@ -97,9 +97,9 @@ module Homebrew } def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} only supports a regex when using a `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end - raise ArgumentError, "The #{T.must(name).demodulize} strategy only supports casks." unless T.unsafe(cask) + raise ArgumentError, "The #{Utils::Inflection.demodulize(T.must(name))} strategy only supports casks." unless T.unsafe(cask) match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/page_match.rb b/Library/Homebrew/livecheck/strategy/page_match.rb index 9b16c8b8c0..fc86e00654 100644 --- a/Library/Homebrew/livecheck/strategy/page_match.rb +++ b/Library/Homebrew/livecheck/strategy/page_match.rb @@ -93,7 +93,7 @@ module Homebrew } def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **_unused, &block) if regex.blank? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} requires a regex or `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} requires a regex or `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/livecheck/strategy/sparkle.rb b/Library/Homebrew/livecheck/strategy/sparkle.rb index 7cf8cd9acf..bba267b1e0 100644 --- a/Library/Homebrew/livecheck/strategy/sparkle.rb +++ b/Library/Homebrew/livecheck/strategy/sparkle.rb @@ -203,7 +203,7 @@ module Homebrew } def self.find_versions(url:, regex: nil, **_unused, &block) if regex.present? && block.blank? - raise ArgumentError, "#{T.must(name).demodulize} only supports a regex when using a `strategy` block" + raise ArgumentError, "#{Utils::Inflection.demodulize(T.must(name))} only supports a regex when using a `strategy` block" end match_data = { matches: {}, regex: regex, url: url } diff --git a/Library/Homebrew/utils/inflection.rb b/Library/Homebrew/utils/inflection.rb index a5cfa16a46..aaeb2ba665 100644 --- a/Library/Homebrew/utils/inflection.rb +++ b/Library/Homebrew/utils/inflection.rb @@ -7,6 +7,24 @@ module Utils # @api private module Inflection extend T::Sig + # 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 # Combines `stem` with the `singular` or `plural` suffix based on `count`. sig { params(stem: String, count: Integer, plural: String, singular: String).returns(String) }