Skip to content

Commit 563c87c

Browse files
committed
Embed mixed-in methods and constants with --embed-mixins
When `--embed-mixins` option is set: - methods from an `extend`ed module are documented as singleton methods - attrs from an `extend`ed module are documented as class attributes - methods from an `include`ed module are documented as instance methods - attrs from an `include`ed module are documented as instance attributes - constants from an `include`ed module are documented Sections are created when needed, and Darkfish's template annotates each of these mixed-in CodeObjects. We also respect the mixin methods' visibility. This feature is inspired by Yard's option of the same name.
1 parent 490e46e commit 563c87c

File tree

7 files changed

+229
-4
lines changed

7 files changed

+229
-4
lines changed

lib/rdoc/class_module.rb

+38-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def comment= comment # :nodoc:
223223
def complete min_visibility
224224
update_aliases
225225
remove_nodoc_children
226+
embed_mixins
226227
update_includes
227228
remove_invisible min_visibility
228229
end
@@ -798,5 +799,41 @@ def update_extends
798799
extends.uniq!
799800
end
800801

801-
end
802+
def embed_mixins
803+
return unless options.embed_mixins
804+
805+
includes.each do |include|
806+
next if String === include.module
807+
include.module.method_list.each do |code_object|
808+
add_method(prepare_to_embed(code_object))
809+
end
810+
include.module.constants.each do |code_object|
811+
add_constant(prepare_to_embed(code_object))
812+
end
813+
include.module.attributes.each do |code_object|
814+
add_attribute(prepare_to_embed(code_object))
815+
end
816+
end
817+
818+
extends.each do |ext|
819+
next if String === ext.module
820+
ext.module.method_list.each do |code_object|
821+
add_method(prepare_to_embed(code_object, true))
822+
end
823+
ext.module.attributes.each do |code_object|
824+
add_attribute(prepare_to_embed(code_object, true))
825+
end
826+
end
827+
end
802828

829+
private
830+
831+
def prepare_to_embed(code_object, singleton=false)
832+
code_object = code_object.dup
833+
code_object.mixin_from = code_object.parent
834+
code_object.singleton = true if singleton
835+
set_current_section(code_object.section.title, code_object.section.comment)
836+
self.visibility = code_object.visibility
837+
code_object
838+
end
839+
end

lib/rdoc/code_object.rb

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class RDoc::CodeObject
9696

9797
attr_accessor :viewer
9898

99+
##
100+
# When mixed-in to a class, this points to the Context in which it was originally defined.
101+
102+
attr_accessor :mixin_from
103+
99104
##
100105
# Creates a new CodeObject that will document itself and its children
101106

lib/rdoc/generator/template/darkfish/class.rhtml

+17-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@
5353
<%- constants.each do |const| -%>
5454
<dt id="<%= const.name %>"><%= const.name %>
5555
<%- if const.comment then -%>
56-
<dd><%= const.description.strip %>
56+
<dd>
57+
<%- if const.mixin_from then -%>
58+
<div class="mixin-from">
59+
Included from <a href="<%= klass.aref_to(const.mixin_from.path)%>"><%= const.mixin_from.full_name %></a>
60+
</div>
61+
<%- end -%>
62+
<%= const.description.strip %>
5763
<%- else -%>
5864
<dd class="missing-docs">(Not documented)
5965
<%- end -%>
@@ -76,6 +82,11 @@
7682
</div>
7783

7884
<div class="method-description">
85+
<%- if attrib.mixin_from then -%>
86+
<div class="mixin-from">
87+
<%= attrib.singleton ? "Extended" : "Included" %> from <a href="<%= klass.aref_to(attrib.mixin_from.path)%>"><%= attrib.mixin_from.full_name %></a>
88+
</div>
89+
<%- end -%>
7990
<%- if attrib.comment then -%>
8091
<%= attrib.description.strip %>
8192
<%- else -%>
@@ -122,6 +133,11 @@
122133
<%- end -%>
123134

124135
<div class="method-description">
136+
<%- if method.mixin_from then -%>
137+
<div class="mixin-from">
138+
<%= method.singleton ? "Extended" : "Included" %> from <a href="<%= klass.aref_to(method.mixin_from.path)%>"><%= method.mixin_from.full_name %></a>
139+
</div>
140+
<%- end -%>
125141
<%- if method.comment then -%>
126142
<%= method.description.strip %>
127143
<%- else -%>

lib/rdoc/generator/template/darkfish/css/rdoc.css

+7
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@ main .aliases {
549549
font-style: italic;
550550
cursor: default;
551551
}
552+
553+
main .mixin-from {
554+
font-size: 80%;
555+
font-style: italic;
556+
margin-bottom: 0.75em;
557+
}
558+
552559
main .method-description ul {
553560
margin-left: 1.5em;
554561
}

lib/rdoc/options.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,20 @@ class RDoc::Options
338338

339339
attr_reader :visibility
340340

341+
##
342+
# Embed mixin methods, attributes, and constants into class documentation. Set via
343+
# +--[no-]embed-mixins+ (Default is +false+.)
344+
345+
attr_accessor :embed_mixins
346+
341347
def initialize loaded_options = nil # :nodoc:
342348
init_ivars
343349
override loaded_options if loaded_options
344350
end
345351

346352
def init_ivars # :nodoc:
347353
@dry_run = false
354+
@embed_mixins = false
348355
@exclude = %w[
349356
~\z \.orig\z \.rej\z \.bak\z
350357
\.gemspec\z
@@ -394,6 +401,7 @@ def init_with map # :nodoc:
394401
@encoding = encoding ? Encoding.find(encoding) : encoding
395402

396403
@charset = map['charset']
404+
@embed_mixins = map['embed_mixins']
397405
@exclude = map['exclude']
398406
@generator_name = map['generator_name']
399407
@hyperlink_all = map['hyperlink_all']
@@ -425,6 +433,7 @@ def override map # :nodoc:
425433
end
426434

427435
@charset = map['charset'] if map.has_key?('charset')
436+
@embed_mixins = map['embed_mixins'] if map.has_key?('embed_mixins')
428437
@exclude = map['exclude'] if map.has_key?('exclude')
429438
@generator_name = map['generator_name'] if map.has_key?('generator_name')
430439
@hyperlink_all = map['hyperlink_all'] if map.has_key?('hyperlink_all')
@@ -452,11 +461,12 @@ def override map # :nodoc:
452461
def == other # :nodoc:
453462
self.class === other and
454463
@encoding == other.encoding and
464+
@embed_mixins == other.embed_mixins and
455465
@generator_name == other.generator_name and
456466
@hyperlink_all == other.hyperlink_all and
457467
@line_numbers == other.line_numbers and
458468
@locale == other.locale and
459-
@locale_dir == other.locale_dir and
469+
@locale_dir == other.locale_dir and
460470
@main_page == other.main_page and
461471
@markup == other.markup and
462472
@op_dir == other.op_dir and
@@ -818,6 +828,14 @@ def parse argv
818828

819829
opt.separator nil
820830

831+
opt.on("--[no-]embed-mixins",
832+
"Embed mixin methods, attributes, and constants",
833+
"into class documentation. (default false)") do |value|
834+
@embed_mixins = value
835+
end
836+
837+
opt.separator nil
838+
821839
markup_formats = RDoc::Text::MARKUP_FORMAT.keys.sort
822840

823841
opt.on("--markup=MARKUP", markup_formats,

test/rdoc/test_rdoc_class_module.rb

+128-1
Original file line numberDiff line numberDiff line change
@@ -1500,5 +1500,132 @@ def test_update_extends_with_colons
15001500
assert_equal [a, c], @c1.extends
15011501
end
15021502

1503-
end
1503+
class TestRDocClassModuleMixins < XrefTestCase
1504+
def setup
1505+
super
1506+
1507+
klass_tl = @store.add_file("klass.rb")
1508+
@klass = klass_tl.add_class(RDoc::NormalClass, "Klass")
1509+
1510+
incmod_tl = @store.add_file("incmod.rb")
1511+
@incmod = incmod_tl.add_module(RDoc::NormalModule, "Incmod")
1512+
1513+
incmod_const = @incmod.add_constant(RDoc::Constant.new("INCMOD_CONST_WITHOUT_A_SECTION", nil, ""))
1514+
incmod_const = @incmod.add_constant(RDoc::Constant.new("INCMOD_CONST", nil, ""))
1515+
incmod_const.section = @incmod.add_section("Incmod const section")
1516+
1517+
incmod_method = @incmod.add_method(RDoc::AnyMethod.new(nil, "incmod_method_without_a_section"))
1518+
incmod_method = @incmod.add_method(RDoc::AnyMethod.new(nil, "incmod_method"))
1519+
incmod_method.section = @incmod.add_section("Incmod method section")
1520+
1521+
incmod_attr = @incmod.add_attribute(RDoc::Attr.new(nil, "incmod_attr_without_a_section", "RW", ""))
1522+
incmod_attr = @incmod.add_attribute(RDoc::Attr.new(nil, "incmod_attr", "RW", ""))
1523+
incmod_attr.section = @incmod.add_section("Incmod attr section")
1524+
1525+
incmod_private_method = @incmod.add_method(RDoc::AnyMethod.new(nil, "incmod_private_method"))
1526+
incmod_private_method.visibility = :private
1527+
1528+
extmod_tl = @store.add_file("extmod.rb")
1529+
@extmod = extmod_tl.add_module(RDoc::NormalModule, "Extmod")
1530+
1531+
extmod_method = @extmod.add_method(RDoc::AnyMethod.new(nil, "extmod_method_without_a_section"))
1532+
extmod_method = @extmod.add_method(RDoc::AnyMethod.new(nil, "extmod_method"))
1533+
extmod_method.section = @extmod.add_section("Extmod method section")
1534+
1535+
extmod_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_attr_without_a_section", "RW", "", true))
1536+
extmod_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_attr", "RW", "", true))
1537+
extmod_attr.section = @extmod.add_section("Extmod attr section")
1538+
1539+
extmod_private_method = @extmod.add_method(RDoc::AnyMethod.new(nil, "extmod_private_method"))
1540+
extmod_private_method.visibility = :private
1541+
1542+
@klass.add_include(RDoc::Include.new("Incmod", nil))
1543+
@klass.add_extend(RDoc::Include.new("Extmod", nil))
1544+
1545+
@klass.add_include(RDoc::Include.new("ExternalInclude", nil))
1546+
@klass.add_extend(RDoc::Include.new("ExternalExtend", nil))
1547+
end
1548+
1549+
def test_embed_mixin_when_false_does_not_embed_anything
1550+
assert_false(@klass.options.embed_mixins)
1551+
@klass.complete(:protected)
1552+
1553+
refute_includes(@klass.constants.map(&:name), "INCMOD_CONST")
1554+
refute_includes(@klass.method_list.map(&:name), "incmod_method")
1555+
refute_includes(@klass.method_list.map(&:name), "extmod_method")
1556+
refute_includes(@klass.attributes.map(&:name), "incmod_attr")
1557+
refute_includes(@klass.attributes.map(&:name), "extmod_attr")
1558+
end
1559+
1560+
def test_embed_mixin_when_true_embeds_methods_and_constants
1561+
@klass.options.embed_mixins = true
1562+
@klass.complete(:protected)
1563+
1564+
# assert on presence and identity of methods and constants
1565+
constant = @klass.constants.find { |c| c.name == "INCMOD_CONST" }
1566+
assert(constant, "constant from included mixin should be present")
1567+
assert_equal(@incmod, constant.mixin_from)
15041568

1569+
instance_method = @klass.method_list.find { |m| m.name == "incmod_method" }
1570+
assert(instance_method, "instance method from included mixin should be present")
1571+
refute(instance_method.singleton)
1572+
assert_equal(@incmod, instance_method.mixin_from)
1573+
1574+
instance_attr = @klass.attributes.find { |a| a.name == "incmod_attr" }
1575+
assert(instance_attr, "instance attr from included mixin should be present")
1576+
refute(instance_attr.singleton)
1577+
assert_equal(@incmod, instance_attr.mixin_from)
1578+
1579+
refute(@klass.method_list.find { |m| m.name == "incmod_private_method" })
1580+
1581+
class_method = @klass.method_list.find { |m| m.name == "extmod_method" }
1582+
assert(class_method, "class method from extended mixin should be present")
1583+
assert(class_method.singleton)
1584+
assert_equal(@extmod, class_method.mixin_from)
1585+
1586+
class_attr = @klass.attributes.find { |a| a.name == "extmod_attr" }
1587+
assert(class_attr, "class attr from extended mixin should be present")
1588+
assert(class_attr.singleton)
1589+
assert_equal(@extmod, class_attr.mixin_from)
1590+
1591+
refute(@klass.method_list.find { |m| m.name == "extmod_private_method" })
1592+
1593+
# assert that sections are also imported
1594+
constant_section = @klass.sections.find { |s| s.title == "Incmod const section" }
1595+
assert(constant_section, "constant from included mixin should have a section")
1596+
assert_equal(constant_section, constant.section)
1597+
1598+
instance_method_section = @klass.sections.find { |s| s.title == "Incmod method section" }
1599+
assert(instance_method_section, "instance method from included mixin should have a section")
1600+
assert_equal(instance_method_section, instance_method.section)
1601+
1602+
instance_attr_section = @klass.sections.find { |s| s.title == "Incmod attr section" }
1603+
assert(instance_attr_section, "instance attr from included mixin should have a section")
1604+
assert_equal(instance_attr_section, instance_attr.section)
1605+
1606+
class_method_section = @klass.sections.find { |s| s.title == "Extmod method section" }
1607+
assert(class_method_section, "class method from extended mixin should have a section")
1608+
assert_equal(class_method_section, class_method.section)
1609+
1610+
class_attr_section = @klass.sections.find { |s| s.title == "Extmod attr section" }
1611+
assert(class_attr_section, "class attr from extended mixin should have a section")
1612+
assert_equal(class_attr_section, class_attr.section)
1613+
1614+
# and check that code objects without a section still have no section
1615+
constant = @klass.constants.find { |c| c.name == "INCMOD_CONST_WITHOUT_A_SECTION" }
1616+
assert_nil(constant.section.title)
1617+
1618+
instance_method = @klass.method_list.find { |c| c.name == "incmod_method_without_a_section" }
1619+
assert_nil(instance_method.section.title)
1620+
1621+
instance_attr = @klass.attributes.find { |c| c.name == "incmod_attr_without_a_section" }
1622+
assert_nil(instance_attr.section.title)
1623+
1624+
class_method = @klass.method_list.find { |c| c.name == "extmod_method_without_a_section" }
1625+
assert_nil(class_method.section.title)
1626+
1627+
class_attr = @klass.attributes.find { |c| c.name == "extmod_attr_without_a_section" }
1628+
assert_nil(class_attr.section.title)
1629+
end
1630+
end
1631+
end

test/rdoc/test_rdoc_options.rb

+15
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class << coder; alias add []=; end
6666
expected = {
6767
'charset' => 'UTF-8',
6868
'encoding' => encoding,
69+
'embed_mixins' => false,
6970
'exclude' => %w[~\z \.orig\z \.rej\z \.bak\z \.gemspec\z],
7071
'hyperlink_all' => false,
7172
'line_numbers' => false,
@@ -561,6 +562,20 @@ def test_parse_root
561562
assert_includes @options.rdoc_include, @options.root.to_s
562563
end
563564

565+
def test_parse_embed_mixins
566+
assert_false(@options.embed_mixins)
567+
568+
out, err = capture_output { @options.parse(["--embed-mixins"]) }
569+
assert_empty(out)
570+
assert_empty(err)
571+
assert_true(@options.embed_mixins)
572+
573+
out, err = capture_output { @options.parse(["--no-embed-mixins"]) }
574+
assert_empty(out)
575+
assert_empty(err)
576+
assert_false(@options.embed_mixins)
577+
end
578+
564579
def test_parse_tab_width
565580
@options.parse %w[--tab-width=1]
566581
assert_equal 1, @options.tab_width

0 commit comments

Comments
 (0)