make show-source code more readable

This commit is contained in:
John Mair 2012-12-20 20:46:33 +01:00
parent 47d30a139c
commit eda2c73afb
2 changed files with 27 additions and 8 deletions

View File

@ -37,21 +37,29 @@ class Pry
end
def process
obj_name = args.empty? ? nil : args.join(" ")
o = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !o
code_object = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
if opts[:a]
result = all_modules(o)
if !code_object
raise Pry::CommandError, "Couldn't locate #{obj_name}!"
end
if code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
# show all monkey patches for a module
result = source_for_all_module_candidates(code_object)
else
result = header(o)
result << Code.new(o.source, start_line_for(o)).
# show the source for a specific code object
result = header(code_object)
result << Code.new(code_object.source, start_line_for(code_object)).
with_line_numbers(use_line_numbers?).to_s
end
stagger_output result
end
def obj_name
@obj_name ||= args.empty? ? nil : args.join(" ")
end
# we need this helper as some Pry::Method objects can wrap Procs
# @return [Boolean]
def real_method_object?(code_object)
@ -76,7 +84,7 @@ class Pry
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} #{code_object.source.lines.count}\n\n"
end
def all_modules(mod)
def source_for_all_module_candidates(mod)
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
mod.number_of_candidates.times do |v|
candidate = mod.candidate(v)

View File

@ -435,6 +435,17 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
result.should =~ /def class_eval_method/
end
it 'should ignore -a when object is not a module' do
TestClassForShowSourceClassEval.class_eval do
def class_eval_method
:bing
end
end
result = pry_eval('show-source TestClassForShowSourceClassEval#class_eval_method -a')
result.should =~ /bing/
end
it 'should show the source for an instance_eval-based monkeypatch' do
TestClassForShowSourceInstanceEval.instance_eval do
def instance_eval_method