Sha256: 9ecb7ce8d45edf3c9028e563ff41f1b2f6b869f2dccd3e367a8e8d0f3cc16f52

Contents?: true

Size: 1018 Bytes

Versions: 2

Compression:

Stored size: 1018 Bytes

Contents

# -*- frozen-string-literal: true -*-
require 'delegate'

module DidYouMean
  class ClassNameChecker
    attr_reader :class_name

    def initialize(exception)
      @class_name, @receiver = exception.name, exception.receiver
    end

    def corrections
      @corrections ||= SpellChecker.new(dictionary: class_names).correct(class_name).map(&:full_name)
    end

    def class_names
      scopes.flat_map do |scope|
        scope.constants.map do |c|
          ClassName.new(c, scope == Object ? "" : "#{scope}::")
        end
      end
    end

    def scopes
      @scopes ||= @receiver.to_s.split("::").inject([Object]) do |_scopes, scope|
        _scopes << _scopes.last.const_get(scope)
      end.uniq
    end

    class ClassName < SimpleDelegator
      attr :namespace

      def initialize(name, namespace = '')
        super(name)
        @namespace = namespace
      end

      def full_name
        self.class.new("#{namespace}#{__getobj__}")
      end
    end

    private_constant :ClassName
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
did_you_mean-1.0.3 lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb
did_you_mean-1.0.2 lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb