Class: DidYouMean::MethodFinder

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

Defined Under Namespace

Modules: RubiniusSupport

Constant Summary

Constants included from BaseFinder

BaseFinder::AT, BaseFinder::EMPTY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseFinder

#suggestions

Constructor Details

#initialize(exception) ⇒ MethodFinder

Returns a new instance of MethodFinder.



6
7
8
9
10
11
12
# File 'lib/did_you_mean/finders/method_finder.rb', line 6

def initialize(exception)
  @method_name = exception.name
  @receiver    = exception.receiver
  @binding     = exception.frame_binding
  @location    = exception.backtrace.first
  @ivar_names  = NameFinder.new(exception).ivar_names
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



4
5
6
# File 'lib/did_you_mean/finders/method_finder.rb', line 4

def method_name
  @method_name
end

#receiverObject (readonly)

Returns the value of attribute receiver.



4
5
6
# File 'lib/did_you_mean/finders/method_finder.rb', line 4

def receiver
  @receiver
end

Instance Method Details

#method_namesObject



21
22
23
24
25
26
27
# File 'lib/did_you_mean/finders/method_finder.rb', line 21

def method_names
  method_names = receiver.methods + receiver.singleton_methods
  method_names += receiver.private_methods if receiver.equal?(@binding.eval("self"))
  method_names.delete(method_name)
  method_names.uniq!
  method_names
end

#receiver_nameObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/did_you_mean/finders/method_finder.rb', line 29

def receiver_name
  return unless @receiver.nil?

  abs_path, lineno, label =
    /(.*):(.*):in `(.*)'/ =~ @location && [$1, $2.to_i, $3]

  line =
    case abs_path
    when "(irb)"
      Readline::HISTORY.to_a.last
    when "(pry)"
      ::Pry.history.to_a.last
    else
      File.open(abs_path) do |file|
        file.detect { file.lineno == lineno }
      end if File.exist?(abs_path)
    end

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

#searchesObject



14
15
16
17
18
19
# File 'lib/did_you_mean/finders/method_finder.rb', line 14

def searches
  {
    method_name        => method_names,
    receiver_name.to_s => @ivar_names
  }
end