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 15 def self.[](path) 16 (@indexes ||= {})[index_path_for_file(path)] ||= 17 Index.new(index_path_for_file(path)) 18 end
index_path_for_file(file)
click to toggle source
# File lib/ronn/index.rb 20 def self.index_path_for_file(file) 21 File.expand_path( 22 if File.directory?(file) 23 File.join(file, 'index.txt') 24 else 25 File.join(File.dirname(file), 'index.txt') 26 end 27 ) 28 end
new(path) { || ... }
click to toggle source
# File lib/ronn/index.rb 30 def initialize(path, &bk) 31 @path = path 32 @references = [] 33 @manuals = {} 34 35 if block_given? 36 read! yield 37 elsif exist? 38 read! File.read(path) 39 end 40 end
Public Instance Methods
<<(path)
click to toggle source
# File lib/ronn/index.rb 89 def <<(path) 90 raise ArgumentError, "local paths only" if path =~ /(https?|mailto):/ 91 return self if any? { |ref| ref.path == File.expand_path(path) } 92 relative_path = relative_to_index(path) 93 @references << \ 94 if path =~ /\.ronn?$/ 95 reference manual(path).reference_name, relative_path 96 else 97 reference File.basename(path), relative_path 98 end 99 self 100 end
[](name)
click to toggle source
# File lib/ronn/index.rb 81 def [](name) 82 references.find { |ref| ref.name == name } 83 end
add_manual(manual)
click to toggle source
# File lib/ronn/index.rb 102 def add_manual(manual) 103 @manuals[File.expand_path(manual.path)] = manual 104 self << manual.path 105 end
each(&bk)
click to toggle source
Enumerable and friends
# File lib/ronn/index.rb 61 def each(&bk) 62 references.each(&bk) 63 end
empty?()
click to toggle source
# File lib/ronn/index.rb 77 def empty? 78 references.empty? 79 end
exist?()
click to toggle source
Determine whether the index file exists.
# File lib/ronn/index.rb 43 def exist? 44 File.exist?(path) 45 end
first()
click to toggle source
# File lib/ronn/index.rb 69 def first 70 references.first 71 end
last()
click to toggle source
# File lib/ronn/index.rb 73 def last 74 references.last 75 end
manual(path)
click to toggle source
# File lib/ronn/index.rb 107 def manual(path) 108 @manuals[File.expand_path(path)] ||= Document.new(path) 109 end
manuals()
click to toggle source
# File lib/ronn/index.rb 111 def manuals 112 select { |ref| ref.relative? && ref.ronn? }. 113 map { |ref| manual(ref.path) } 114 end
read!(data)
click to toggle source
Load index data from a string.
# File lib/ronn/index.rb 48 def read!(data) 49 data.each_line do |line| 50 line = line.strip.gsub(/\s*#.*$/, '') 51 if !line.empty? 52 name, url = line.split(/ +/, 2) 53 @references << reference(name, url) 54 end 55 end 56 end
reference(name, path)
click to toggle source
# File lib/ronn/index.rb 85 def reference(name, path) 86 Reference.new(self, name, path) 87 end
relative_to_index(path)
click to toggle source
# File lib/ronn/index.rb 131 def relative_to_index(path) 132 path = File.expand_path(path) 133 index_dir = File.dirname(File.expand_path(self.path)) 134 path.sub(/^#{index_dir}\//, '') 135 end
size()
click to toggle source
# File lib/ronn/index.rb 65 def size 66 references.size 67 end
to_a()
click to toggle source
# File lib/ronn/index.rb 123 def to_a 124 references 125 end
to_h()
click to toggle source
# File lib/ronn/index.rb 127 def to_h 128 to_a.map { |doc| doc.to_hash } 129 end
to_text()
click to toggle source
Converting
# File lib/ronn/index.rb 119 def to_text 120 map { |ref| [ref.name, ref.location].join(' ') }.join("\n") 121 end