diff options
Diffstat (limited to 'lib/did_you_mean.rb')
-rw-r--r-- | lib/did_you_mean.rb | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/lib/did_you_mean.rb b/lib/did_you_mean.rb index 6d3a6e8bda..7b069935c4 100644 --- a/lib/did_you_mean.rb +++ b/lib/did_you_mean.rb @@ -86,12 +86,25 @@ require_relative 'did_you_mean/tree_spell_checker' # module DidYouMean # Map of error types and spell checker objects. - SPELL_CHECKERS = Hash.new(NullChecker) + @spell_checkers = Hash.new(NullChecker) + + # Returns a sharable hash map of error types and spell checker objects. + def self.spell_checkers + @spell_checkers + end # Adds +DidYouMean+ functionality to an error using a given spell checker def self.correct_error(error_class, spell_checker) - SPELL_CHECKERS[error_class.name] = spell_checker - error_class.prepend(Correctable) unless error_class < Correctable + if defined?(Ractor) + new_mapping = { **@spell_checkers, error_class.to_s => spell_checker } + new_mapping.default = NullChecker + + @spell_checkers = Ractor.make_shareable(new_mapping) + else + spell_checkers[error_class.to_s] = spell_checker + end + + error_class.prepend(Correctable) if error_class.is_a?(Class) && !(error_class < Correctable) end correct_error NameError, NameErrorCheckers @@ -100,15 +113,43 @@ module DidYouMean correct_error LoadError, RequirePathChecker if RUBY_VERSION >= '2.8.0' correct_error NoMatchingPatternKeyError, PatternKeyNameChecker if defined?(::NoMatchingPatternKeyError) + # TODO: Remove on 3.3: + class DeprecatedMapping # :nodoc: + def []=(key, value) + warn "Calling `DidYouMean::SPELL_CHECKERS[#{key.to_s}] = #{value.to_s}' has been deprecated. " \ + "Please call `DidYouMean.correct_error(#{key.to_s}, #{value.to_s})' instead." + + DidYouMean.correct_error(key, value) + end + + def merge!(hash) + warn "Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. " \ + "Please call `DidYouMean.correct_error(error_name, spell_checker)' instead." + + hash.each do |error_class, spell_checker| + DidYouMean.correct_error(error_class, spell_checker) + end + end + end + + # TODO: Remove on 3.3: + SPELL_CHECKERS = DeprecatedMapping.new + deprecate_constant :SPELL_CHECKERS + private_constant :DeprecatedMapping + # Returns the currently set formatter. By default, it is set to +DidYouMean::Formatter+. def self.formatter - @formatter + if defined?(Reactor) + Ractor.current[:__did_you_mean_formatter__] || Formatter + else + Formatter + end end # Updates the primary formatter used to format the suggestions. def self.formatter=(formatter) - @formatter = formatter + if defined?(Reactor) + Ractor.current[:__did_you_mean_formatter__] = formatter + end end - - @formatter = Formatter.new end |