Class: DidYouMean::ClassFinder

Inherits:
Object
  • Object
show all
Includes:
BaseFinder
Defined in:
lib/did_you_mean/finders/name_error_finders/class_finder.rb

Constant Summary

Constants included from BaseFinder

BaseFinder::AT, BaseFinder::EMPTY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception) ⇒ ClassFinder

Returns a new instance of ClassFinder.



8
9
10
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 8

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

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



6
7
8
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 6

def class_name
  @class_name
end

#original_messageObject (readonly)

Returns the value of attribute original_message.



6
7
8
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 6

def original_message
  @original_message
end

Instance Method Details

#class_namesObject



16
17
18
19
20
21
22
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 16

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

#name_from_messageObject

Always use the original error message to retrieve the user input since JRuby 1.7 behaves differently from MRI/Rubinius.

class Name; end
error = (Name::DoesNotExist rescue $!)

# on MRI/Rubinius
error.name #=> :DoesNotExist

# on JRuby <= 1.7
error.name #=> :'Name::DoesNotExist'


37
38
39
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 37

def name_from_message
  /([A-Z]\w*$)/.match(original_message)[0]
end

#scopesObject



50
51
52
53
54
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 50

def scopes
  @scopes ||= scope_base.inject([Object]) do |_scopes, scope|
    _scopes << _scopes.last.const_get(scope)
  end
end

#searchesObject



12
13
14
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 12

def searches
  {name_from_message => class_names}
end

#suggestionsObject



46
47
48
# File 'lib/did_you_mean/finders/name_error_finders/class_finder.rb', line 46

def suggestions
  super.map(&:full_name)
end