Permalink
Browse files

DRYed up show-doc/show-source code

* also made Pry::WrappedModule.from_str more robust
  • Loading branch information...
banister committed Apr 18, 2012
1 parent c32f009 commit 5993c4521522c4cf5b55de42f8f43a7c3db4f9ff
Showing with 30 additions and 24 deletions.
  1. +22 −20 lib/pry/default_commands/introspection.rb
  2. +8 −4 lib/pry/wrapped_module.rb
@@ -5,12 +5,10 @@ module DefaultCommands
# For show-doc and show-source
module ModuleIntrospectionHelpers
+ attr_accessor :module_object
+
def module?(name)
- begin
- kind = target.eval("defined?(#{name})")
- rescue Pry::RescuableException
- end
- !!(kind == "constant" && target.eval(name).is_a?(Module))
+ self.module_object = Pry::WrappedModule.from_str(name, target)
end
def method?
@@ -21,7 +19,7 @@ def method?
def process(name)
if module?(name)
- code_or_doc = process_module(name)
+ code_or_doc = process_module
else method?
code_or_doc = process_method
end
@@ -66,17 +64,17 @@ def options(opt)
opt.on :a, :all, "Show docs for all definitions and monkeypatches of the module (modules only)"
end
- def process_module(name)
- mod = Pry::WrappedModule.from_str(name, target)
-
+ def process_module
if opts.present?(:all)
- all_modules(mod)
+ all_modules
else
- normal_module(mod)
+ normal_module
end
end
- def normal_module(mod)
+ def normal_module
+ mod = module_object
+
# source_file reveals the underlying .c file in case of core
# classes on MRI. This is different to source_location, which
# will return nil.
@@ -101,7 +99,9 @@ def normal_module(mod)
end
end
- def all_modules(mod)
+ def all_modules
+ mod = module_object
+
doc = ""
doc << "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
mod.number_of_candidates.times do |v|
@@ -226,25 +226,27 @@ def process_method
with_line_numbers(use_line_numbers?).to_s
end
- def process_module(name)
- mod = Pry::WrappedModule.from_str(name, target)
-
+ def process_module
if opts.present?(:all)
- all_modules(mod)
+ all_modules
else
- normal_module(mod)
+ normal_module
end
end
- def normal_module(mod)
+ def normal_module
+ mod = module_object
+
file_name, line = mod.source_location
set_file_and_dir_locals(file_name)
code = ""
code << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n\n"
code << Code.from_module(mod, module_start_line(mod)).with_line_numbers(use_line_numbers?)
end
- def all_modules(mod)
+ def all_modules
+ mod = module_object
+
code = ""
code << "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
mod.number_of_candidates.times do |v|
View
@@ -22,13 +22,17 @@ class WrappedModule
# Convert a string to a module.
#
# @param [String] mod_name
+ # @param [Binding] target The binding where the lookup takes place.
# @return [Module, nil] The module or `nil` (if conversion failed).
# @example
# Pry::WrappedModule.from_str("Pry::Code")
- def self.from_str(mod_name, binding=TOPLEVEL_BINDING)
- mod = binding.eval(mod_name)
- if mod.is_a?(Module)
- Pry::WrappedModule.new(mod)
+ def self.from_str(mod_name, target=TOPLEVEL_BINDING)
+ kind = target.eval("defined?(#{mod_name})")
+
+ # if we dont limit it to constants then from_str could end up
+ # executing methods which is not good, i.e `show-source pry`
+ if (kind == "constant" && target.eval(mod_name).is_a?(Module))
+ Pry::WrappedModule.new(target.eval(mod_name))
else
nil
end

0 comments on commit 5993c45

Please sign in to comment.