class Pry::Command::FindMethod
def additional_info(header, method)
Return the matched lines of method source if `-c` is given or ""
def additional_info(header, method) if opts.content? ": " + colorize_code(matched_method_lines(header, method)) else "" end end
def content_search(namespace)
-
Module
() -- The namespace to search
def content_search(namespace) search_all_methods(namespace) do |meth| begin meth.source =~ pattern rescue RescuableException false end end end
def matched_method_lines(header, method)
def matched_method_lines(header, method) method.source.split(/\n/).select {|x| x =~ pattern }.join("\n#{' ' * header.length}") end
def name_search(namespace)
-
Module
() -- The namespace to search
def name_search(namespace) search_all_methods(namespace) do |meth| meth.name =~ pattern end end
def options(opti)
def options(opti) opti.on :n, :name, "Search for a method by name" opti.on :c, :content, "Search for a method based on content in Regex form" end
def pattern
-
(Regexp)
- The pattern to search for.
def pattern @pattern ||= ::Regexp.new args[0] end
def print_matches(matches)
-
Array
(Method
) --
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
def print_matches_for_class(klass, grouped)
def print_matches_for_class(klass, grouped) output.puts text.bold(klass.name) grouped[klass].each do |method| header = method.name_with_owner output.puts header + additional_info(header, method) end end
def process
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
def recurse_namespace(klass, done={}, &block)
- Yieldparam: klazz - Each class/module in the namespace.
Parameters:
-
Hash
(Module, Boolean
) -- The namespaces we've already visited (private) -
Module
() -- The namespace in which to start the search.
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
def search_all_methods(namespace)
- Yieldreturn: - Boolean
Other tags:
- Yieldparam: Method - The method to test
Parameters:
-
Module
() -- The namespace in which to search.
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
def search_class
instance, return its class. If no search object is given
We only search classes, so if the search object is an
The class to search for methods.
def search_class klass = if args[1] target.eval(args[1]) else target_self end klass.is_a?(Module) ? klass : klass.class end
def setup
def setup require 'ruby18_source_location' if mri_18? end
def show_search_results(matches)
-
matches
(Array
) --
def show_search_results(matches) if matches.empty? output.puts text.bold("No Methods Matched") else print_matches(matches) end end