Class: RDoc::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc/store.rb

Overview

A set of rdoc data for a single project (gem, path, etc.).

The store manages reading and writing ri data for a project and maintains a cache of methods, classes and ancestors in the store.

The store maintains a #cache of its contents for faster lookup. After adding items to the store it must be flushed using #save_cache. The cache contains the following structures:

@cache = { :ancestors => {}, # class name => ancestor names :attributes => {}, # class name => attributes :class_methods => {}, # class name => class methods :instance_methods => {}, # class name => instance methods :modules => [], # classes and modules in this store :pages => [], # page names }

TODO need to prune classes

Defined Under Namespace

Classes: Error, MissingFileError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, path: nil, type: nil) ⇒ Store

Creates a new Store of type that will load or save to path



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rdoc/store.rb', line 119

def initialize(options, path: nil, type: nil)
  @options  = options
  @dry_run  = options.dry_run
  @encoding = options.encoding
  @path     = path || options.op_dir
  @type     = type

  @cache = {
    :ancestors                   => {},
    :attributes                  => {},
    :class_methods               => {},
    :c_class_variables           => {},
    :c_singleton_class_variables => {},
    :encoding                    => @encoding,
    :instance_methods            => {},
    :main                        => options.main_page,
    :modules                     => [],
    :pages                       => [],
    :title                       => options.title,
  }

  @classes_hash = {}
  @modules_hash = {}
  @files_hash   = {}

  @c_enclosure_classes = {}
  @c_enclosure_names   = {}

  @c_class_variables           = {}
  @c_singleton_class_variables = {}

  @unique_classes = nil
  @unique_modules = nil
end

Instance Attribute Details

#c_class_variables ⇒ Object (readonly)

Maps C variables to class or module names for each parsed C file.



81
82
83
# File 'lib/rdoc/store.rb', line 81

def c_class_variables
  @c_class_variables
end

#c_enclosure_classes ⇒ Object (readonly)

Stores the name of the C variable a class belongs to. This helps wire up classes defined from C across files.



74
75
76
# File 'lib/rdoc/store.rb', line 74

def c_enclosure_classes
  @c_enclosure_classes
end

#c_enclosure_names ⇒ Object (readonly)

:nodoc:



76
77
78
# File 'lib/rdoc/store.rb', line 76

def c_enclosure_names
  @c_enclosure_names
end

#c_singleton_class_variables ⇒ Object (readonly)

Maps C variables to singleton class names for each parsed C file.



86
87
88
# File 'lib/rdoc/store.rb', line 86

def c_singleton_class_variables
  @c_singleton_class_variables
end

#cache ⇒ Object (readonly)

The contents of the Store



109
110
111
# File 'lib/rdoc/store.rb', line 109

def cache
  @cache
end

#dry_run ⇒ Object

If true this Store will not write any files



91
92
93
# File 'lib/rdoc/store.rb', line 91

def dry_run
  @dry_run
end

#encoding ⇒ Object

The encoding of the contents in the Store



114
115
116
# File 'lib/rdoc/store.rb', line 114

def encoding
  @encoding
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



98
99
100
# File 'lib/rdoc/store.rb', line 98

def options
  @options
end

#path ⇒ Object

Path this store reads or writes



96
97
98
# File 'lib/rdoc/store.rb', line 96

def path
  @path
end

#type ⇒ Object

Type of ri datastore this was loaded from. See RDoc::RI::Driver, RDoc::RI::Paths.



104
105
106
# File 'lib/rdoc/store.rb', line 104

def type
  @type
end

Instance Method Details

#add_c_enclosure(variable, namespace) ⇒ Object

Adds module as an enclosure (namespace) for the given variable for C files.



158
159
160
# File 'lib/rdoc/store.rb', line 158

def add_c_enclosure(variable, namespace)
  @c_enclosure_classes[variable] = namespace
end

#add_c_variables(c_parser) ⇒ Object

Adds C variables from an RDoc::Parser::C



165
166
167
168
169
170
171
# File 'lib/rdoc/store.rb', line 165

def add_c_variables(c_parser)
  filename = c_parser.top_level.relative_name

  @c_class_variables[filename] = make_variable_map c_parser.classes

  @c_singleton_class_variables[filename] = c_parser.singleton_classes
end

#add_file(absolute_name, relative_name: absolute_name, parser: nil) ⇒ Object

Adds the file with name as an RDoc::TopLevel to the store. Returns the created RDoc::TopLevel.



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rdoc/store.rb', line 177

def add_file(absolute_name, relative_name: absolute_name, parser: nil)
  unless top_level = @files_hash[relative_name]
    top_level = TopLevel.new absolute_name, relative_name
    top_level.parser = parser if parser
    top_level.store = self
    @files_hash[relative_name] = top_level
    @page_index = nil
  end

  top_level
end

#all_classes ⇒ Object

Returns all classes discovered by RDoc



319
320
321
# File 'lib/rdoc/store.rb', line 319

def all_classes
  @classes_hash.values
end

#all_classes_and_modules ⇒ Object

Returns all classes and modules discovered by RDoc



326
327
328
# File 'lib/rdoc/store.rb', line 326

def all_classes_and_modules
  @classes_hash.values + @modules_hash.values
end

#all_files ⇒ Object

All TopLevels known to RDoc



413
414
415
# File 'lib/rdoc/store.rb', line 413

def all_files
  @files_hash.values
end

#all_modules ⇒ Object

Returns all modules discovered by RDoc



420
421
422
# File 'lib/rdoc/store.rb', line 420

def all_modules
  modules_hash.values
end

#ancestors ⇒ Object

Ancestors cache accessor. Maps a klass name to an Array of its ancestors in this store. If Foo in this store inherits from Object, Kernel won't be listed (it will be included from ruby's ri store).



429
430
431
# File 'lib/rdoc/store.rb', line 429

def ancestors
  @cache[:ancestors]
end

#attributes ⇒ Object

Attributes cache accessor. Maps a class to an Array of its attributes.



436
437
438
# File 'lib/rdoc/store.rb', line 436

def attributes
  @cache[:attributes]
end

#cache_path ⇒ Object

Path to the cache file



443
444
445
# File 'lib/rdoc/store.rb', line 443

def cache_path
  File.join @path, 'cache.ri'
end

#class_file(klass_name) ⇒ Object

Path to the ri data for klass_name



450
451
452
453
# File 'lib/rdoc/store.rb', line 450

def class_file(klass_name)
  name = klass_name.split('::').last
  File.join class_path(klass_name), "cdesc-#{name}.ri"
end

#class_methods ⇒ Object

Class methods cache accessor. Maps a class to an Array of its class methods (not full name).



459
460
461
# File 'lib/rdoc/store.rb', line 459

def class_methods
  @cache[:class_methods]
end

#class_path(klass_name) ⇒ Object

Path where data for klass_name will be stored (methods or class data)



466
467
468
# File 'lib/rdoc/store.rb', line 466

def class_path(klass_name)
  File.join @path, *klass_name.split('::')
end

#classes_hash ⇒ Object

Hash of all classes known to RDoc



473
474
475
# File 'lib/rdoc/store.rb', line 473

def classes_hash
  @classes_hash
end

#clean_cache_collection(collection) ⇒ Object

Removes empty items and ensures item in each collection are unique and sorted



481
482
483
484
485
486
487
488
489
490
491
# File 'lib/rdoc/store.rb', line 481

def clean_cache_collection(collection) # :nodoc:
  collection.each do |name, item|
    if item.empty?
      collection.delete name
    else
      # HACK mongrel-1.1.5 documents its files twice
      item.uniq!
      item.sort!
    end
  end
end

#cleanup_stale_contributions ⇒ Object

Removes stale empty placeholders left by clear_file_contributions with keep_position: true. After re-parsing, a file may no longer define a class it previously contributed to, leaving an empty entry in comment_location and a stale in_files reference. Call this after all re-parsing is complete.



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rdoc/store.rb', line 282

def cleanup_stale_contributions
  all_classes_and_modules.each do |cm|
    cm.comment_location.delete_if { |_, comments| comments.empty? }
    cm.rebuild_comment_from_location

    cm.in_files.select! { |tl| cm.comment_location.key?(tl) ||
      cm.method_list.any? { |m| m.file == tl } ||
      cm.attributes.any? { |a| a.file == tl } ||
      cm.constants.any? { |c| c.file == tl } }

    if cm.in_files.empty?
      if cm.is_a?(NormalModule)
        @modules_hash.delete(cm.full_name)
      else
        @classes_hash.delete(cm.full_name)
      end
      cm.parent&.classes_hash&.delete(cm.name)
      cm.parent&.modules_hash&.delete(cm.name)
    end
  end
end

#clear_file_contributions(relative_name, keep_position: false) ⇒ Object

Removes a file's contributions (methods, constants, comments, etc.) from its classes and modules. If no other files contribute to a class or module, it is removed from the store entirely. This prevents duplication when the file is re-parsed while preserving shared namespaces like RDoc that span many files.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rdoc/store.rb', line 216

def clear_file_contributions(relative_name, keep_position: false)
  top_level = @files_hash[relative_name]
  return unless top_level

  top_level.classes_or_modules.each do |cm|
    # Remove methods and attributes contributed by this file
    cm.method_list.reject! { |m| m.file == top_level }
    cm.attributes.reject! { |a| a.file == top_level }

    # Rebuild methods_hash from remaining methods and attributes
    cm.methods_hash.clear
    cm.method_list.each { |m| cm.methods_hash[m.pretty_name] = m }
    cm.attributes.each { |a| cm.methods_hash[a.pretty_name] = a }

    # Remove constants contributed by this file
    cm.constants.reject! { |c| c.file == top_level }
    cm.constants_hash.clear
    cm.constants.each { |c| cm.constants_hash[c.name] = c }

    # Remove includes, extends, and aliases from this file
    cm.includes.reject! { |i| i.file == top_level }
    cm.extends.reject! { |e| e.file == top_level }
    cm.aliases.reject! { |a| a.file == top_level }
    cm.external_aliases.reject! { |a| a.file == top_level }

    # Clear or remove comment entries from this file
    if cm.is_a?(ClassModule)
      if keep_position
        cm.comment_location[top_level] = [] if cm.comment_location.key?(top_level)
      else
        cm.comment_location.delete(top_level)
      end
      cm.rebuild_comment_from_location
    end

    unless keep_position
      # Remove this file from the class/module's file list
      cm.in_files.delete(top_level)

      # If no files contribute to this class/module anymore, remove it
      # from the store entirely.  This handles file deletion correctly
      # for classes that are only defined in the deleted file, while
      # preserving classes that span multiple files.
      if cm.in_files.empty?
        if cm.is_a?(NormalModule)
          @modules_hash.delete(cm.full_name)
        else
          @classes_hash.delete(cm.full_name)
        end
        cm.parent&.classes_hash&.delete(cm.name)
        cm.parent&.modules_hash&.delete(cm.name)
      end
    end
  end

  # Clear the TopLevel's class/module list to prevent duplicates
  top_level.classes_or_modules.clear
end

#clear_rbs_signatures ⇒ Object

:nodoc:



406
407
408
# File 'lib/rdoc/store.rb', line 406

def clear_rbs_signatures # :nodoc:
  @rbs_signatures = nil
end

#complete(min_visibility) ⇒ Object

Prepares the RDoc code object tree for use by a generator.

It finds unique classes/modules defined, and replaces classes/modules that are aliases for another one by a copy with RDoc::ClassModule#is_alias_for set.

It updates the RDoc::ClassModule#constant_aliases attribute of "real" classes or modules.

It also completely removes the classes and modules that should be removed from the documentation and the methods that have a visibility below min_visibility, which is the --visibility option.

See also RDoc::Context#remove_from_documentation?



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/rdoc/store.rb', line 509

def complete(min_visibility)
  fix_basic_object_inheritance

  # cache included modules before they are removed from the documentation
  all_classes_and_modules.each { |cm| cm.ancestors }

  unless min_visibility == :nodoc
    remove_nodoc @classes_hash
    remove_nodoc @modules_hash
  end

  @unique_classes = find_unique @classes_hash
  @unique_modules = find_unique @modules_hash

  unique_classes_and_modules.each do |cm|
    cm.complete min_visibility
  end

  @files_hash.each_key do |file_name|
    tl = @files_hash[file_name]

    unless tl.text?
      tl.modules_hash.clear
      tl.classes_hash.clear

      tl.classes_or_modules.each do |cm|
        name = cm.full_name
        if cm.type == 'class'
          tl.classes_hash[name] = cm if @classes_hash[name]
        else
          tl.modules_hash[name] = cm if @modules_hash[name]
        end
      end
    end
  end
end

#files_hash ⇒ Object

Hash of all files known to RDoc



549
550
551
# File 'lib/rdoc/store.rb', line 549

def files_hash
  @files_hash
end

#find_c_enclosure(variable) ⇒ Object

Finds the enclosure (namespace) for the given C variable.



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/rdoc/store.rb', line 556

def find_c_enclosure(variable)
  @c_enclosure_classes.fetch variable do
    break unless name = @c_enclosure_names[variable]

    mod = find_class_or_module name

    unless mod
      loaded_mod = load_class_data name

      file = loaded_mod.in_files.first

      return unless file # legacy data source

      file.store = self

      mod = file.add_module NormalModule, name
    end

    @c_enclosure_classes[variable] = mod
  end
end

#find_class_named(name) ⇒ Object

Finds the class with name in all discovered classes



581
582
583
# File 'lib/rdoc/store.rb', line 581

def find_class_named(name)
  @classes_hash[name]
end

#find_class_named_from(name, from) ⇒ Object

Finds the class with name starting in namespace from



588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/rdoc/store.rb', line 588

def find_class_named_from(name, from)
  from = find_class_named from unless Context === from

  until TopLevel === from do
    return nil unless from

    klass = from.find_class_named name
    return klass if klass

    from = from.parent
  end

  find_class_named name
end

#find_class_or_module(name) ⇒ Object

Finds the class or module with name



606
607
608
609
# File 'lib/rdoc/store.rb', line 606

def find_class_or_module(name)
  name = $' if name =~ /^::/
  @classes_hash[name] || @modules_hash[name]
end

#find_file_named(name) ⇒ Object

Finds the file with name in all discovered files



614
615
616
# File 'lib/rdoc/store.rb', line 614

def find_file_named(name)
  @files_hash[name]
end

#find_module_named(name) ⇒ Object

Finds the module with name in all discovered modules



621
622
623
# File 'lib/rdoc/store.rb', line 621

def find_module_named(name)
  @modules_hash[name]
end

#find_text_page(file_name) ⇒ Object

Returns the RDoc::TopLevel that is a text file and has the given file_name



629
630
631
632
# File 'lib/rdoc/store.rb', line 629

def find_text_page(file_name)
  page = @files_hash[file_name]
  page if page&.text?
end

#find_unique(all_hash) ⇒ Object

Finds unique classes/modules defined in all_hash, and returns them as an array. Performs the alias updates in all_hash: see ::complete.

TODO aliases should be registered by Context#add_module_alias



641
642
643
644
645
646
647
648
649
# File 'lib/rdoc/store.rb', line 641

def find_unique(all_hash)
  unique = []

  all_hash.each_pair do |full_name, cm|
    unique << cm if full_name == cm.full_name
  end

  unique
end

#fix_basic_object_inheritance ⇒ Object

Fixes the erroneous BasicObject < Object in 1.9.

Because we assumed all classes without a stated superclass inherit from Object, we have the above wrong inheritance.

We fix BasicObject right away if we are running in a Ruby version >= 1.9.



660
661
662
663
664
# File 'lib/rdoc/store.rb', line 660

def fix_basic_object_inheritance
  basic = classes_hash['BasicObject']
  return unless basic
  basic.superclass = nil
end

#friendly_path ⇒ Object

Friendly rendition of #path



669
670
671
672
673
674
675
676
677
678
679
# File 'lib/rdoc/store.rb', line 669

def friendly_path
  case type
  when :gem
    parent = File.expand_path '..', @path
    "gem #{File.basename parent}"
  when :home   then ::RDoc.home
  when :site   then 'ruby site'
  when :system then 'ruby core'
  else @path
  end
end

#inspect ⇒ Object

:nodoc:



681
682
683
# File 'lib/rdoc/store.rb', line 681

def inspect # :nodoc:
  "#<%s:0x%x %s %p>" % [self.class, object_id, @path, module_names.sort]
end

#instance_methods ⇒ Object

Instance methods cache accessor. Maps a class to an Array of its instance methods (not full name).



689
690
691
# File 'lib/rdoc/store.rb', line 689

def instance_methods
  @cache[:instance_methods]
end

#invalidate_type_name_lookup ⇒ Object

Invalidates the cached type name lookup. Server mode calls this after re-parsing changes the set of classes and modules.



341
342
343
# File 'lib/rdoc/store.rb', line 341

def invalidate_type_name_lookup # :nodoc:
  @type_name_lookup = nil
end

#load_all ⇒ Object

Loads all items from this store into memory. This recreates a documentation tree for use by a generator



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/rdoc/store.rb', line 697

def load_all
  load_cache

  module_names.each do |module_name|
    mod = find_class_or_module(module_name) || load_class(module_name)

    # load method documentation since the loaded class/module does not have
    # it
    loaded_methods = mod.method_list.map do |method|
      load_method module_name, method.full_name
    end

    mod.method_list.replace loaded_methods

    loaded_attributes = mod.attributes.map do |attribute|
      load_method module_name, attribute.full_name
    end

    mod.attributes.replace loaded_attributes
  end

  all_classes_and_modules.each do |mod|
    descendent_re = /^#{mod.full_name}::[^:]+$/

    module_names.each do |name|
      next unless name =~ descendent_re

      descendent = find_class_or_module name

      case descendent
      when NormalClass
        mod.classes_hash[name] = descendent
      when NormalModule
        mod.modules_hash[name] = descendent
      end
    end
  end

  @page_index = nil
  @cache[:pages].each do |page_name|
    @files_hash[page_name] = load_page(page_name)
  end
end

#load_cache ⇒ Object

Loads cache file for this store



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/rdoc/store.rb', line 744

def load_cache
  #orig_enc = @encoding

  @cache = marshal_load(cache_path)

  load_enc = @cache[:encoding]

  # TODO this feature will be time-consuming to add:
  # a) Encodings may be incompatible but transcodeable
  # b) Need to warn in the appropriate spots, wherever they may be
  # c) Need to handle cross-cache differences in encodings
  # d) Need to warn when generating into a cache with different encodings
  #
  #if orig_enc and load_enc != orig_enc then
  #  warn "Cached encoding #{load_enc} is incompatible with #{orig_enc}\n" \
  #       "from #{path}/cache.ri" unless
  #    Encoding.compatible? orig_enc, load_enc
  #end

  @encoding = load_enc unless @encoding

  @cache[:pages]                       ||= []
  @cache[:main]                        ||= nil
  @cache[:c_class_variables]           ||= {}
  @cache[:c_singleton_class_variables] ||= {}

  @cache[:c_class_variables].each do |_, map|
    map.each do |variable, name|
      @c_enclosure_names[variable] = name
    end
  end

  @cache
rescue Errno::ENOENT
end

#load_class(klass_name) ⇒ Object

Loads ri data for klass_name and hooks it up to this store.



783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/rdoc/store.rb', line 783

def load_class(klass_name)
  obj = load_class_data klass_name

  obj.store = self

  case obj
  when NormalClass
    @classes_hash[klass_name] = obj
  when SingleClass
    @classes_hash[klass_name] = obj
  when NormalModule
    @modules_hash[klass_name] = obj
  end
end

#load_class_data(klass_name) ⇒ Object

Loads ri data for klass_name



801
802
803
804
805
806
807
808
809
# File 'lib/rdoc/store.rb', line 801

def load_class_data(klass_name)
  file = class_file klass_name

  marshal_load(file)
rescue Errno::ENOENT => e
  error = MissingFileError.new(self, file, klass_name)
  error.set_backtrace e.backtrace
  raise error
end

#load_method(klass_name, method_name) ⇒ Object

Loads ri data for method_name in klass_name



814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/rdoc/store.rb', line 814

def load_method(klass_name, method_name)
  file = method_file klass_name, method_name

  obj = marshal_load(file)
  obj.store = self
  obj.parent ||= find_class_or_module(klass_name) || load_class(klass_name)
  obj
rescue Errno::ENOENT => e
  error = MissingFileError.new(self, file, klass_name + method_name)
  error.set_backtrace e.backtrace
  raise error
end

#load_page(page_name) ⇒ Object

Loads ri data for page_name



830
831
832
833
834
835
836
837
838
839
840
# File 'lib/rdoc/store.rb', line 830

def load_page(page_name)
  file = page_file page_name

  obj = marshal_load(file)
  obj.store = self
  obj
rescue Errno::ENOENT => e
  error = MissingFileError.new(self, file, page_name)
  error.set_backtrace e.backtrace
  raise error
end

#main ⇒ Object

Gets the main page for this RDoc store. This page is used as the root of the RDoc server.



846
847
848
# File 'lib/rdoc/store.rb', line 846

def main
  @cache[:main]
end

#main=(page) ⇒ Object

Sets the main page for this RDoc store.



853
854
855
# File 'lib/rdoc/store.rb', line 853

def main=(page)
  @cache[:main] = page
end

#make_variable_map(variables) ⇒ Object

Converts the variable => ClassModule map variables from a C parser into a variable => class name map.



861
862
863
864
865
866
867
868
869
# File 'lib/rdoc/store.rb', line 861

def make_variable_map(variables)
  map = {}

  variables.each { |variable, class_module|
    map[variable] = class_module.full_name
  }

  map
end

#merge_rbs_signatures(signatures) ⇒ Object

Stores RBS type signatures loaded from sidecar .rbs files, keyed by "ClassName#method" or "ClassName.method". Replaces any previously stored set, so passing {} clears it. Inline #: annotations on method objects are NOT touched — those are owned by the source file.



373
374
375
# File 'lib/rdoc/store.rb', line 373

def merge_rbs_signatures(signatures)
  @rbs_signatures = signatures
end

#method_file(klass_name, method_name) ⇒ Object

Path to the ri data for method_name in klass_name



874
875
876
877
878
879
880
881
882
# File 'lib/rdoc/store.rb', line 874

def method_file(klass_name, method_name)
  method_name = method_name.split('::').last
  method_name =~ /#(.*)/
  method_type = $1 ? 'i' : 'c'
  method_name = $1 if $1
  method_name = method_name.gsub(/\W/) { "%%%02x" % $&[0].ord }

  File.join class_path(klass_name), "#{method_name}-#{method_type}.ri"
end

#module_names ⇒ Object

Modules cache accessor. An Array of all the module (and class) names in the store.



888
889
890
# File 'lib/rdoc/store.rb', line 888

def module_names
  @cache[:modules]
end

#modules_hash ⇒ Object

Hash of all modules known to RDoc



895
896
897
# File 'lib/rdoc/store.rb', line 895

def modules_hash
  @modules_hash
end

#page(name) ⇒ Object

Returns the RDoc::TopLevel that is a file and has the given name



902
903
904
905
906
907
908
909
# File 'lib/rdoc/store.rb', line 902

def page(name)
  @page_index ||= @files_hash.each_value.each_with_object({}) do |file, index|
    index[file.page_name] ||= file
    index[file.base_name] ||= file
  end

  @page_index[name]
end

#page_file(page_name) ⇒ Object

Path to the ri data for page_name



914
915
916
917
918
# File 'lib/rdoc/store.rb', line 914

def page_file(page_name)
  file_name = File.basename(page_name).gsub('.', '_')

  File.join @path, File.dirname(page_name), "page-#{file_name}.ri"
end

#rbs_signature_for(method_attr) ⇒ Object

Returns the RBS type signature lines for method_attr from loaded sidecar .rbs files, or nil if none. Falls through to the canonical method for aliases, and handles initialize -> .new singleton mapping.



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/rdoc/store.rb', line 383

def rbs_signature_for(method_attr)
  return nil unless @rbs_signatures
  cm = method_attr.parent
  return nil unless cm.respond_to?(:full_name)

  key = method_attr.singleton ? "#{cm.full_name}.#{method_attr.name}" : "#{cm.full_name}##{method_attr.name}"
  sig = @rbs_signatures[key]

  # RBS keys constructors as #initialize, but RDoc renames them to .new
  if !sig && method_attr.name == 'new' && method_attr.singleton
    sig = @rbs_signatures["#{cm.full_name}#initialize"]
  end

  # For aliases, fall through to the canonical method (its inline #:
  # takes precedence over any sidecar signature on the alias's name).
  if !sig && method_attr.is_alias_for
    canonical = method_attr.is_alias_for
    sig = canonical.type_signature_lines || rbs_signature_for(canonical)
  end

  sig
end

#remove_file(relative_name) ⇒ Object

Removes a file and its classes/modules from the store. Used by the live-reloading server when a source file is deleted.

Note: this does not handle reopened classes correctly. If a class is defined across multiple files (e.g. Foo in both a.rb and b.rb), deleting one file removes the entire class from the store — including the parts contributed by the other file. Saving the remaining file triggers a re-parse that restores it.



199
200
201
202
203
204
205
206
207
# File 'lib/rdoc/store.rb', line 199

def remove_file(relative_name)
  top_level = @files_hash.delete(relative_name)
  @c_class_variables.delete(relative_name)
  @c_singleton_class_variables.delete(relative_name)
  return unless top_level

  @page_index = nil
  remove_classes_and_modules(top_level.classes_or_modules)
end

#remove_nodoc(all_hash) ⇒ Object

Removes from all_hash the contexts that are nodoc or have no content.

See RDoc::Context#remove_from_documentation?



925
926
927
928
929
930
# File 'lib/rdoc/store.rb', line 925

def remove_nodoc(all_hash)
  all_hash.keys.each do |name|
    context = all_hash[name]
    all_hash.delete(name) if context.remove_from_documentation?
  end
end

#resolve_c_superclasses ⇒ Object

Make sure any references to C variable names are resolved to the corresponding class.



308
309
310
311
312
313
314
# File 'lib/rdoc/store.rb', line 308

def resolve_c_superclasses
  @classes_hash.each_value do |klass|
    if klass.superclass.is_a?(String) && (candidate = find_c_enclosure(klass.superclass))
      klass.superclass = candidate
    end
  end
end

#save ⇒ Object

Saves all entries in the store



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'lib/rdoc/store.rb', line 935

def save
  load_cache

  all_classes_and_modules.each do |klass|
    save_class klass

    klass.each_method do |method|
      save_method klass, method
    end

    klass.attributes.each do |attribute|
      save_method klass, attribute
    end
  end

  all_files.each do |file|
    save_page file
  end

  save_cache
end

#save_cache ⇒ Object

Writes the cache file for this store



960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/rdoc/store.rb', line 960

def save_cache
  clean_cache_collection @cache[:ancestors]
  clean_cache_collection @cache[:attributes]
  clean_cache_collection @cache[:class_methods]
  clean_cache_collection @cache[:instance_methods]

  @cache[:modules].uniq!
  @cache[:modules].sort!

  @cache[:pages].uniq!
  @cache[:pages].sort!

  @cache[:encoding] = @encoding # this gets set twice due to assert_cache

  @cache[:c_class_variables].merge!           @c_class_variables
  @cache[:c_singleton_class_variables].merge! @c_singleton_class_variables

  return if @dry_run

  File.open cache_path, 'wb' do |io|
    Marshal.dump @cache, io
  end
end

#save_class(klass) ⇒ Object

Writes the ri data for klass (or module)



987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/rdoc/store.rb', line 987

def save_class(klass)
  full_name = klass.full_name

  FileUtils.mkdir_p class_path(full_name) unless @dry_run

  @cache[:modules] << full_name

  path = class_file full_name

  begin
    disk_klass = load_class full_name

    klass = disk_klass.merge klass
  rescue MissingFileError
  end

  # BasicObject has no ancestors
  ancestors = klass.direct_ancestors.compact.map do |ancestor|
    # HACK for classes we don't know about (class X < RuntimeError)
    String === ancestor ? ancestor : ancestor.full_name
  end

  @cache[:ancestors][full_name] ||= []
  @cache[:ancestors][full_name].concat ancestors

  attribute_definitions = klass.attributes.map do |attribute|
    "#{attribute.definition} #{attribute.name}"
  end

  unless attribute_definitions.empty?
    @cache[:attributes][full_name] ||= []
    @cache[:attributes][full_name].concat attribute_definitions
  end

  to_delete = []

  unless klass.method_list.empty?
    @cache[:class_methods][full_name]    ||= []
    @cache[:instance_methods][full_name] ||= []

    class_methods, instance_methods =
      klass.method_list.partition { |meth| meth.singleton }

    class_methods    = class_methods.   map { |method| method.name }
    instance_methods = instance_methods.map { |method| method.name }
    attribute_names  = klass.attributes.map { |attr|   attr.name }

    old = @cache[:class_methods][full_name] - class_methods
    to_delete.concat old.map { |method|
      method_file full_name, "#{full_name}::#{method}"
    }

    old = @cache[:instance_methods][full_name] -
      instance_methods - attribute_names
    to_delete.concat old.map { |method|
      method_file full_name, "#{full_name}##{method}"
    }

    @cache[:class_methods][full_name]    = class_methods
    @cache[:instance_methods][full_name] = instance_methods
  end

  return if @dry_run

  FileUtils.rm_f to_delete

  File.open path, 'wb' do |io|
    Marshal.dump klass, io
  end
end

#save_method(klass, method) ⇒ Object

Writes the ri data for method on klass



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# File 'lib/rdoc/store.rb', line 1061

def save_method(klass, method)
  full_name = klass.full_name

  FileUtils.mkdir_p class_path(full_name) unless @dry_run

  cache = if method.singleton
            @cache[:class_methods]
          else
            @cache[:instance_methods]
          end
  cache[full_name] ||= []
  cache[full_name] << method.name

  return if @dry_run

  File.open method_file(full_name, method.full_name), 'wb' do |io|
    Marshal.dump method, io
  end
end

#save_page(page) ⇒ Object

Writes the ri data for page



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/rdoc/store.rb', line 1084

def save_page(page)
  return unless page.text?

  path = page_file page.full_name

  FileUtils.mkdir_p File.dirname(path) unless @dry_run

  cache[:pages] ||= []
  cache[:pages] << page.full_name

  return if @dry_run

  File.open path, 'wb' do |io|
    Marshal.dump page, io
  end
end

#source ⇒ Object

Source of the contents of this store.

For a store from a gem the source is the gem name. For a store from the home directory the source is "home". For system ri store (the standard library documentation) the source is"ruby". For a store from the site ri directory the store is "site". For other stores the source is the #path.



1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/rdoc/store.rb', line 1110

def source
  case type
  when :gem    then File.basename File.expand_path '..', @path
  when :home   then 'home'
  when :site   then 'site'
  when :system then 'ruby'
  else @path
  end
end

#title ⇒ Object

Gets the title for this RDoc store. This is used as the title in each page on the RDoc server



1124
1125
1126
# File 'lib/rdoc/store.rb', line 1124

def title
  @cache[:title]
end

#title=(title) ⇒ Object

Sets the title page for this RDoc store.



1131
1132
1133
# File 'lib/rdoc/store.rb', line 1131

def title=(title)
  @cache[:title] = title
end

#type_name_lookup ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/rdoc/store.rb', line 345

def type_name_lookup
  @type_name_lookup ||= begin
    lookup = {}
    unqualified_names = {}
    ambiguous_names = {}
    all_classes_and_modules.each do |cm|
      lookup[cm.full_name] = cm.path
      unqualified_name = cm.name

      if ambiguous_names[unqualified_name]
        # already known ambiguous, skip
      elsif unqualified_names.key?(unqualified_name)
        unqualified_names.delete(unqualified_name)
        ambiguous_names[unqualified_name] = true
      else
        unqualified_names[unqualified_name] = cm.path
      end
    end
    lookup.merge!(unqualified_names)
  end
end

#unique_classes ⇒ Object

Returns the unique classes discovered by RDoc.

::complete must have been called prior to using this method.



1140
1141
1142
# File 'lib/rdoc/store.rb', line 1140

def unique_classes
  @unique_classes
end

#unique_classes_and_modules ⇒ Object

Returns the unique classes and modules discovered by RDoc. ::complete must have been called prior to using this method.



1148
1149
1150
# File 'lib/rdoc/store.rb', line 1148

def unique_classes_and_modules
  @unique_classes + @unique_modules
end

#unique_modules ⇒ Object

Returns the unique modules discovered by RDoc. ::complete must have been called prior to using this method.



1156
1157
1158
# File 'lib/rdoc/store.rb', line 1156

def unique_modules
  @unique_modules
end