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, line 15
def self.[](path)
  (@indexes ||= {})[index_path_for_file(path)] ||=
    Index.new(index_path_for_file(path))
end
index_path_for_file(file) click to toggle source
# File lib/ronn/index.rb, line 20
def self.index_path_for_file(file)
  File.expand_path(
    if File.directory?(file)
      File.join(file, 'index.txt')
    else
      File.join(File.dirname(file), 'index.txt')
    end
  )
end
new(path) { || ... } click to toggle source
# File lib/ronn/index.rb, line 30
def initialize(path, &bk)
  @path = path
  @references = []
  @manuals    = {}

  if block_given?
    read! yield
  elsif exist?
    read! File.read(path)
  end
end

Public Instance Methods

<<(path) click to toggle source
# File lib/ronn/index.rb, line 89
def <<(path)
  raise ArgumentError, "local paths only" if path =~ /(https?|mailto):/
  return self if any? { |ref| ref.path == File.expand_path(path) }
  relative_path = relative_to_index(path)
  @references << \
    if path =~ /\.ronn?$/
      reference manual(path).reference_name, relative_path
    else
      reference File.basename(path), relative_path
    end
  self
end
[](name) click to toggle source
# File lib/ronn/index.rb, line 81
def [](name)
  references.find { |ref| ref.name == name }
end
add_manual(manual) click to toggle source
# File lib/ronn/index.rb, line 102
def add_manual(manual)
  @manuals[File.expand_path(manual.path)] = manual
  self << manual.path
end
each(&bk) click to toggle source

Enumerable and friends

# File lib/ronn/index.rb, line 61
def each(&bk)
  references.each(&bk)
end
empty?() click to toggle source
# File lib/ronn/index.rb, line 77
def empty?
  references.empty?
end
exist?() click to toggle source

Determine whether the index file exists.

# File lib/ronn/index.rb, line 43
def exist?
  File.exist?(path)
end
first() click to toggle source
# File lib/ronn/index.rb, line 69
def first
  references.first
end
last() click to toggle source
# File lib/ronn/index.rb, line 73
def last
  references.last
end
manual(path) click to toggle source
# File lib/ronn/index.rb, line 107
def manual(path)
  @manuals[File.expand_path(path)] ||= Document.new(path)
end
manuals() click to toggle source
# File lib/ronn/index.rb, line 111
def manuals
  select { |ref| ref.relative? && ref.ronn? }.
  map    { |ref| manual(ref.path) }
end
read!(data) click to toggle source

Load index data from a string.

# File lib/ronn/index.rb, line 48
def read!(data)
  data.each_line do |line|
    line = line.strip.gsub(/\s*#.*$/, '')
    if !line.empty?
      name, url = line.split(/ +/, 2)
      @references << reference(name, url)
    end
  end
end
reference(name, path) click to toggle source
# File lib/ronn/index.rb, line 85
def reference(name, path)
  Reference.new(self, name, path)
end
relative_to_index(path) click to toggle source
# File lib/ronn/index.rb, line 131
def relative_to_index(path)
  path = File.expand_path(path)
  index_dir = File.dirname(File.expand_path(self.path))
  path.sub(/^#{index_dir}\//, '')
end
size() click to toggle source
# File lib/ronn/index.rb, line 65
def size
  references.size
end
to_a() click to toggle source
# File lib/ronn/index.rb, line 123
def to_a
  references
end
to_h() click to toggle source
# File lib/ronn/index.rb, line 127
def to_h
  to_a.map { |doc| doc.to_hash }
end
to_text() click to toggle source

Converting

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