Sha256: f19b84025ea3a026e2a05598c42edc33e54e0c71029d896d57adc1ea6e7823e5

Contents?: true

Size: 828 Bytes

Versions: 7

Compression:

Stored size: 828 Bytes

Contents

module DidYouMean
  module Experimental
    module KeyErrorWithNameAndKeys
      FILE_REGEXP = %r"#{Regexp.quote(__FILE__)}"

      def fetch(name, *)
        super
      rescue KeyError => e
        e.instance_variable_set(:@name, name)
        e.instance_variable_set(:@keys, keys)
        $@.delete_if { |s| FILE_REGEXP =~ s } if $@

        raise e
      end
    end
    Hash.prepend KeyErrorWithNameAndKeys

    class KeyNameChecker
      def initialize(key_error)
        @name = key_error.instance_variable_get(:@name)
        @keys = key_error.instance_variable_get(:@keys)
      end

      def corrections
        @corrections ||= SpellChecker.new(dictionary: @keys).correct(@name).map(&:inspect)
      end
    end

    SPELL_CHECKERS["KeyError"] = KeyNameChecker
    KeyError.prepend DidYouMean::Correctable
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
did_you_mean-1.1.3 lib/did_you_mean/experimental/key_error_name_correction.rb
did_you_mean-1.0.4 lib/did_you_mean/experimental/key_error_name_correction.rb
did_you_mean-1.0.3 lib/did_you_mean/experimental/key_error_name_correction.rb
did_you_mean-1.1.2 lib/did_you_mean/experimental/key_error_name_correction.rb
ruby-compiler-0.1.1 vendor/ruby/gems/did_you_mean-1.1.0/lib/did_you_mean/experimental/key_error_name_correction.rb
did_you_mean-1.1.0 lib/did_you_mean/experimental/key_error_name_correction.rb
did_you_mean-1.0.2 lib/did_you_mean/experimental/key_error_name_correction.rb