Class: Ronn::Index
Overview
Maintains a list of links / references to manuals and other resources.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
Class Method Summary collapse
-
.[](path) ⇒ Object
Retrieve an Index for <path>, where <path> is a directory or normal file.
- .index_path_for_file(file) ⇒ Object
Instance Method Summary collapse
- #<<(path) ⇒ Object
- #[](name) ⇒ Object
- #add_manual(manual) ⇒ Object
-
#each(&bk) ⇒ Object
Enumerable and friends.
- #empty? ⇒ Boolean
-
#exist? ⇒ Boolean
Determine whether the index file exists.
- #first ⇒ Object
-
#initialize(path, &bk) ⇒ Index
constructor
A new instance of Index.
- #last ⇒ Object
- #manual(path) ⇒ Object
- #manuals ⇒ Object
-
#read!(data) ⇒ Object
Load index data from a string.
- #reference(name, path) ⇒ Object
- #relative_to_index(path) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
-
#to_text ⇒ Object
Converting.
Constructor Details
#initialize(path, &bk) ⇒ Index
Returns a new instance of Index.
30 31 32 33 34 35 36 37 38 39 40 |
# 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 |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/ronn/index.rb', line 9 def path @path end |
#references ⇒ Object (readonly)
Returns the value of attribute references.
10 11 12 |
# File 'lib/ronn/index.rb', line 10 def references @references end |
Class Method Details
.[](path) ⇒ Object
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.
15 16 17 18 |
# 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) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/ronn/index.rb', line 20 def self.index_path_for_file(file) File.( if File.directory?(file) File.join(file, 'index.txt') else File.join(File.dirname(file), 'index.txt') end ) end |
Instance Method Details
#<<(path) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# 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.(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) ⇒ Object
81 82 83 |
# File 'lib/ronn/index.rb', line 81 def [](name) references.find { |ref| ref.name == name } end |
#add_manual(manual) ⇒ Object
102 103 104 105 |
# File 'lib/ronn/index.rb', line 102 def add_manual(manual) @manuals[File.(manual.path)] = manual self << manual.path end |
#each(&bk) ⇒ Object
Enumerable and friends
61 62 63 |
# File 'lib/ronn/index.rb', line 61 def each(&bk) references.each(&bk) end |
#empty? ⇒ Boolean
77 78 79 |
# File 'lib/ronn/index.rb', line 77 def empty? references.empty? end |
#exist? ⇒ Boolean
Determine whether the index file exists.
43 44 45 |
# File 'lib/ronn/index.rb', line 43 def exist? File.exist?(path) end |
#first ⇒ Object
69 70 71 |
# File 'lib/ronn/index.rb', line 69 def first references.first end |
#last ⇒ Object
73 74 75 |
# File 'lib/ronn/index.rb', line 73 def last references.last end |
#manual(path) ⇒ Object
107 108 109 |
# File 'lib/ronn/index.rb', line 107 def manual(path) @manuals[File.(path)] ||= Document.new(path) end |
#manuals ⇒ Object
111 112 113 114 |
# File 'lib/ronn/index.rb', line 111 def manuals select { |ref| ref.relative? && ref.ronn? }. map { |ref| manual(ref.path) } end |
#read!(data) ⇒ Object
Load index data from a string.
48 49 50 51 52 53 54 55 56 |
# 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) ⇒ Object
85 86 87 |
# File 'lib/ronn/index.rb', line 85 def reference(name, path) Reference.new(self, name, path) end |
#relative_to_index(path) ⇒ Object
131 132 133 134 135 |
# File 'lib/ronn/index.rb', line 131 def relative_to_index(path) path = File.(path) index_dir = File.dirname(File.(self.path)) path.sub(/^#{index_dir}\//, '') end |
#size ⇒ Object
65 66 67 |
# File 'lib/ronn/index.rb', line 65 def size references.size end |
#to_a ⇒ Object
123 124 125 |
# File 'lib/ronn/index.rb', line 123 def to_a references end |
#to_h ⇒ Object
127 128 129 |
# File 'lib/ronn/index.rb', line 127 def to_h to_a.map { |doc| doc.to_hash } end |
#to_text ⇒ Object
Converting
119 120 121 |
# File 'lib/ronn/index.rb', line 119 def to_text map { |ref| [ref.name, ref.location].join(' ') }.join("\n") end |