1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

find-method refactor: 'pattern' becomes a method

This commit is contained in:
John Mair 2013-01-03 21:59:13 +01:00
parent a42eec8853
commit 1dda70c695

View file

@ -32,28 +32,31 @@ class Pry
def process def process
return if args.size < 1 return if args.size < 1
klass = search_class klass = search_class
pattern = ::Regexp.new args[0]
matches = if opts.content? matches = if opts.content?
content_search(pattern, klass) content_search(klass)
else else
name_search(pattern, klass) name_search(klass)
end end
show_search_results(matches, pattern) show_search_results(matches)
end end
private private
# @return [Regexp] The pattern to search for.
def pattern
@pattern ||= ::Regexp.new args[0]
end
# Output the result of the search. # Output the result of the search.
# #
# @param [Array] matches # @param [Array] matches
# @param [Regex] pattern def show_search_results(matches)
def show_search_results(matches, pattern)
if matches.empty? if matches.empty?
output.puts text.bold("No Methods Matched") output.puts text.bold("No Methods Matched")
else else
print_matches(matches, pattern) print_matches(matches)
end end
end end
@ -74,35 +77,35 @@ class Pry
# pretty-print a list of matching methods. # pretty-print a list of matching methods.
# #
# @param Array[Method] # @param Array[Method]
def print_matches(matches, pattern) def print_matches(matches)
grouped = matches.group_by(&:owner) grouped = matches.group_by(&:owner)
order = grouped.keys.sort_by{ |x| x.name || x.to_s } order = grouped.keys.sort_by{ |x| x.name || x.to_s }
order.each do |klass| order.each do |klass|
print_matches_for_class(klass, grouped, pattern) print_matches_for_class(klass, grouped)
end end
end end
# Print matched methods for a class # Print matched methods for a class
def print_matches_for_class(klass, grouped, pattern) def print_matches_for_class(klass, grouped)
output.puts text.bold(klass.name) output.puts text.bold(klass.name)
grouped[klass].each do |method| grouped[klass].each do |method|
header = method.name_with_owner header = method.name_with_owner
output.puts header + additional_info(header, method, pattern) output.puts header + additional_info(header, method)
end end
end end
# Return the matched lines of method source if `-c` is given or "" # Return the matched lines of method source if `-c` is given or ""
# if `-c` was not given # if `-c` was not given
def additional_info(header, method, pattern) def additional_info(header, method)
if opts.content? if opts.content?
": " + colorize_code(matched_method_lines(header, method, pattern)) ": " + colorize_code(matched_method_lines(header, method))
else else
"" ""
end end
end end
def matched_method_lines(header, method, pattern) def matched_method_lines(header, method)
method.source.split(/\n/).select {|x| x =~ pattern }.join("\n#{' ' * header.length}") method.source.split(/\n/).select {|x| x =~ pattern }.join("\n#{' ' * header.length}")
end end
@ -160,27 +163,25 @@ class Pry
# Search for all methods with a name that matches the given regex # Search for all methods with a name that matches the given regex
# within a namespace. # within a namespace.
# #
# @param Regex The regex to search for
# @param Module The namespace to search # @param Module The namespace to search
# @return Array[Method] # @return Array[Method]
# #
def name_search(regex, namespace) def name_search(namespace)
search_all_methods(namespace) do |meth| search_all_methods(namespace) do |meth|
meth.name =~ regex meth.name =~ pattern
end end
end end
# Search for all methods who's implementation matches the given regex # Search for all methods who's implementation matches the given regex
# within a namespace. # within a namespace.
# #
# @param Regex The regex to search for
# @param Module The namespace to search # @param Module The namespace to search
# @return Array[Method] # @return Array[Method]
# #
def content_search(regex, namespace) def content_search(namespace)
search_all_methods(namespace) do |meth| search_all_methods(namespace) do |meth|
begin begin
meth.source =~ regex meth.source =~ pattern
rescue RescuableException rescue RescuableException
false false
end end