Module: Pry::MethodInfo

Defined in:
lib/pry-doc.rb

Class Method Summary collapse

Class Method Details

.aliases(meth) ⇒ Array

Retrives aliases of a method

Parameters:

  • meth (Method, UnboundMethod)

    The method object.

Returns:

  • (Array)

    The aliases of a method if it exists otherwise, return empty array



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pry-doc.rb', line 42

def self.aliases(meth)
  host        = meth.owner
  method_type = is_singleton?(meth) ? :method : :instance_method

  methods = Pry::Method.send(:all_from_common, host, method_type, false).
                        map { |m| m.instance_variable_get(:@method) }

  methods.select { |m| host.send(method_type,m.name) == host.send(method_type,meth.name) }.
          reject { |m| m.name == meth.name }.
          map    { |m| host.send(method_type,m.name) }
end

.c_files_found?(gem_dir) ⇒ Boolean

Returns true if c files exist?.

Parameters:

  • root (String)

    directory path of gem that method belongs to

Returns:

  • (Boolean)

    true if c files exist?



117
118
119
# File 'lib/pry-doc.rb', line 117

def self.c_files_found?(gem_dir)
  Dir.glob("#{gem_dir}/**/*.c").count > 0
end

.cache(meth) ⇒ Object

Cache the file that holds the method or return immediately if file is already cached. Return if the method cannot be cached - i.e is a C stdlib method.

Parameters:

  • meth (Method, UnboundMethod)

    The method object.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/pry-doc.rb', line 203

def self.cache(meth)
  file, _ = meth.source_location

  return if is_eval_method?(meth)
  return if cached?(meth)

  if !file
    parse_and_cache_if_gem_cext(meth)
    return
  end

  log.enter_level(Logger::FATAL) do
    YARD.parse(file)
  end
end

.cached?(meth) ⇒ Boolean

Check whether the file containing the method is already cached.

Parameters:

  • meth (Method, UnboundMethod)

    The method object.

Returns:

  • (Boolean)

    Whether the method is cached.



64
65
66
# File 'lib/pry-doc.rb', line 64

def self.cached?(meth)
  !!registry_lookup(meth)
end

.find_gem_dir(meth) ⇒ String

Returns root directory path of gem that method belongs to, nil if could not be found.

Parameters:

  • meth (Method, UnboundMethod)

    The method object.

Returns:

  • (String)

    root directory path of gem that method belongs to, nil if could not be found



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/pry-doc.rb', line 135

def self.find_gem_dir(meth)
  host = method_host(meth)

  begin
    host_source_location, _ =  WrappedModule.new(host).source_location
    break if host_source_location != nil
    return unless host.name
    host = eval(host.namespace_name)
  end while host

  # we want to exclude all source_locations that aren't gems (i.e
  # stdlib)
  if host_source_location && host_source_location =~ %r{/gems/}
    gem_root(host_source_location)
  else

    # the WrappedModule approach failed, so try our backup approach
    gem_dir_from_method(meth)
  end
end

.gem_dir_from_method(meth) ⇒ String?

Try to recover the gem directory of a gem based on a method object.

Parameters:

  • meth (Method, UnboundMethod)

    The method object.

Returns:

  • (String, nil)

    The located gem directory.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pry-doc.rb', line 181

def self.gem_dir_from_method(meth)
  guess = 0

  host = method_host(meth)
  return unless host.name
  root_module_name = host.name.split("::").first
  while gem_name = guess_gem_name_from_module_name(root_module_name, guess)
    matches = $LOAD_PATH.grep %r{/gems/#{gem_name}} if !gem_name.empty?
    if matches && matches.any?
      return gem_root(matches.first)
    else
      guess += 1
    end
  end

  nil
end

.gem_root(dir) ⇒ String

FIXME: this is unnecessarily limited to ext/ and lib/ folders

Returns:

  • (String)

    The root folder of a given gem directory.



128
129
130
# File 'lib/pry-doc.rb', line 128

def self.gem_root(dir)
  dir.split(/\/(?:lib|ext)(?:\/|$)/).first
end

.guess_gem_name_from_module_name(name, guess) ⇒ String?

Try to guess what the gem name will be based on the name of the module. We try a few approaches here depending on the guess parameter.

Parameters:

  • name (String)

    The name of the module.

  • guess (Fixnum)

    The current guessing approach to use.

Returns:

  • (String, nil)

    The guessed gem name, or nil if out of guesses.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pry-doc.rb', line 161

def self.guess_gem_name_from_module_name(name, guess)
  case guess
  when 0
    name.downcase
  when 1
    name.scan(/[A-Z][a-z]+/).map(&:downcase).join('_')
  when 2
    name.scan(/[A-Z][a-z]+/).map(&:downcase).join('_').sub("_", "-")
  when 3
    name.scan(/[A-Z][a-z]+/).map(&:downcase).join('-')
  when 4
    name
  else
    nil
  end
end

.info_for(meth) ⇒ YARD::CodeObjects::MethodObject

Retrieve the YARD object that contains the method data.

Parameters:

  • meth (Method, UnboundMethod)

    The method object.

Returns:

  • (YARD::CodeObjects::MethodObject)

    The YARD data for the method.



86
87
88
89
# File 'lib/pry-doc.rb', line 86

def self.info_for(meth)
  cache(meth)
  registry_lookup(meth)
end

.is_eval_method?(meth) ⇒ Boolean

Determine whether a method is an eval method.

Returns:

  • (Boolean)

    Whether the method is an eval method.



93
94
95
96
97
98
99
100
# File 'lib/pry-doc.rb', line 93

def self.is_eval_method?(meth)
  file, _ = meth.source_location
  if file =~ /(\(.*\))|<.*>/
    true
  else
    false
  end
end

.is_singleton?(meth) ⇒ Boolean

Checks whether method is a singleton (i.e class method)

Parameters:

  • meth (Method, UnboundMethod)
  • true (Boolean)

    if singleton

Returns:

  • (Boolean)


57
58
59
# File 'lib/pry-doc.rb', line 57

def self.is_singleton?(meth)
  meth.owner.ancestors.first != meth.owner
end

.method_host(meth) ⇒ Object

Returns The host of the method (receiver or owner).

Returns:

  • (Object)

    The host of the method (receiver or owner).



122
123
124
# File 'lib/pry-doc.rb', line 122

def self.method_host(meth)
  is_singleton?(meth) && Module === meth.receiver ? meth.receiver : meth.owner
end

.parse_and_cache_if_gem_cext(meth) ⇒ Object

Attempts to find the c source files if method belongs to a gem and use YARD to parse and cache the source files for display

Parameters:

  • meth (Method, UnboundMethod)

    The method object.



106
107
108
109
110
111
112
113
# File 'lib/pry-doc.rb', line 106

def self.parse_and_cache_if_gem_cext(meth)
  if gem_dir = find_gem_dir(meth)
    if c_files_found?(gem_dir)
      warn "Scanning and caching *.c files..."
      YARD.parse("#{gem_dir}/**/*.c")
    end
  end
end

.receiver_notation_for(meth) ⇒ String

Convert a method object into the Class#method string notation.

Parameters:

  • meth (Method, UnboundMethod)

Returns:

  • (String)

    The method in string receiver notation.



30
31
32
33
34
35
36
# File 'lib/pry-doc.rb', line 30

def self.receiver_notation_for(meth)
  if is_singleton?(meth)
    "#{meth.owner.to_s[/#<.+?:(.+?)>/, 1]}.#{meth.name}"
  else
    "#{meth.owner.name}##{meth.name}"
  end
end

.registry_lookup(meth) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pry-doc.rb', line 68

def self.registry_lookup(meth)
  obj = YARD::Registry.at(receiver_notation_for(meth))

  if obj.nil?
    if !(aliases = aliases(meth)).empty?
      obj = YARD::Registry.at(receiver_notation_for(aliases.first))
    elsif meth.owner == Kernel
      # YARD thinks that some methods are on Object when
      # they're actually on Kernel; so try again on Object if Kernel fails.
      obj = YARD::Registry.at("Object##{meth.name}")
    end
  end
  obj
end