Skip to content
This repository
Browse code

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...
commit 2f61dfbf5531015feb6da896fb8fddf722d773a0 1 parent c188290
Eric Hodel authored November 17, 2012
16  lib/rdoc/class_module.rb
@@ -19,6 +19,7 @@ class RDoc::ClassModule < RDoc::Context
19 19
   #   * Added sections
20 20
   #   * Added in_files
21 21
   #   * Added parent name
  22
+  #   * Complete Constant dump
22 23
 
23 24
   MARSHAL_VERSION = 3 # :nodoc:
24 25
 
@@ -292,9 +293,7 @@ def marshal_dump # :nodoc:
292 293
       @superclass,
293 294
       parse(@comment_location),
294 295
       attrs,
295  
-      constants.map do |const|
296  
-        [const.name, parse(const.comment), const.file_name]
297  
-      end,
  296
+      constants,
298 297
       includes.map do |incl|
299 298
         [incl.name, parse(incl.comment), incl.file_name]
300 299
       end,
@@ -344,9 +343,14 @@ def marshal_load array # :nodoc:
344 343
       attr.record_location RDoc::TopLevel.new file
345 344
     end
346 345
 
347  
-    array[6].each do |name, comment, file|
348  
-      const = add_constant RDoc::Constant.new(name, nil, comment)
349  
-      const.record_location RDoc::TopLevel.new file
  346
+    array[6].each do |constant, comment, file|
  347
+      case constant
  348
+      when RDoc::Constant then
  349
+        add_constant constant
  350
+      else
  351
+        constant = add_constant RDoc::Constant.new(constant, nil, comment)
  352
+        constant.record_location RDoc::TopLevel.new file
  353
+      end
350 354
     end
351 355
 
352 356
     array[7].each do |name, comment, file|
14  lib/rdoc/code_object.rb
@@ -85,9 +85,9 @@ class RDoc::CodeObject
85 85
   attr_reader :received_nodoc
86 86
 
87 87
   ##
88  
-  # Which section are we in
  88
+  # Set the section this CodeObject is in
89 89
 
90  
-  attr_accessor :section
  90
+  attr_writer :section
91 91
 
92 92
   ##
93 93
   # The RDoc::Store for this object.
@@ -314,6 +314,16 @@ def record_location top_level
314 314
   end
315 315
 
316 316
   ##
  317
+  # The section this CodeObject is in.  Sections allow grouping of constants,
  318
+  # attributes and methods inside a class or module.
  319
+
  320
+  def section
  321
+    return @section if @section
  322
+
  323
+    @section = parent.add_section @section_title if parent
  324
+  end
  325
+
  326
+  ##
317 327
   # Enable capture of documentation unless documentation has been
318 328
   # turned off by :enddoc:
319 329
 
69  lib/rdoc/constant.rb
@@ -3,12 +3,12 @@
3 3
 
4 4
 class RDoc::Constant < RDoc::CodeObject
5 5
 
  6
+  MARSHAL_VERSION = 0 # :nodoc:
  7
+
6 8
   ##
7  
-  # If this constant is an alias for a module or class,
8  
-  # this is the RDoc::ClassModule it is an alias for.
9  
-  # +nil+ otherwise.
  9
+  # Sets the module or class this is constant is an alias for.
10 10
 
11  
-  attr_accessor :is_alias_for
  11
+  attr_writer :is_alias_for
12 12
 
13 13
   ##
14 14
   # The constant's name
@@ -21,13 +21,22 @@ class RDoc::Constant < RDoc::CodeObject
21 21
   attr_accessor :value
22 22
 
23 23
   ##
  24
+  # The constant's visibility
  25
+
  26
+  attr_accessor :visibility
  27
+
  28
+  ##
24 29
   # Creates a new constant with +name+, +value+ and +comment+
25 30
 
26 31
   def initialize(name, value, comment)
27 32
     super()
28  
-    @name = name
  33
+
  34
+    @name  = name
29 35
     @value = value
  36
+
30 37
     @is_alias_for = nil
  38
+    @visibility   = nil
  39
+
31 40
     self.comment = comment
32 41
   end
33 42
 
@@ -64,6 +73,18 @@ def full_name
64 73
     @full_name ||= "#{parent_name}::#{@name}"
65 74
   end
66 75
 
  76
+  ##
  77
+  # The module or class this constant is an alias for
  78
+
  79
+  def is_alias_for
  80
+    case @is_alias_for
  81
+    when String then
  82
+      @is_alias_for = @store.find_class_or_module @is_alias_for
  83
+    else
  84
+      @is_alias_for
  85
+    end
  86
+  end
  87
+
67 88
   def inspect # :nodoc:
68 89
     "#<%s:0x%x %s::%s>" % [
69 90
       self.class, object_id,
@@ -72,6 +93,44 @@ def inspect # :nodoc:
72 93
   end
73 94
 
74 95
   ##
  96
+  # Dumps this Constant for use by ri.  See also #marshal_load
  97
+
  98
+  def marshal_dump
  99
+    [ MARSHAL_VERSION,
  100
+      @name,
  101
+      full_name,
  102
+      @visibility,
  103
+      @is_alias_for ? @is_alias_for.full_name : nil,
  104
+      parse(@comment),
  105
+      @file.absolute_name,
  106
+      @parent.name,
  107
+      @parent.class,
  108
+      @section.title,
  109
+    ]
  110
+  end
  111
+
  112
+  ##
  113
+  # Loads this Constant from +array+.  For a loaded Constant the following
  114
+  # methods will return cached values:
  115
+  #
  116
+  # * #full_name
  117
+  # * #parent_name
  118
+
  119
+  def marshal_load array
  120
+    @name          = array[1]
  121
+    @full_name     = array[2]
  122
+    @visibility    = array[3]
  123
+    @is_alias_for  = array[4]
  124
+    @comment       = array[5]
  125
+    #                      6 handled below
  126
+    @parent_name   = array[7]
  127
+    @parent_class  = array[8]
  128
+    @section_title = array[9]
  129
+
  130
+    @file = RDoc::TopLevel.new array[6]
  131
+  end
  132
+
  133
+  ##
75 134
   # Path to this constant
76 135
 
77 136
   def path
6  lib/rdoc/method_attr.rb
@@ -365,12 +365,6 @@ def pretty_print q # :nodoc:
365 365
     end
366 366
   end
367 367
 
368  
-  def section # :nodoc:
369  
-    return @section if @section
370  
-
371  
-    @section = parent.add_section @section_title if parent
372  
-  end
373  
-
374 368
   ##
375 369
   # Used by RDoc::Generator::JsonIndex to create a record for the search
376 370
   # engine.
40  test/test_rdoc_class_module.rb
@@ -461,27 +461,29 @@ def test_marshal_load_version_3
461 461
     cm.add_comment 'this is a comment', tl
462 462
 
463 463
     loaded = Marshal.load "\x04\bU:\x16RDoc::NormalClass[\x13i\bI\"\nKlass" \
464  
-                          "\x06:\x06EFI\"\x15Namespace::Klass\x06;\x06FI" \
465  
-                          "\"\nSuper\x06;\x06Fo:\eRDoc::Markup::Document\a" \
  464
+                          "\x06:\x06ETI\"\x15Namespace::Klass\x06;\x06TI" \
  465
+                          "\"\nSuper\x06;\x06To:\eRDoc::Markup::Document\a" \
466 466
                           ":\v@parts[\x06o;\a\a;\b[\x06o" \
467 467
                           ":\x1CRDoc::Markup::Paragraph\x06;\b[\x06I" \
468  
-                          "\"\x16this is a comment\x06;\x06F:\n@fileI" \
469  
-                          "\"\ffile.rb\x06;\x06F;\n0[\a[\nI\"\aa2\x06" \
470  
-                          ";\x06FI\"\aRW\x06;\x06F:\vpublicT@\x11[\nI" \
471  
-                          "\"\aa1\x06;\x06FI\"\aRW\x06;\x06F;\vF@\x11" \
472  
-                          "[\x06[\bI\"\aC1\x06;\x06Fo;\a\a;\b[\x00" \
473  
-                          ";\n0@\x11[\x06[\bI\"\aI1\x06;\x06Fo;\a\a;\b" \
474  
-                          "[\x00;\n0@\x11[\a[\aI\"\nclass\x06;\x06F[\b" \
475  
-                          "[\a;\v[\x00[\a:\x0Eprotected[\x00[\a" \
476  
-                          ":\fprivate[\x00[\aI\"\rinstance\x06;\x06F[\b" \
477  
-                          "[\a;\v[\x06[\aI\"\am1\x06;\x06F@\x11[\a;\f[\x00" \
478  
-                          "[\a;\r[\x00[\x06[\bI\"\aE1\x06;\x06Fo;\a\a;\b" \
479  
-                          "[\x00;\n0@\x11[\aU:\eRDoc::Context::Section" \
480  
-                          "[\bi\x000o;\a\a;\b[\x00;\n0U;\x0E[\bi\x00I" \
481  
-                          "\"\fsection\x06;\x06Fo;\a\a;\b[\x06o;\a\a;\b" \
482  
-                          "[\x06o;\t\x06;\b[\x06I\"\x14section comment\x06" \
483  
-                          ";\x06F;\n@\x11;\n0[\x06@\x11I\"\x0ENamespace\x06" \
484  
-                          ";\x06Fc\x17RDoc::NormalModule"
  468
+                          "\"\x16this is a comment\x06;\x06T:\n@fileI" \
  469
+                          "\"\ffile.rb\x06;\x06T;\n0[\a[\nI\"\aa2\x06;" \
  470
+                          "\x06TI\"\aRW\x06;\x06T:\vpublicT@\x11[\nI" \
  471
+                          "\"\aa1\x06;\x06TI\"\aRW\x06;\x06T;\vF@\x11" \
  472
+                          "[\x06U:\x13RDoc::Constant[\x0Fi\x00I\"\aC1\x06" \
  473
+                          ";\x06TI\"\x19Namespace::Klass::C1\x06;\x06T00o" \
  474
+                          ";\a\a;\b[\x00;\n0@\x11@\ac\x16RDoc::NormalClass0" \
  475
+                          "[\x06[\bI\"\aI1\x06;\x06To;\a\a;\b[\x00;\n0@\x11" \
  476
+                          "[\a[\aI\"\nclass\x06;\x06T[\b[\a;\v[\x00[\a" \
  477
+                          ":\x0Eprotected[\x00[\a:\fprivate[\x00[\aI" \
  478
+                          "\"\rinstance\x06;\x06T[\b[\a;\v[\x06[\aI" \
  479
+                          "\"\am1\x06;\x06T@\x11[\a;\r[\x00[\a;\x0E[\x00" \
  480
+                          "[\x06[\bI\"\aE1\x06;\x06To;\a\a;\b[\x00;\n0@\x11" \
  481
+                          "[\aU:\eRDoc::Context::Section[\bi\x000o;\a\a;\b" \
  482
+                          "[\x00;\n0U;\x0F[\bi\x00I\"\fsection\x06;\x06To" \
  483
+                          ";\a\a;\b[\x06o;\a\a;\b[\x06o;\t\x06;\b[\x06I" \
  484
+                          "\"\x14section comment\x06;\x06T;\n@\x11;\n0" \
  485
+                          "[\x06@\x11I\"\x0ENamespace\x06" \
  486
+                          ";\x06Tc\x17RDoc::NormalModule"
485 487
 
486 488
     loaded.store = @store
487 489
 
20  test/test_rdoc_code_object.rb
@@ -263,6 +263,26 @@ def test_record_location_ignored
263 263
     refute @co.ignored?
264 264
   end
265 265
 
  266
+  def test_section
  267
+    parent = RDoc::Context.new
  268
+    section = parent.sections.first
  269
+
  270
+    @co.parent = parent
  271
+    @co.instance_variable_set :@section, section
  272
+
  273
+    assert_equal section, @co.section
  274
+
  275
+    @co.instance_variable_set :@section, nil
  276
+    @co.instance_variable_set :@section_title, nil
  277
+
  278
+    assert_equal section, @co.section
  279
+
  280
+    @co.instance_variable_set :@section, nil
  281
+    @co.instance_variable_set :@section_title, 'new title'
  282
+
  283
+    assert_equal 'new title', @co.section.title
  284
+  end
  285
+
266 286
   def test_start_doc
267 287
     @co.document_self = false
268 288
     @co.document_children = false
89  test/test_rdoc_constant.rb
@@ -12,6 +12,95 @@ def test_full_name
12 12
     assert_equal 'C1::CONST', @const.full_name
13 13
   end
14 14
 
  15
+  def test_marshal_dump
  16
+    top_level = @store.add_file 'file.rb'
  17
+
  18
+    c = RDoc::Constant.new 'CONST', nil, 'this is a comment'
  19
+    c.record_location top_level
  20
+
  21
+    aliased = top_level.add_class RDoc::NormalClass, 'Aliased'
  22
+    c.is_alias_for = aliased
  23
+
  24
+    cm = top_level.add_class RDoc::NormalClass, 'Klass'
  25
+    cm.add_constant c
  26
+
  27
+    section = cm.sections.first
  28
+
  29
+    loaded = Marshal.load Marshal.dump c
  30
+    loaded.store = @store
  31
+
  32
+    comment = doc(para('this is a comment'))
  33
+
  34
+    assert_equal c, loaded
  35
+
  36
+    assert_equal aliased,        loaded.is_alias_for
  37
+    assert_equal comment,        loaded.comment
  38
+    assert_equal top_level,      loaded.file
  39
+    assert_equal 'Klass::CONST', loaded.full_name
  40
+    assert_equal 'CONST',        loaded.name
  41
+    assert_nil                   loaded.visibility
  42
+    assert_equal cm,             loaded.parent
  43
+    assert_equal section,        loaded.section
  44
+  end
  45
+
  46
+  def test_marshal_load
  47
+    top_level = @store.add_file 'file.rb'
  48
+
  49
+    c = RDoc::Constant.new 'CONST', nil, 'this is a comment'
  50
+    c.record_location top_level
  51
+
  52
+    cm = top_level.add_class RDoc::NormalClass, 'Klass'
  53
+    cm.add_constant c
  54
+
  55
+    section = cm.sections.first
  56
+
  57
+    loaded = Marshal.load Marshal.dump c
  58
+    loaded.store = @store
  59
+
  60
+    comment = doc(para('this is a comment'))
  61
+
  62
+    assert_equal c, loaded
  63
+
  64
+    assert_nil                   loaded.is_alias_for
  65
+    assert_equal comment,        loaded.comment
  66
+    assert_equal top_level,      loaded.file
  67
+    assert_equal 'Klass::CONST', loaded.full_name
  68
+    assert_equal 'CONST',        loaded.name
  69
+    assert_nil                   loaded.visibility
  70
+    assert_equal cm,             loaded.parent
  71
+    assert_equal section,        loaded.section
  72
+  end
  73
+
  74
+  def test_marshal_load_version_0
  75
+    top_level = @store.add_file 'file.rb'
  76
+
  77
+    aliased = top_level.add_class RDoc::NormalClass, 'Aliased'
  78
+    cm      = top_level.add_class RDoc::NormalClass, 'Klass'
  79
+    section = cm.sections.first
  80
+
  81
+    loaded = Marshal.load "\x04\bU:\x13RDoc::Constant[\x0Fi\x00I" \
  82
+                          "\"\nCONST\x06:\x06ETI\"\x11Klass::CONST\x06" \
  83
+                          ";\x06T0I\"\fAliased\x06;\x06To" \
  84
+                          ":\eRDoc::Markup::Document\a:\v@parts[\x06o" \
  85
+                          ":\x1CRDoc::Markup::Paragraph\x06;\b[\x06I" \
  86
+                          "\"\x16this is a comment\x06;\x06T:\n@file0I" \
  87
+                          "\"\ffile.rb\x06;\x06TI\"\nKlass\x06" \
  88
+                          ";\x06Tc\x16RDoc::NormalClass0"
  89
+
  90
+    loaded.store = @store
  91
+
  92
+    comment = doc(para('this is a comment'))
  93
+
  94
+    assert_equal aliased,        loaded.is_alias_for
  95
+    assert_equal comment,        loaded.comment
  96
+    assert_equal top_level,      loaded.file
  97
+    assert_equal 'Klass::CONST', loaded.full_name
  98
+    assert_equal 'CONST',        loaded.name
  99
+    assert_nil                   loaded.visibility
  100
+    assert_equal cm,             loaded.parent
  101
+    assert_equal section,        loaded.section
  102
+  end
  103
+
15 104
   def test_path
16 105
     assert_equal 'C1.html#CONST', @const.path
17 106
   end
16  test/test_rdoc_method_attr.rb
@@ -140,22 +140,6 @@ def test_search_record
140 140
     assert_equal expected, @c1_m.search_record
141 141
   end
142 142
 
143  
-  def test_section
144  
-    section = @c1.sections.find { |s| s.title.nil? }
145  
-
146  
-    assert_equal section, @c1_m.section
147  
-
148  
-    @c1_m.instance_variable_set :@section, nil
149  
-    @c1_m.instance_variable_set :@section_title, nil
150  
-
151  
-    assert_equal section, @c1_m.section
152  
-
153  
-    @c1_m.instance_variable_set :@section, nil
154  
-    @c1_m.instance_variable_set :@section_title, 'new title'
155  
-
156  
-    assert_equal 'new title', @c1_m.section.title
157  
-  end
158  
-
159 143
   def test_to_s
160 144
     assert_equal 'RDoc::AnyMethod: C1#m',  @c1_m.to_s
161 145
     assert_equal 'RDoc::AnyMethod: C2#b',  @c2_b.to_s

0 notes on commit 2f61dfb

Please sign in to comment.
Something went wrong with that request. Please try again.