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::DocumentationHelpers

Helpers::DocumentationHelpers::YARD_TAGS

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary

Attributes inherited from Pry::ClassCommand

#args, #opts

Attributes inherited from Pry::Command

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

Instance Method Summary collapse

Methods included from Helpers::BaseHelpers

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

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

#_pry_, #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, default_options, #description, doc, #find_hooks, group, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, #normalize_method_args, options, #pass_block, #process_line, #run, #source, source, source_file, source_line, #state, state, subclass, #target_self, #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_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #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.

Parameters:

  • namespace (Module)

    The namespace to search

Returns:


186
187
188
189
190
191
192
193
194
# File 'lib/pry/commands/find_method.rb', line 186

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
111
112
# 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.

Parameters:

  • namespace (Module)

    The namespace to search

Returns:


174
175
176
177
178
# File 'lib/pry/commands/find_method.rb', line 174

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

#options(opt) ⇒ Object


31
32
33
34
# File 'lib/pry/commands/find_method.rb', line 31

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)

Returns The pattern to search for.

Returns:

  • (Regexp)

    The pattern to search for.


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.

Parameters:


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


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

def process
  return if args.empty?

  klass = search_class

  matches = opts.content? ? content_search(klass) : name_search(klass)
  show_search_results(matches)
end

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

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

Parameters:

  • klass (Module)

    The namespace in which to start the search.

  • done (Hash<Module,Boolean>) (defaults to: {})

    The namespaces we've already visited (private)

Yield Parameters:

  • klass

    Each class/module in the namespace.


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

def recurse_namespace(klass, done = {}, &block)
  return if !klass.is_a?(Module) || 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 # rubocop:disable Lint/HandleExceptions
      # 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.

Parameters:

  • namespace (Module)

    The namespace in which to search.

Yield Parameters:

  • method (Method)

    The method to test

Yield Returns:

  • (Boolean)

Returns:


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/pry/commands/find_method.rb', line 150

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

  recurse_namespace(namespace) do |klass|
    methods = Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)
    methods.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.

Parameters:

  • matches (Array)

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