Sha256: 8745b3aef02948f0e4475dafa1401d953a8b19c2009910d7dcb4381e1b32f634

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module DidYouMean
  class MethodNameChecker
    include SpellCheckable
    attr_reader :method_name, :receiver

    def initialize(exception)
      @method_name = exception.name
      @receiver    = exception.receiver
    end

    def candidates
      { method_name => method_names }
    end

    def method_names
      method_names = receiver.methods + receiver.singleton_methods + receiver.private_methods
      method_names.delete(method_name)
      method_names.uniq!
      method_names
    end

    module IvarNameCorrectable
      REPLS = {
        "(irb)" => -> { Readline::HISTORY.to_a.last }
      }

      def initialize(exception)
        super

        @location   = exception.backtrace_locations.first
        @ivar_names = exception.instance_variable_get(:@frame_binding).receiver.instance_variables
      end

      def candidates
        super.merge(receiver_name.to_s => @ivar_names)
      end

      private

      def receiver_name
        return unless receiver.nil?

        abs_path = @location.absolute_path
        lineno   = @location.lineno

        /@(\w+)*\.#{method_name}/ =~ line(abs_path, lineno).to_s && $1
      end

      def line(abs_path, lineno)
        if REPLS[abs_path]
          REPLS[abs_path].call
        elsif File.exist?(abs_path)
          File.open(abs_path) do |file|
            file.detect { file.lineno == lineno }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
did_you_mean-1.0.0.rc1 lib/did_you_mean/spell_checkers/method_name_checker.rb