Permalink
Browse files

refactored

  • Loading branch information...
1 parent 5a60bd5 commit 0957c1379f40d6294dabb909eb286887da2242de @banister committed Mar 13, 2011
Showing with 68 additions and 47 deletions.
  1. +25 −19 lib/pry-doc.rb
  2. +43 −28 test/test.rb
View
@@ -14,62 +14,68 @@
class Pry
module MethodInfo
-
@doc_cache = {}
class << self; attr_reader :doc_cache; end
+ # Convert a method object into the `Class#method` string notation.
+ #@param [Method, UnboundMethod] meth
+ #@return [String] The method in string receiver notation.
def self.receiver_notation_for(meth)
if meth.owner.name
- #klassinst, klass = '#', meth.owner.name
"#{meth.owner.name}##{meth.name}"
else
- #klassinst, klass = '.', meth.owner.to_s[/#<.+?:(.+?)>/, 1]
"#{meth.owner.to_s[/#<.+?:(.+?)>/, 1]}.#{meth.name}"
end
end
- # Retrieve the YARD object that contains the method data.
+ # Check whether the file containing the method is already cached.
# @param [Method, UnboundMethod] meth The method object.
- # @return [YARD::CodeObjects::MethodObject] The YARD data for the method.
- def self.yard_object_for(meth)
- return nil if is_eval_method?(meth)
- cache(meth)
-
+ # @return [Boolean] Whether the method is cached.
+ def self.cached?(meth)
+ !!registry_lookup(meth)
+ end
+
+ def self.registry_lookup(meth)
obj = YARD::Registry.at(receiver_notation_for(meth))
- # Stupidly, YARD thinks that some methods are on Object when
+ # YARD thinks that some methods are on Object when
# they're actually on Kernel; so try again on Object if Kernel fails.
if obj.nil? && meth.owner == Kernel
obj = YARD::Registry.at("Object##{meth.name}")
end
-
obj
end
+ # Retrieve the YARD object that contains the method data.
+ # @param [Method, UnboundMethod] meth The method object.
+ # @return [YARD::CodeObjects::MethodObject] The YARD data for the method.
+ def self.yard_object_for(meth)
+ cache(meth)
+ registry_lookup(meth)
+ end
+
def self.is_eval_method?(meth)
file, _ = meth.source_location
-
if file =~ /(\(.*\))|<.*>/
- return true
+ true
+ else
+ false
end
- false
end
- # Cache the file that holds the method or return true if file is
- # already cached. Return false if the method cannot be cached -
+ # Cache the file that holds the method or return immediately if file is
+ # already cached. Return if the method cannot be cached -
# i.e is a C method.
# @param [Method, UnboundMethod] meth The method object.
- # @return [Boolean] Whether the cache was successful.
def self.cache(meth)
file, _ = meth.source_location
return if !file
return if is_eval_method?(meth)
- return if doc_cache[File.expand_path(file)]
+ return if cached?(meth)
log.enter_level(Logger::FATAL) do
YARD.parse(file)
end
- doc_cache[File.expand_path(file)] = true
end
end
end
View
@@ -2,52 +2,67 @@
require 'rubygems'
require "#{direc}/../lib/pry-doc"
+require "#{direc}/test_helper"
require 'bacon'
+require 'set'
require 'pry'
puts "Testing pry-doc version #{PryDoc::VERSION}..."
puts "Ruby version: #{RUBY_VERSION}"
describe PryDoc do
- it 'should look up core (C) methods' do
- obj = Pry::MethodInfo.yard_object_for(method(:puts))
- obj.source.should.not == nil
- end
-
- it 'should look up core (C) instance methods' do
- Module.module_eval do
- obj = Pry::MethodInfo.yard_object_for(instance_method(:include))
+ describe "core C methods" do
+ it 'should look up core (C) methods' do
+ obj = Pry::MethodInfo.yard_object_for(method(:puts))
obj.source.should.not == nil
end
- end
- it 'should return nil for eval methods' do
- eval("def hello; end")
-
- obj = Pry::MethodInfo.yard_object_for(method(:hello))
- obj.should == nil
+ it 'should look up core (C) instance methods' do
+ Module.module_eval do
+ obj = Pry::MethodInfo.yard_object_for(instance_method(:include))
+ obj.source.should.not == nil
+ end
+ end
+ end
+
+ describe "eval methods" do
+ it 'should return nil for eval methods' do
+ eval("def hello; end")
+
+ obj = Pry::MethodInfo.yard_object_for(method(:hello))
+ obj.should == nil
+ end
end
- it 'should look up ruby methods' do
- c = Class.new do
- def message
- end
+ describe "pure ruby methods" do
+ it 'should look up ruby methods' do
+ obj = Pry::MethodInfo.yard_object_for(C.new.method(:message))
+ obj.should.not == nil
end
- obj = Pry::MethodInfo.yard_object_for(c.new.method(:message))
- obj.should.not == nil
- end
+ it 'should look up ruby instance methods' do
- it 'should look up ruby instance methods' do
- c = Class.new do
- def message
+ g = Class.new do
+ def yo; end
end
+
+ obj = Pry::MethodInfo.yard_object_for(C.instance_method(:message))
+ obj.should.not == nil
end
+ end
- binding.pry
-
- obj = Pry::MethodInfo.yard_object_for(c.instance_method(:message))
- obj.should.not == nil
+ describe "Ruby stdlib methods" do
+ it "should look up ruby stdlib method" do
+ obj = Pry::MethodInfo.yard_object_for(Set.instance_method(:union))
+ obj.should.not == nil
+ end
end
+
+ describe "C stdlib methods" do
+ it "should return nil for C stdlib method" do
+ obj = Pry::MethodInfo.yard_object_for(Readline.method(:readline))
+ obj.should == nil
+ end
+ end
end

0 comments on commit 0957c13

Please sign in to comment.