Permalink
Browse files

Added marshal_dump to Constant for section support

ClassModule now saves and loads a Constant as-is

Moved #section to RDoc::CodeObject for reuse
  • Loading branch information...
1 parent c188290 commit 2f61dfbf5531015feb6da896fb8fddf722d773a0 @drbrain drbrain committed Nov 18, 2012
@@ -19,6 +19,7 @@ class RDoc::ClassModule < RDoc::Context
# * Added sections
# * Added in_files
# * Added parent name
+ # * Complete Constant dump
MARSHAL_VERSION = 3 # :nodoc:
@@ -292,9 +293,7 @@ def marshal_dump # :nodoc:
@superclass,
parse(@comment_location),
attrs,
- constants.map do |const|
- [const.name, parse(const.comment), const.file_name]
- end,
+ constants,
includes.map do |incl|
[incl.name, parse(incl.comment), incl.file_name]
end,
@@ -344,9 +343,14 @@ def marshal_load array # :nodoc:
attr.record_location RDoc::TopLevel.new file
end
- array[6].each do |name, comment, file|
- const = add_constant RDoc::Constant.new(name, nil, comment)
- const.record_location RDoc::TopLevel.new file
+ array[6].each do |constant, comment, file|
+ case constant
+ when RDoc::Constant then
+ add_constant constant
+ else
+ constant = add_constant RDoc::Constant.new(constant, nil, comment)
+ constant.record_location RDoc::TopLevel.new file
+ end
end
array[7].each do |name, comment, file|
@@ -85,9 +85,9 @@ class RDoc::CodeObject
attr_reader :received_nodoc
##
- # Which section are we in
+ # Set the section this CodeObject is in
- attr_accessor :section
+ attr_writer :section
##
# The RDoc::Store for this object.
@@ -314,6 +314,16 @@ def record_location top_level
end
##
+ # The section this CodeObject is in. Sections allow grouping of constants,
+ # attributes and methods inside a class or module.
+
+ def section
+ return @section if @section
+
+ @section = parent.add_section @section_title if parent
+ end
+
+ ##
# Enable capture of documentation unless documentation has been
# turned off by :enddoc:
View
@@ -3,12 +3,12 @@
class RDoc::Constant < RDoc::CodeObject
+ MARSHAL_VERSION = 0 # :nodoc:
+
##
- # If this constant is an alias for a module or class,
- # this is the RDoc::ClassModule it is an alias for.
- # +nil+ otherwise.
+ # Sets the module or class this is constant is an alias for.
- attr_accessor :is_alias_for
+ attr_writer :is_alias_for
##
# The constant's name
@@ -21,13 +21,22 @@ class RDoc::Constant < RDoc::CodeObject
attr_accessor :value
##
+ # The constant's visibility
+
+ attr_accessor :visibility
+
+ ##
# Creates a new constant with +name+, +value+ and +comment+
def initialize(name, value, comment)
super()
- @name = name
+
+ @name = name
@value = value
+
@is_alias_for = nil
+ @visibility = nil
+
self.comment = comment
end
@@ -64,6 +73,18 @@ def full_name
@full_name ||= "#{parent_name}::#{@name}"
end
+ ##
+ # The module or class this constant is an alias for
+
+ def is_alias_for
+ case @is_alias_for
+ when String then
+ @is_alias_for = @store.find_class_or_module @is_alias_for
+ else
+ @is_alias_for
+ end
+ end
+
def inspect # :nodoc:
"#<%s:0x%x %s::%s>" % [
self.class, object_id,
@@ -72,6 +93,44 @@ def inspect # :nodoc:
end
##
+ # Dumps this Constant for use by ri. See also #marshal_load
+
+ def marshal_dump
+ [ MARSHAL_VERSION,
+ @name,
+ full_name,
+ @visibility,
+ @is_alias_for ? @is_alias_for.full_name : nil,
+ parse(@comment),
+ @file.absolute_name,
+ @parent.name,
+ @parent.class,
+ @section.title,
+ ]
+ end
+
+ ##
+ # Loads this Constant from +array+. For a loaded Constant the following
+ # methods will return cached values:
+ #
+ # * #full_name
+ # * #parent_name
+
+ def marshal_load array
+ @name = array[1]
+ @full_name = array[2]
+ @visibility = array[3]
+ @is_alias_for = array[4]
+ @comment = array[5]
+ # 6 handled below
+ @parent_name = array[7]
+ @parent_class = array[8]
+ @section_title = array[9]
+
+ @file = RDoc::TopLevel.new array[6]
+ end
+
+ ##
# Path to this constant
def path
@@ -365,12 +365,6 @@ def pretty_print q # :nodoc:
end
end
- def section # :nodoc:
- return @section if @section
-
- @section = parent.add_section @section_title if parent
- end
-
##
# Used by RDoc::Generator::JsonIndex to create a record for the search
# engine.
@@ -461,27 +461,29 @@ def test_marshal_load_version_3
cm.add_comment 'this is a comment', tl
loaded = Marshal.load "\x04\bU:\x16RDoc::NormalClass[\x13i\bI\"\nKlass" \
- "\x06:\x06EFI\"\x15Namespace::Klass\x06;\x06FI" \
- "\"\nSuper\x06;\x06Fo:\eRDoc::Markup::Document\a" \
+ "\x06:\x06ETI\"\x15Namespace::Klass\x06;\x06TI" \
+ "\"\nSuper\x06;\x06To:\eRDoc::Markup::Document\a" \
":\v@parts[\x06o;\a\a;\b[\x06o" \
":\x1CRDoc::Markup::Paragraph\x06;\b[\x06I" \
- "\"\x16this is a comment\x06;\x06F:\n@fileI" \
- "\"\ffile.rb\x06;\x06F;\n0[\a[\nI\"\aa2\x06" \
- ";\x06FI\"\aRW\x06;\x06F:\vpublicT@\x11[\nI" \
- "\"\aa1\x06;\x06FI\"\aRW\x06;\x06F;\vF@\x11" \
- "[\x06[\bI\"\aC1\x06;\x06Fo;\a\a;\b[\x00" \
- ";\n0@\x11[\x06[\bI\"\aI1\x06;\x06Fo;\a\a;\b" \
- "[\x00;\n0@\x11[\a[\aI\"\nclass\x06;\x06F[\b" \
- "[\a;\v[\x00[\a:\x0Eprotected[\x00[\a" \
- ":\fprivate[\x00[\aI\"\rinstance\x06;\x06F[\b" \
- "[\a;\v[\x06[\aI\"\am1\x06;\x06F@\x11[\a;\f[\x00" \
- "[\a;\r[\x00[\x06[\bI\"\aE1\x06;\x06Fo;\a\a;\b" \
- "[\x00;\n0@\x11[\aU:\eRDoc::Context::Section" \
- "[\bi\x000o;\a\a;\b[\x00;\n0U;\x0E[\bi\x00I" \
- "\"\fsection\x06;\x06Fo;\a\a;\b[\x06o;\a\a;\b" \
- "[\x06o;\t\x06;\b[\x06I\"\x14section comment\x06" \
- ";\x06F;\n@\x11;\n0[\x06@\x11I\"\x0ENamespace\x06" \
- ";\x06Fc\x17RDoc::NormalModule"
+ "\"\x16this is a comment\x06;\x06T:\n@fileI" \
+ "\"\ffile.rb\x06;\x06T;\n0[\a[\nI\"\aa2\x06;" \
+ "\x06TI\"\aRW\x06;\x06T:\vpublicT@\x11[\nI" \
+ "\"\aa1\x06;\x06TI\"\aRW\x06;\x06T;\vF@\x11" \
+ "[\x06U:\x13RDoc::Constant[\x0Fi\x00I\"\aC1\x06" \
+ ";\x06TI\"\x19Namespace::Klass::C1\x06;\x06T00o" \
+ ";\a\a;\b[\x00;\n0@\x11@\ac\x16RDoc::NormalClass0" \
+ "[\x06[\bI\"\aI1\x06;\x06To;\a\a;\b[\x00;\n0@\x11" \
+ "[\a[\aI\"\nclass\x06;\x06T[\b[\a;\v[\x00[\a" \
+ ":\x0Eprotected[\x00[\a:\fprivate[\x00[\aI" \
+ "\"\rinstance\x06;\x06T[\b[\a;\v[\x06[\aI" \
+ "\"\am1\x06;\x06T@\x11[\a;\r[\x00[\a;\x0E[\x00" \
+ "[\x06[\bI\"\aE1\x06;\x06To;\a\a;\b[\x00;\n0@\x11" \
+ "[\aU:\eRDoc::Context::Section[\bi\x000o;\a\a;\b" \
+ "[\x00;\n0U;\x0F[\bi\x00I\"\fsection\x06;\x06To" \
+ ";\a\a;\b[\x06o;\a\a;\b[\x06o;\t\x06;\b[\x06I" \
+ "\"\x14section comment\x06;\x06T;\n@\x11;\n0" \
+ "[\x06@\x11I\"\x0ENamespace\x06" \
+ ";\x06Tc\x17RDoc::NormalModule"
loaded.store = @store
@@ -263,6 +263,26 @@ def test_record_location_ignored
refute @co.ignored?
end
+ def test_section
+ parent = RDoc::Context.new
+ section = parent.sections.first
+
+ @co.parent = parent
+ @co.instance_variable_set :@section, section
+
+ assert_equal section, @co.section
+
+ @co.instance_variable_set :@section, nil
+ @co.instance_variable_set :@section_title, nil
+
+ assert_equal section, @co.section
+
+ @co.instance_variable_set :@section, nil
+ @co.instance_variable_set :@section_title, 'new title'
+
+ assert_equal 'new title', @co.section.title
+ end
+
def test_start_doc
@co.document_self = false
@co.document_children = false
@@ -12,6 +12,95 @@ def test_full_name
assert_equal 'C1::CONST', @const.full_name
end
+ def test_marshal_dump
+ top_level = @store.add_file 'file.rb'
+
+ c = RDoc::Constant.new 'CONST', nil, 'this is a comment'
+ c.record_location top_level
+
+ aliased = top_level.add_class RDoc::NormalClass, 'Aliased'
+ c.is_alias_for = aliased
+
+ cm = top_level.add_class RDoc::NormalClass, 'Klass'
+ cm.add_constant c
+
+ section = cm.sections.first
+
+ loaded = Marshal.load Marshal.dump c
+ loaded.store = @store
+
+ comment = doc(para('this is a comment'))
+
+ assert_equal c, loaded
+
+ assert_equal aliased, loaded.is_alias_for
+ assert_equal comment, loaded.comment
+ assert_equal top_level, loaded.file
+ assert_equal 'Klass::CONST', loaded.full_name
+ assert_equal 'CONST', loaded.name
+ assert_nil loaded.visibility
+ assert_equal cm, loaded.parent
+ assert_equal section, loaded.section
+ end
+
+ def test_marshal_load
+ top_level = @store.add_file 'file.rb'
+
+ c = RDoc::Constant.new 'CONST', nil, 'this is a comment'
+ c.record_location top_level
+
+ cm = top_level.add_class RDoc::NormalClass, 'Klass'
+ cm.add_constant c
+
+ section = cm.sections.first
+
+ loaded = Marshal.load Marshal.dump c
+ loaded.store = @store
+
+ comment = doc(para('this is a comment'))
+
+ assert_equal c, loaded
+
+ assert_nil loaded.is_alias_for
+ assert_equal comment, loaded.comment
+ assert_equal top_level, loaded.file
+ assert_equal 'Klass::CONST', loaded.full_name
+ assert_equal 'CONST', loaded.name
+ assert_nil loaded.visibility
+ assert_equal cm, loaded.parent
+ assert_equal section, loaded.section
+ end
+
+ def test_marshal_load_version_0
+ top_level = @store.add_file 'file.rb'
+
+ aliased = top_level.add_class RDoc::NormalClass, 'Aliased'
+ cm = top_level.add_class RDoc::NormalClass, 'Klass'
+ section = cm.sections.first
+
+ loaded = Marshal.load "\x04\bU:\x13RDoc::Constant[\x0Fi\x00I" \
+ "\"\nCONST\x06:\x06ETI\"\x11Klass::CONST\x06" \
+ ";\x06T0I\"\fAliased\x06;\x06To" \
+ ":\eRDoc::Markup::Document\a:\v@parts[\x06o" \
+ ":\x1CRDoc::Markup::Paragraph\x06;\b[\x06I" \
+ "\"\x16this is a comment\x06;\x06T:\n@file0I" \
+ "\"\ffile.rb\x06;\x06TI\"\nKlass\x06" \
+ ";\x06Tc\x16RDoc::NormalClass0"
+
+ loaded.store = @store
+
+ comment = doc(para('this is a comment'))
+
+ assert_equal aliased, loaded.is_alias_for
+ assert_equal comment, loaded.comment
+ assert_equal top_level, loaded.file
+ assert_equal 'Klass::CONST', loaded.full_name
+ assert_equal 'CONST', loaded.name
+ assert_nil loaded.visibility
+ assert_equal cm, loaded.parent
+ assert_equal section, loaded.section
+ end
+
def test_path
assert_equal 'C1.html#CONST', @const.path
end
@@ -140,22 +140,6 @@ def test_search_record
assert_equal expected, @c1_m.search_record
end
- def test_section
- section = @c1.sections.find { |s| s.title.nil? }
-
- assert_equal section, @c1_m.section
-
- @c1_m.instance_variable_set :@section, nil
- @c1_m.instance_variable_set :@section_title, nil
-
- assert_equal section, @c1_m.section
-
- @c1_m.instance_variable_set :@section, nil
- @c1_m.instance_variable_set :@section_title, 'new title'
-
- assert_equal 'new title', @c1_m.section.title
- end
-
def test_to_s
assert_equal 'RDoc::AnyMethod: C1#m', @c1_m.to_s
assert_equal 'RDoc::AnyMethod: C2#b', @c2_b.to_s

0 comments on commit 2f61dfb

Please sign in to comment.