Skip to content

Commit

Permalink
Document block for Struct.new if present
Browse files Browse the repository at this point in the history
The documentation of Ruby says:
> If a block is given it will be evaluated in the context of StructClass,
> assing the created class as a parameter:
> (snip)
> This is the recommended way to customize a struct. Subclassing an
> anonymous struct creates an extra anonymous class that will never be
> used.

This change implements documentation for such usage.
  • Loading branch information
akihikodaki committed Jun 20, 2017
1 parent a85a262 commit 343b314
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/yard/handlers/ruby/constant_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/handlers/constant_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions spec/handlers/examples/constant_handler_001.rb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 343b314

Please sign in to comment.