Refactor the way how `ls` displays self methods

This commit is contained in:
Kyrylo Silin 2013-12-01 01:34:14 +02:00
parent 94db19efc1
commit b2f32f3088
6 changed files with 56 additions and 20 deletions

View File

@ -15,7 +15,7 @@ class Pry
end
def correct_opts?
super || (!@has_any_opts && Module === @interrogatee)
super || (!@has_any_opts && interrogating_a_module?)
end
def output_self

View File

@ -7,7 +7,13 @@ class Pry
end
def regexp
proc { |x| x.grep(@grep_regexp) }
proc { |x|
if x.instance_of?(Array)
x.grep(@grep_regexp)
else
x =~ @grep_regexp
end
}
end
end

View File

@ -1,7 +1,11 @@
module Pry::Command::Ls::Interrogateable
def interrogating_a_module?
Module === @interrogatee
end
def interrogatee_mod
if Module === @interrogatee
if interrogating_a_module?
@interrogatee
else
class << @interrogatee

View File

@ -29,27 +29,12 @@ class Pry
# Reverse the resolution order so that the most useful information
# appears right by the prompt.
resolution_order.take_while(&below_ceiling).reverse.map do |klass|
methods_here = grep.regexp[format((methods[klass] || []))]
methods_here = (methods[klass] || []).select { |m| grep.regexp[m.name] }
heading = "#{ Pry::WrappedModule.new(klass).method_prefix }methods"
output_section(heading, methods_here)
output_section(heading, format(methods_here))
end.join('')
end
# Format and colourise a list of methods.
def format(methods)
methods.sort_by(&:name).map do |method|
if method.name == 'method_missing'
color(:method_missing, 'method_missing')
elsif method.visibility == :private
color(:private_method, method.name)
elsif method.visibility == :protected
color(:protected_method, method.name)
else
color(:public_method, method.name)
end
end
end
end
end
end

View File

@ -37,4 +37,18 @@ module Pry::Command::Ls::MethodsHelper
lambda { |klass| !ceiling.include?(klass) }
end
def format(methods)
methods.sort_by(&:name).map do |method|
if method.name == 'method_missing'
color(:method_missing, 'method_missing')
elsif method.visibility == :private
color(:private_method, method.name)
elsif method.visibility == :protected
color(:protected_method, method.name)
else
color(:public_method, method.name)
end
end
end
end

View File

@ -0,0 +1,27 @@
class Pry
class Command::Ls < Pry::ClassCommand
class SelfMethods < Pry::Command::Ls::Formatter
include Pry::Command::Ls::Interrogateable
include Pry::Command::Ls::MethodsHelper
def initialize(interrogatee, has_any_opts, opts)
@interrogatee = interrogatee
@has_any_opts = has_any_opts
end
def correct_opts?
!@has_any_opts && interrogating_a_module?
end
def output_self
methods = all_methods(true).select do |m|
m.owner == @interrogatee && grep.regexp[m.name]
end
heading = "#{ Pry::WrappedModule.new(@interrogatee).method_prefix }methods"
output_section(heading, format(methods))
end
end
end
end