Class: Pry::Command::FindMethod

Inherits:
Pry::ClassCommand show all
Extended by:
Helpers::BaseHelpers
Defined in:
lib/pry/commands/find_method.rb

Constant Summary

Constants inherited from Pry::Command

VOID_VALUE

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary

Attributes inherited from Pry::ClassCommand

#args, #opts

Attributes inherited from Pry::Command

#_pry_, #arg_string, #captures, #command_block, #command_set, #context, #eval_string, #hooks, #output, #target

Instance Method Summary collapse

Methods included from Helpers::BaseHelpers

colorize_code, command_dependencies_met?, find_command, heading, highlight, not_a_real_file?, safe_send, silence_warnings, stagger_output, use_ansi_codes?

Methods included from Platform

#jruby?, #jruby_19?, #known_engines, #linux?, #mac_osx?, #mri?, #mri_19?, #mri_2?, #rbx?, #windows?, #windows_ansi?

Methods inherited from Pry::ClassCommand

#call, #complete, doc, #help, inherited, #setup, #slop, source, source_file, source_line, source_location, source_object, #subcommands

Methods inherited from Pry::Command

#after_hooks, banner, #before_hooks, #block, #call_safely, #call_with_hooks, #check_for_command_collision, command_name, #command_name, #command_options, command_regex, #commands, #complete, convert_to_regex, #correct_arg_arity, default_options, #dependencies_met?, #description, doc, #find_hooks, group, hooks, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, options, #pass_block, #process_line, #run, source, #source, source_file, source_line, source_location, #source_location, #state, subclass, #target_self, #text, #tokenize, #use_unpatched_symbol, #void

Methods included from Pry::CodeObject::Helpers

#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?

Methods included from Helpers::DocumentationHelpers

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

Methods included from Helpers::Text

#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers

Methods included from Helpers::CommandHelpers

#absolute_index_number, absolute_index_number, absolute_index_range, #absolute_index_range, command_error, #command_error, #get_method_or_raise, get_method_or_raise, internal_binding?, #internal_binding?, one_index_number, #one_index_number, one_index_range, #one_index_range, #one_index_range_or_number, one_index_range_or_number, #restrict_to_lines, restrict_to_lines, #set_file_and_dir_locals, set_file_and_dir_locals, #temp_file, temp_file, #unindent, unindent

Methods included from Helpers::OptionsHelpers

method_object, #method_object, method_options, #method_options

Constructor Details

This class inherits a constructor from Pry::Command

Instance Method Details

#additional_info(header, method) ⇒ Object (private)

Return the matched lines of method source if -c is given or "" if -c was not given


100
101
102
103
104
105
106
# File 'lib/pry/commands/find_method.rb', line 100

def additional_info(header, method)
  if opts.content?
    ": " << colorize_code(matched_method_lines(header, method))
  else
    ""
  end
end

#content_search(namespace) ⇒ Array<Method> (private)

Search for all methods who's implementation matches the given regex within a namespace.


181
182
183
184
185
186
187
188
189
# File 'lib/pry/commands/find_method.rb', line 181

def content_search(namespace)
  search_all_methods(namespace) do |meth|
    begin
      meth.source =~ pattern
    rescue RescuableException
      false
    end
  end
end

#matched_method_lines(header, method) ⇒ Object (private)


108
109
110
# File 'lib/pry/commands/find_method.rb', line 108

def matched_method_lines(header, method)
  method.source.split(/\n/).select {|x| x =~ pattern }.join("\n#{' ' * header.length}")
end

#name_search(namespace) ⇒ Array<Method> (private)

Search for all methods with a name that matches the given regex within a namespace.


169
170
171
172
173
# File 'lib/pry/commands/find_method.rb', line 169

def name_search(namespace)
  search_all_methods(namespace) do |meth|
    meth.name =~ pattern
  end
end

#options(opt) ⇒ Object


27
28
29
30
# File 'lib/pry/commands/find_method.rb', line 27

def options(opt)
  opt.on :n, :name,    "Search for a method by name"
  opt.on :c, :content, "Search for a method based on content in Regex form"
end

#patternRegexp (private)


48
49
50
# File 'lib/pry/commands/find_method.rb', line 48

def pattern
  @pattern ||= ::Regexp.new args[0]
end

pretty-print a list of matching methods.


80
81
82
83
84
85
86
87
# File 'lib/pry/commands/find_method.rb', line 80

def print_matches(matches)
  grouped = matches.group_by(&:owner)
  order = grouped.keys.sort_by{ |x| x.name || x.to_s }

  order.each do |klass|
    print_matches_for_class(klass, grouped)
  end
end

Print matched methods for a class


90
91
92
93
94
95
96
# File 'lib/pry/commands/find_method.rb', line 90

def print_matches_for_class(klass, grouped)
  output.puts bold(klass.name)
  grouped[klass].each do |method|
    header = method.name_with_owner
    output.puts header + additional_info(header, method)
  end
end

#processObject


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pry/commands/find_method.rb', line 32

def process
  return if args.size < 1
  klass = search_class

  matches = if opts.content?
    content_search(klass)
  else
    name_search(klass)
  end

  show_search_results(matches)
end

#recurse_namespace(klass, done = {}) {|klass| ... } ⇒ Object (private)

Run the given block against every constant in the provided namespace.

Yield Parameters:

  • klass

    Each class/module in the namespace.


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/pry/commands/find_method.rb', line 118

def recurse_namespace(klass, done={}, &block)
  return if !(Module === klass) || done[klass]

  done[klass] = true

  yield klass

  klass.constants.each do |name|
    next if klass.autoload?(name)
    begin
      const = klass.const_get(name)
    rescue RescuableException
      # constant loading is an inexact science at the best of times,
      # this often happens when a constant was .autoload? but someone
      # tried to load it. It's now not .autoload? but will still raise
      # a NameError when you access it.
    else
      recurse_namespace(const, done, &block)
    end
  end
end

#search_all_methods(namespace) {|method| ... } ⇒ Array<Method> (private)

Gather all the methods in a namespace that pass the given block.

Yield Parameters:

  • method (Method)

    The method to test

Yield Returns:

  • (Boolean)

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pry/commands/find_method.rb', line 147

def search_all_methods(namespace)
  done = Hash.new{ |h,k| h[k] = {} }
  matches = []

  recurse_namespace(namespace) do |klass|
    (Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)).each do |method|
      next if done[method.owner][method.name]
      done[method.owner][method.name] = true

      matches << method if yield method
    end
  end

  matches
end

#search_classObject (private)

The class to search for methods. We only search classes, so if the search object is an instance, return its class. If no search object is given search target_self.


67
68
69
70
71
72
73
74
75
# File 'lib/pry/commands/find_method.rb', line 67

def search_class
  klass = if args[1]
            target.eval(args[1])
          else
            target_self
          end

  klass.is_a?(Module) ? klass : klass.class
end

#show_search_results(matches) ⇒ Object (private)

Output the result of the search.


55
56
57
58
59
60
61
# File 'lib/pry/commands/find_method.rb', line 55

def show_search_results(matches)
  if matches.empty?
    output.puts bold("No Methods Matched")
  else
    print_matches(matches)
  end
end