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

Public Instance Methods

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

Enumerable and friends

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

Determine whether the index file exists.

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

Load index data from a string.

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

Converting

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