From c1ae63446f0fffc6f5ca1a7aff06c5e546b8a1ff Mon Sep 17 00:00:00 2001 From: John Mair Date: Thu, 27 Dec 2012 13:33:56 +0100 Subject: [PATCH] define .source, .doc, etc on Pry::ClassCommand This is so that Pry::CodeObject can return Pry::ClassCommand objects directly, this is impt as we want slightly different behaviour than just Pry::WrappedModule(command_class), notably .doc should return command help rather than comments above the command's class definition. --- lib/pry/code_object.rb | 4 +++- lib/pry/command.rb | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/pry/code_object.rb b/lib/pry/code_object.rb index 6575a8d7..078fa0b7 100644 --- a/lib/pry/code_object.rb +++ b/lib/pry/code_object.rb @@ -70,7 +70,9 @@ class Pry if str !~ /\S#\S/ && target.eval("defined? #{str} ") =~ /variable|constant/ obj = target.eval(str) - if obj.respond_to?(:source_location) + # restrict to only objects we KNOW for sure support the full API + # Do NOT support just any object that responds to source_location + if [::Proc, ::Method, ::UnboundMethod].any? { |o| obj.is_a?(o) } Pry::Method(obj) elsif !obj.is_a?(Module) Pry::WrappedModule(obj.class) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index d30e8b8c..45ea5100 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -506,6 +506,30 @@ class Pry # gems your command needs to run, or to set up state. class ClassCommand < Command + class << self + def source + Pry::WrappedModule(self).source + end + + def doc + new.help + end + + def source_location + Pry::WrappedModule(self).source_location + end + + def source_file + Pry::WrappedModule(self).source_file + end + alias_method :file, :source_file + + def source_line + Pry::WrappedModule(self).source_line + end + alias_method :line, :source_line + end + # The class that couples together subcommands and top-level options (that # are known as "default" options). The explicitly defined instance methods # of this class provide the coupling with default options of a