Class: Ronn::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronn/index.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Index

Returns a new instance of Index.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ronn/index.rb', line 28

def initialize(path)
  @path = path
  @references = []
  @manuals    = {}

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/ronn/index.rb', line 8

def path
  @path
end

#referencesObject (readonly)

Returns the value of attribute references.



8
9
10
# File 'lib/ronn/index.rb', line 8

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.



13
14
15
16
# File 'lib/ronn/index.rb', line 13

def self.[](path)
  (@indexes ||= {})[index_path_for_file(path)] ||=
    Index.new(index_path_for_file(path))
end

.index_path_for_file(file) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/ronn/index.rb', line 18

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

Instance Method Details

#<<(path) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ronn/index.rb', line 87

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) ⇒ Object



79
80
81
# File 'lib/ronn/index.rb', line 79

def [](name)
  references.find { |ref| ref.name == name }
end

#add_manual(manual) ⇒ Object



100
101
102
103
# File 'lib/ronn/index.rb', line 100

def add_manual(manual)
  @manuals[File.expand_path(manual.path)] = manual
  self << manual.path
end

#each(&block) ⇒ Object

Enumerable and friends



59
60
61
# File 'lib/ronn/index.rb', line 59

def each(&block)
  references.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ronn/index.rb', line 75

def empty?
  references.empty?
end

#exist?Boolean

Determine whether the index file exists.

Returns:

  • (Boolean)


41
42
43
# File 'lib/ronn/index.rb', line 41

def exist?
  File.exist?(path)
end

#firstObject



67
68
69
# File 'lib/ronn/index.rb', line 67

def first
  references.first
end

#lastObject



71
72
73
# File 'lib/ronn/index.rb', line 71

def last
  references.last
end

#manual(path) ⇒ Object



105
106
107
# File 'lib/ronn/index.rb', line 105

def manual(path)
  @manuals[File.expand_path(path)] ||= Document.new(path)
end

#manualsObject



109
110
111
112
# File 'lib/ronn/index.rb', line 109

def manuals
  select { |ref| ref.relative? && ref.ronn? }
    .map { |ref| manual(ref.path) }
end

#read!(data) ⇒ Object

Load index data from a string.



46
47
48
49
50
51
52
53
54
# File 'lib/ronn/index.rb', line 46

def read!(data)
  data.each_line do |line|
    line = line.strip.gsub(/\s*#.*$/, '')
    unless line.empty?
      name, url = line.split(/\s+/, 2)
      @references << reference(name, url)
    end
  end
end

#reference(name, path) ⇒ Object



83
84
85
# File 'lib/ronn/index.rb', line 83

def reference(name, path)
  Reference.new(self, name, path)
end

#relative_to_index(path) ⇒ Object



129
130
131
132
133
# File 'lib/ronn/index.rb', line 129

def relative_to_index(path)
  path = File.expand_path(path)
  index_dir = File.dirname(File.expand_path(self.path))
  path.sub(/^#{index_dir}\//, '')
end

#sizeObject



63
64
65
# File 'lib/ronn/index.rb', line 63

def size
  references.size
end

#to_aObject



121
122
123
# File 'lib/ronn/index.rb', line 121

def to_a
  references
end

#to_hObject



125
126
127
# File 'lib/ronn/index.rb', line 125

def to_h
  to_a.map(&:to_hash)
end

#to_textObject

Converting



117
118
119
# File 'lib/ronn/index.rb', line 117

def to_text
  map { |ref| [ref.name, ref.location].join(' ') }.join("\n")
end