Class: Pry::WrappedModule

Inherits:
Object show all
Includes:
Helpers::DocumentationHelpers
Defined in:
lib/pry/wrapped_module.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Helpers::DocumentationHelpers

#process_comment_markup, #process_rdoc, #process_yardoc, #process_yardoc_tag, #strip_comments_from_c_code, #strip_leading_hash_and_whitespace_from_ruby_comments, #strip_leading_whitespace

Constructor Details

- (WrappedModule) initialize(mod)

Create a new WrappedModule

Parameters:

  • (Module)

Raises:

  • ArgumentError, if the argument is not a Module



46
47
48
49
50
51
52
53
# File 'lib/pry/wrapped_module.rb', line 46

def initialize(mod)
  raise ArgumentError, "Tried to initialize a WrappedModule with a non-module #{mod.inspect}" unless ::Module === mod
  @wrapped = mod
  @host_file_lines = nil
  @source = nil
  @source_location = nil
  @doc = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_name, *args, &block)

Forward method invocations to the wrapped module



105
106
107
# File 'lib/pry/wrapped_module.rb', line 105

def method_missing(method_name, *args, &block)
  wrapped.send(method_name, *args, &block)
end

Instance Attribute Details

- (Object) wrapped (readonly, private)

Returns the value of attribute wrapped



19
20
21
# File 'lib/pry/wrapped_module.rb', line 19

def wrapped
  @wrapped
end

Class Method Details

+ (Module?) from_str(mod_name, target = TOPLEVEL_BINDING)

Convert a string to a module.

Examples:

Pry::WrappedModule.from_str("Pry::Code")

Parameters:

  • mod_name (String)
  • target (Binding) (defaults to: TOPLEVEL_BINDING)

    The binding where the lookup takes place.

Returns:

  • (Module, nil)

    The module or nil (if conversion failed).



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pry/wrapped_module.rb', line 29

def self.from_str(mod_name, target=TOPLEVEL_BINDING)
  kind = target.eval("defined?(#{mod_name})")

  # if we dont limit it to constants then from_str could end up
  # executing methods which is not good, i.e `show-source pry`
  if (kind == "constant" && target.eval(mod_name).is_a?(Module))
    Pry::WrappedModule.new(target.eval(mod_name))
  else
    nil
  end
rescue RescuableException
  nil
end

Instance Method Details

- (Object) all_from_common(mod, method_type)

FIXME: a variant of this method is also found in Pry::Method



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/pry/wrapped_module.rb', line 250

def all_from_common(mod, method_type)
  %w(public protected private).map do |visibility|
    safe_send(mod, :#{visibility}_#{method_type}s", false).select do |method_name|
      if method_type == :method
        safe_send(mod, method_type, method_name).owner == class << mod; self; end
      else
        safe_send(mod, method_type, method_name).owner == mod
      end
    end.map do |method_name|
      Pry::Method.new(safe_send(mod, method_type, method_name), :visibility => visibility.to_sym)
    end
  end.flatten
end

- (Object) all_methods_for(mod)



264
265
266
# File 'lib/pry/wrapped_module.rb', line 264

def all_methods_for(mod)
  all_from_common(mod, :instance_method) + all_from_common(mod, :method)
end

- (Object) all_source_locations_by_popularity



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/pry/wrapped_module.rb', line 268

def all_source_locations_by_popularity
  return @all_source_locations_by_popularity if @all_source_locations_by_popularity

  ims = all_methods_for(wrapped)

  ims.reject! do |v|
    begin
      v.alias? || v.source_location.nil?
    rescue Pry::RescuableException
      true
    end
  end

  @all_source_locations_by_popularity = ims.group_by { |v| Array(v.source_location).first }.
    sort_by { |k, v| -v.size }
end

- (Object) doc

Raises:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pry/wrapped_module.rb', line 122

def doc
  return @doc if @doc

  if yard_docs?
    from_yard = YARD::Registry.at(name)
    @doc = from_yard.docstring
  elsif source_location.nil?
    raise CommandError, "Can't find module's source location"
  else
    @doc = extract_doc_for_candidate(0)
  end

  raise CommandError, "Can't find docs for module: #{name}." if !@doc

  @doc = process_doc(@doc)
end

- (Object) doc_for_candidate(idx)

Raises:



139
140
141
142
143
144
# File 'lib/pry/wrapped_module.rb', line 139

def doc_for_candidate(idx)
  doc = extract_doc_for_candidate(idx)
  raise CommandError, "Can't find docs for module: #{name}." if !doc

  process_doc(doc)
end

- (Object) extract_doc



223
224
225
# File 'lib/pry/wrapped_module.rb', line 223

def extract_doc
  extract_doc_for_candidate(0)
end

- (Object) extract_doc_for_candidate(idx)



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/pry/wrapped_module.rb', line 227

def extract_doc_for_candidate(idx)
  _, line_num = module_source_location_for_candidate(idx)

  buffer = ""
  lines_for_file(source_file_for_candidate(idx))[0..(line_num - 2)].each do |line|
    # Add any line that is a valid ruby comment,
    # but clear as soon as we hit a non comment line.
    if (line =~ /^\s*#/) || (line =~ /^\s*$/)
      buffer << line.lstrip
    else
      buffer.replace("")
    end
  end

  buffer
end

- (Object) lines_for_file(file)

memoized lines for file



193
194
195
196
197
198
199
200
201
# File 'lib/pry/wrapped_module.rb', line 193

def lines_for_file(file)
  @lines_for_file ||= {}

  if file == Pry.eval_path
   @lines_for_file[file] ||= Pry.line_buffer.drop(1)
  else
   @lines_for_file[file] ||= File.readlines(file)
  end
end

- (Object) method_candidates



285
286
287
288
289
# File 'lib/pry/wrapped_module.rb', line 285

def method_candidates
  @method_candidates ||= all_source_locations_by_popularity.map do |group|
    group.last.sort_by(&:source_line).first # best candidate for group
  end
end

- (Object) method_prefix

The prefix that would appear before methods defined on this class.

i.e. the "String." or "String#" in String.new and String#initialize.

Returns:

  • String



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pry/wrapped_module.rb', line 60

def method_prefix
  if singleton_class?
    if Module === singleton_instance
      "#{WrappedModule.new(singleton_instance).nonblank_name}."
    else
      "self."
    end
  else
    "#{nonblank_name}#"
  end
end

- (Object) method_source_location_for_candidate(idx)



295
296
297
298
299
300
301
302
303
# File 'lib/pry/wrapped_module.rb', line 295

def method_source_location_for_candidate(idx)
  file, line = method_candidates[idx].source_location

  if file && RbxPath.is_core_path?(file)
    file = RbxPath.convert_path_to_full(file)
  end

  [file, line]
end

- (Object) module_source_location_for_candidate(idx)



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/pry/wrapped_module.rb', line 203

def module_source_location_for_candidate(idx)
  mod_type_string = wrapped.class.to_s.downcase
  file, line = method_source_location_for_candidate(idx)

  return nil if !file.is_a?(String)

  class_regexes = [/#{mod_type_string}\s*(\w*)(::)?#{wrapped.name.split(/::/).last}/,
                   /(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/,
                   /(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/]

  host_file_lines = lines_for_file(file)

  search_lines = host_file_lines[0..(line - 2)]
  idx = search_lines.rindex { |v| class_regexes.any? { |r| r =~ v } }

  [file,  idx + 1]
rescue Pry::RescuableException
  nil
end

- (String) nonblank_name

The name of the Module if it has one, otherwise #Class:0xf00.

Returns:

  • (String)


75
76
77
78
79
80
81
# File 'lib/pry/wrapped_module.rb', line 75

def nonblank_name
  if name.to_s == ""
    wrapped.inspect
  else
    name
  end
end

- (Object) number_of_candidates



291
292
293
# File 'lib/pry/wrapped_module.rb', line 291

def number_of_candidates
  method_candidates.count
end

- (Object) process_doc(doc)



117
118
119
120
# File 'lib/pry/wrapped_module.rb', line 117

def process_doc(doc)
  process_comment_markup(strip_leading_hash_and_whitespace_from_ruby_comments(doc),
                         :ruby)
end

- (Boolean) respond_to?(method_name)

Returns:

  • (Boolean)


109
110
111
# File 'lib/pry/wrapped_module.rb', line 109

def respond_to?(method_name)
  super || wrapped.respond_to?(method_name)
end

- (Object) safe_send(obj, method, *args, &block)

FIXME: this method is also found in Pry::Method



245
246
247
# File 'lib/pry/wrapped_module.rb', line 245

def safe_send(obj, method, *args, &block)
  (Module === obj ? Module : Object).instance_method(method).bind(obj).call(*args, &block)
end

- (Boolean) singleton_class?

Is this a singleton class?

Returns:

  • (Boolean)


85
86
87
# File 'lib/pry/wrapped_module.rb', line 85

def singleton_class?
  wrapped != wrapped.ancestors.first
end

- (Object) singleton_instance

Get the instance associated with this singleton class.

Returns:

Raises:

  • ArgumentError: tried to get instance of non singleton class



94
95
96
97
98
99
100
101
102
# File 'lib/pry/wrapped_module.rb', line 94

def singleton_instance
  raise ArgumentError, "tried to get instance of non singleton class" unless singleton_class?

  if Helpers::BaseHelpers.jruby?
    wrapped.to_java.attached
  else
    @singleton_instance ||= ObjectSpace.each_object(wrapped).detect{ |x| (class << x; self; end) == wrapped }
  end
end

- (Object) source

Retrieve the source for the module.



147
148
149
# File 'lib/pry/wrapped_module.rb', line 147

def source
  @source ||= source_for_candidate(0)
end

- (Object) source_file



158
159
160
161
162
163
164
165
# File 'lib/pry/wrapped_module.rb', line 158

def source_file
  if yard_docs?
    from_yard = YARD::Registry.at(name)
    from_yard.file
  else
    source_file_for_candidate(0)
  end
end

- (Object) source_file_for_candidate(idx)



171
172
173
# File 'lib/pry/wrapped_module.rb', line 171

def source_file_for_candidate(idx)
  Array(module_source_location_for_candidate(idx)).first
end

- (Object) source_for_candidate(idx)

Raises:



151
152
153
154
155
156
# File 'lib/pry/wrapped_module.rb', line 151

def source_for_candidate(idx)
  file, line = module_source_location_for_candidate(idx)
  raise CommandError, "Could not locate source for #{wrapped}!" if file.nil?

  strip_leading_whitespace(Pry::Code.retrieve_complete_expression_from(lines_for_file(file)[(line - 1)..-1]))
end

- (Object) source_line



167
168
169
# File 'lib/pry/wrapped_module.rb', line 167

def source_line
  source_line_for_candidate(0)
end

- (Object) source_line_for_candidate(idx)



175
176
177
# File 'lib/pry/wrapped_module.rb', line 175

def source_line_for_candidate(idx)
  Array(module_source_location_for_candidate(idx)).last
end

- (Array<String, Fixnum>) source_location

Retrieve the source location of a module. Return value is in same format as Method#source_location. If the source location cannot be found this method returns nil.

Parameters:

  • mod (Module)

    The module (or class).

Returns:

  • (Array<String, Fixnum>)

    The source location of the module (or class).



186
187
188
189
190
# File 'lib/pry/wrapped_module.rb', line 186

def source_location
  @source_location ||= module_source_location_for_candidate(0)
rescue Pry::RescuableException
  nil
end

- (Boolean) yard_docs?

Returns:

  • (Boolean)


113
114
115
# File 'lib/pry/wrapped_module.rb', line 113

def yard_docs?
  !!(defined?(YARD) && YARD::Registry.at(name))
end