Class: DidYouMean::ClassNameChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception) ⇒ ClassNameChecker

Returns a new instance of ClassNameChecker.



9
10
11
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb', line 9

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

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



7
8
9
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb', line 7

def class_name
  @class_name
end

Instance Method Details

#class_namesObject



20
21
22
23
24
25
26
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb', line 20

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

#correctionsObject



13
14
15
16
17
18
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb', line 13

def corrections
  @corrections ||= SpellChecker.new(dictionary: class_names)
                     .correct(class_name)
                     .map(&:full_name)
                     .reject {|qualified_name| @original_message.include?(qualified_name) }
end

#scopesObject



28
29
30
31
32
# File 'lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb', line 28

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