Class Ronn::Index
In: lib/ronn/index.rb
Parent: Object

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

Methods

<<   []   []   add_manual   each   empty?   exist?   first   index_path_for_file   last   manual   manuals   new   read!   reference   relative_to_index   size   to_a   to_h   to_text  

Included Modules

Enumerable

Attributes

path  [R] 
references  [R] 

Public Class methods

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.

[Source]

    # File lib/ronn/index.rb, line 15
15:     def self.[](path)
16:       (@indexes ||= {})[index_path_for_file(path)] ||=
17:         Index.new(index_path_for_file(path))
18:     end

[Source]

    # File lib/ronn/index.rb, line 20
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

[Source]

    # File lib/ronn/index.rb, line 30
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

[Source]

     # File lib/ronn/index.rb, line 89
 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

[Source]

    # File lib/ronn/index.rb, line 81
81:     def [](name)
82:       references.find { |ref| ref.name == name }
83:     end

[Source]

     # File lib/ronn/index.rb, line 102
102:     def add_manual(manual)
103:       @manuals[File.expand_path(manual.path)] = manual
104:       self << manual.path
105:     end

Enumerable and friends

[Source]

    # File lib/ronn/index.rb, line 61
61:     def each(&bk)
62:       references.each(&bk)
63:     end

[Source]

    # File lib/ronn/index.rb, line 77
77:     def empty?
78:       references.empty?
79:     end

Determine whether the index file exists.

[Source]

    # File lib/ronn/index.rb, line 43
43:     def exist?
44:       File.exist?(path)
45:     end

[Source]

    # File lib/ronn/index.rb, line 69
69:     def first
70:       references.first
71:     end

[Source]

    # File lib/ronn/index.rb, line 73
73:     def last
74:       references.last
75:     end

[Source]

     # File lib/ronn/index.rb, line 107
107:     def manual(path)
108:       @manuals[File.expand_path(path)] ||= Document.new(path)
109:     end

[Source]

     # File lib/ronn/index.rb, line 111
111:     def manuals
112:       select { |ref| ref.relative? && ref.ronn? }.
113:       map    { |ref| manual(ref.path) }
114:     end

Load index data from a string.

[Source]

    # File lib/ronn/index.rb, line 48
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

[Source]

    # File lib/ronn/index.rb, line 85
85:     def reference(name, path)
86:       Reference.new(self, name, path)
87:     end

[Source]

     # File lib/ronn/index.rb, line 131
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

[Source]

    # File lib/ronn/index.rb, line 65
65:     def size
66:       references.size
67:     end

[Source]

     # File lib/ronn/index.rb, line 123
123:     def to_a
124:       references
125:     end

[Source]

     # File lib/ronn/index.rb, line 127
127:     def to_h
128:       to_a.map { |doc| doc.to_hash }
129:     end

Converting

[Source]

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

[Validate]