Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DRYed up show-doc/show-source code

* also made Pry::WrappedModule.from_str more robust
  • Loading branch information...
commit 5993c4521522c4cf5b55de42f8f43a7c3db4f9ff 1 parent c32f009
@banister banister authored
View
42 lib/pry/default_commands/introspection.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,17 +226,17 @@ 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 = ""
@@ -244,7 +244,9 @@ def normal_module(mod)
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
12 lib/pry/wrapped_module.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.