diff --git a/lib/yard/handlers/ruby/constant_handler.rb b/lib/yard/handlers/ruby/constant_handler.rb index ded5c0431..d90504c6b 100644 --- a/lib/yard/handlers/ruby/constant_handler.rb +++ b/lib/yard/handlers/ruby/constant_handler.rb @@ -35,6 +35,7 @@ def process_structclass(statement) if lhs.type == :const klass = create_class(lhs[0], P(:Struct)) create_attributes(klass, extract_parameters(statement[1])) + parse_block(statement[1].block[1], :namespace => klass) unless statement[1].block.nil? else raise YARD::Parser::UndocumentableError, "Struct assignment to #{statement[0].source}" end diff --git a/spec/handlers/constant_handler_spec.rb b/spec/handlers/constant_handler_spec.rb index d79eb7a91..5b181d74f 100644 --- a/spec/handlers/constant_handler_spec.rb +++ b/spec/handlers/constant_handler_spec.rb @@ -27,6 +27,14 @@ end end + it 'documents block for Struct.new if present' do + obj = Registry.at("MyStructWithConstant") + expect(obj).to be_kind_of(CodeObjects::ClassObject) + expect(obj.constants[0].docstring).to eq 'A constant.' + expect(obj.constants[0].name).to eq :CONSTANT + expect(obj.constants[0].value).to eq "42" + end + it "turns Const = Struct.new('Name', :sym) into class Const with attr :sym" do obj = Registry.at("NotMyClass") expect(obj).to be_kind_of(CodeObjects::ClassObject) diff --git a/spec/handlers/examples/constant_handler_001.rb.txt b/spec/handlers/examples/constant_handler_001.rb.txt index e321a1a90..196bf1b59 100644 --- a/spec/handlers/examples/constant_handler_001.rb.txt +++ b/spec/handlers/examples/constant_handler_001.rb.txt @@ -18,6 +18,11 @@ MyClass = Struct.new(:a, :b, :c) NotMyClass = Struct.new("NotMyClass2", :b, :c) MyEmptyStruct = Struct.new +MyStructWithConstant = Struct.new do + # A constant. + CONSTANT = 42 +end + # A crazy struct. # # @attr [String] bar An attr