class Ronn::Index

Maintains a list of links / references to manuals and other resources.

Attributes

path[R]
references[R]

Public Class Methods

[](path) click to toggle source

Retrieve an Index for <path>, where <path> is a directory or normal file. The index is loaded from the corresponding index.txt file if one exists.

   # File lib/ronn/index.rb
14 def self.[](path)
15   (@indexes ||= {})[index_path_for_file(path)] ||=
16     Index.new(index_path_for_file(path))
17 end
index_path_for_file(file) click to toggle source
   # File lib/ronn/index.rb
19 def self.index_path_for_file(file)
20   File.expand_path(
21     if File.directory?(file)
22       File.join(file, 'index.txt')
23     else
24       File.join(File.dirname(file), 'index.txt')
25     end
26   )
27 end
new(path) { || ... } click to toggle source
   # File lib/ronn/index.rb
29 def initialize(path)
30   @path = path
31   @references = []
32   @manuals    = {}
33 
34   if block_given?
35     read! yield
36   elsif exist?
37     read! File.read(path)
38   end
39 end

Public Instance Methods

<<(path) click to toggle source
   # File lib/ronn/index.rb
88 def <<(path)
89   raise ArgumentError, 'local paths only' if path =~ /(https?|mailto):/
90   return self if any? { |ref| ref.path == File.expand_path(path) }
91   relative_path = relative_to_index(path)
92   @references << \
93     if path =~ /\.ronn?$/
94       reference manual(path).reference_name, relative_path
95     else
96       reference File.basename(path), relative_path
97     end
98   self
99 end
[](name) click to toggle source
   # File lib/ronn/index.rb
80 def [](name)
81   references.find { |ref| ref.name == name }
82 end
add_manual(manual) click to toggle source
    # File lib/ronn/index.rb
101 def add_manual(manual)
102   @manuals[File.expand_path(manual.path)] = manual
103   self << manual.path
104 end
each(&block) click to toggle source

Enumerable and friends

   # File lib/ronn/index.rb
60 def each(&block)
61   references.each(&block)
62 end
empty?() click to toggle source
   # File lib/ronn/index.rb
76 def empty?
77   references.empty?
78 end
exist?() click to toggle source

Determine whether the index file exists.

   # File lib/ronn/index.rb
42 def exist?
43   File.exist?(path)
44 end
first() click to toggle source
   # File lib/ronn/index.rb
68 def first
69   references.first
70 end
last() click to toggle source
   # File lib/ronn/index.rb
72 def last
73   references.last
74 end
manual(path) click to toggle source
    # File lib/ronn/index.rb
106 def manual(path)
107   @manuals[File.expand_path(path)] ||= Document.new(path)
108 end
manuals() click to toggle source
    # File lib/ronn/index.rb
110 def manuals
111   select { |ref| ref.relative? && ref.ronn? }
112     .map { |ref| manual(ref.path) }
113 end
read!(data) click to toggle source

Load index data from a string.

   # File lib/ronn/index.rb
47 def read!(data)
48   data.each_line do |line|
49     line = line.strip.gsub(/\s*#.*$/, '')
50     unless line.empty?
51       name, url = line.split(/\s+/, 2)
52       @references << reference(name, url)
53     end
54   end
55 end
reference(name, path) click to toggle source
   # File lib/ronn/index.rb
84 def reference(name, path)
85   Reference.new(self, name, path)
86 end
relative_to_index(path) click to toggle source
    # File lib/ronn/index.rb
130 def relative_to_index(path)
131   path = File.expand_path(path)
132   index_dir = File.dirname(File.expand_path(self.path))
133   path.sub(/^#{index_dir}\//, '')
134 end
size() click to toggle source
   # File lib/ronn/index.rb
64 def size
65   references.size
66 end
to_a() click to toggle source
    # File lib/ronn/index.rb
122 def to_a
123   references
124 end
to_h() click to toggle source
    # File lib/ronn/index.rb
126 def to_h
127   to_a.map(&:to_hash)
128 end
to_text() click to toggle source

Converting

    # File lib/ronn/index.rb
118 def to_text
119   map { |ref| [ref.name, ref.location].join(' ') }.join("\n")
120 end