Replace deconstantize inflection with util

This commit is contained in:
Douglas Eichelberger 2023-02-23 22:20:35 -08:00
parent 2400c6daed
commit 2c73d4d9b7
2 changed files with 18 additions and 2 deletions

View File

@ -52,8 +52,8 @@ module Formulary
next if type == :formulary_factory
cached_objects.each_value do |klass|
namespace = klass.name.deconstantize
next if namespace.deconstantize != name
namespace = Utils::Inflection.deconstantize(klass.name)
next if Utils::Inflection.deconstantize(namespace) != name
remove_const(Utils::Inflection.demodulize(namespace))
end

View File

@ -7,6 +7,22 @@ module Utils
# @api private
module Inflection
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"