mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
supercharged get_method_object() method (and show-method, show-doc, etc commands), started tests for command helpers (test_command_helpers.rb)
This commit is contained in:
parent
635acbdc45
commit
f765df9a45
3 changed files with 19 additions and 56 deletions
|
@ -30,15 +30,6 @@ e.g show-doc hello_method
|
|||
next if opts.help?
|
||||
|
||||
meth_name = args.shift
|
||||
if meth_name
|
||||
if meth_name =~ /\A([^\.\#]+)[\.\#](.+)\z/ && !opts.context?
|
||||
context, meth_name = $1, $2
|
||||
target = Pry.binding_for(target.eval(context))
|
||||
end
|
||||
else
|
||||
meth_name = meth_name_from_binding(target)
|
||||
end
|
||||
|
||||
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
|
||||
output.puts "Invalid method name: #{meth_name}. Type `show-doc --help` for help"
|
||||
next
|
||||
|
@ -48,11 +39,8 @@ e.g show-doc hello_method
|
|||
next if !doc
|
||||
|
||||
next output.puts("No documentation found.") if doc.empty?
|
||||
|
||||
doc = process_comment_markup(doc, code_type)
|
||||
|
||||
output.puts make_header(meth, code_type, doc)
|
||||
|
||||
render_output(opts.flood?, false, doc)
|
||||
doc
|
||||
end
|
||||
|
@ -82,15 +70,6 @@ e.g: stat hello_method
|
|||
next if opts.help?
|
||||
|
||||
meth_name = args.shift
|
||||
if meth_name
|
||||
if meth_name =~ /\A([^\.\#]+)[\.\#](.+)\z/ && !opts.context?
|
||||
context, meth_name = $1, $2
|
||||
target = Pry.binding_for(target.eval(context))
|
||||
end
|
||||
else
|
||||
meth_name = meth_name_from_binding(target)
|
||||
end
|
||||
|
||||
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
|
||||
output.puts "Invalid method name: #{meth_name}. Type `stat --help` for help"
|
||||
next
|
||||
|
@ -139,15 +118,6 @@ e.g: gist -d my_method
|
|||
# This needs to be extracted into its own method as it's shared
|
||||
# by show-method and show-doc and stat commands
|
||||
meth_name = args.shift
|
||||
if meth_name
|
||||
if meth_name =~ /\A([^\.\#]+)[\.\#](.+)\z/
|
||||
context, meth_name = $1, $2
|
||||
target = Pry.binding_for(target.eval(context))
|
||||
end
|
||||
else
|
||||
meth_name = meth_name_from_binding(target)
|
||||
end
|
||||
|
||||
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
|
||||
output.puts "Invalid method name: #{meth_name}. Type `gist-method --help` for help"
|
||||
next
|
||||
|
@ -169,7 +139,6 @@ e.g: gist -d my_method
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -27,15 +27,6 @@ e.g: show-method hello_method
|
|||
next if opts.help?
|
||||
|
||||
meth_name = args.shift
|
||||
if meth_name
|
||||
if meth_name =~ /\A([^\.\#]+)[\.\#](.+)\z/ && !opts.context?
|
||||
context, meth_name = $1, $2
|
||||
target = Pry.binding_for(target.eval(context))
|
||||
end
|
||||
else
|
||||
meth_name = meth_name_from_binding(target)
|
||||
end
|
||||
|
||||
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
|
||||
output.puts "Invalid method name: #{meth_name}. Type `show-method --help` for help"
|
||||
next
|
||||
|
@ -141,15 +132,6 @@ e.g: edit-method hello_method
|
|||
next if opts.help?
|
||||
|
||||
meth_name = args.shift
|
||||
if meth_name
|
||||
if meth_name =~ /\A([^\.\#]+)[\.\#](.+)\z/ && !opts.context?
|
||||
context, meth_name = $1, $2
|
||||
target = Pry.binding_for(target.eval(context))
|
||||
end
|
||||
else
|
||||
meth_name = meth_name_from_binding(target)
|
||||
end
|
||||
|
||||
if (meth = get_method_object(meth_name, target, opts.to_hash(true))).nil?
|
||||
output.puts "Invalid method name: #{meth_name}."
|
||||
next
|
||||
|
|
|
@ -134,23 +134,35 @@ class Pry
|
|||
end
|
||||
|
||||
def get_method_object(meth_name, target, options)
|
||||
if meth_name
|
||||
if meth_name =~ /(\S+)\#(\S+)\Z/
|
||||
context, meth_name = $1, $2
|
||||
target = Pry.binding_for(target.eval(context))
|
||||
options["instance-methods"] = true
|
||||
options[:methods] = false
|
||||
elsif meth_name =~ /(\S+)\.(\S+)\Z/
|
||||
context, meth_name = $1, $2
|
||||
target = Pry.binding_for(target.eval(context))
|
||||
options["instance-methods"] = false
|
||||
options[:methods] = true
|
||||
end
|
||||
else
|
||||
meth_name = meth_name_from_binding(target)
|
||||
end
|
||||
|
||||
if !meth_name
|
||||
return nil
|
||||
end
|
||||
|
||||
if options["instance-methods"]
|
||||
target.eval("instance_method(:#{meth_name})")
|
||||
target.eval("instance_method(:#{meth_name})") rescue nil
|
||||
elsif options[:methods]
|
||||
target.eval("method(:#{meth_name})")
|
||||
target.eval("method(:#{meth_name})") rescue nil
|
||||
else
|
||||
begin
|
||||
target.eval("instance_method(:#{meth_name})")
|
||||
rescue
|
||||
begin
|
||||
target.eval("method(:#{meth_name})")
|
||||
rescue
|
||||
return nil
|
||||
end
|
||||
target.eval("method(:#{meth_name})") rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue