Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/method_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ def class_comment
class_inst_or_module = class_inst_or_module.class
end

unless const_name
raise SourceNotFoundError, "Could not locate source for an anonymous class or module!"
end

location = class_inst_or_module.const_source_location(const_name)

MethodSource.comment_helper(location, defined?(name) ? name : inspect)
Expand All @@ -174,4 +178,3 @@ class Proc
include MethodSource::SourceLocation::ProcExtensions
include MethodSource::MethodExtensions
end

10 changes: 10 additions & 0 deletions spec/method_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@
it 'should return comment for class instance' do
expect(C.new.method(:hello).class_comment).to eq(@class_comment)
end

it 'should raise a source error for an anonymous class' do
anonymous_class = Class.new do
def hello; end
end

expect do
anonymous_class.instance_method(:hello).class_comment
end.to raise_error(MethodSource::SourceNotFoundError, /anonymous class/)
end
end
# end
describe "Comment tests" do
Expand Down