Class: ReVIEW::Book::Index
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/review/book/index.rb,
lib/review/book/index/item.rb
Defined Under Namespace
Classes: Item
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Index
Returns a new instance of Index.
41
42
43
44
45
|
# File 'lib/review/book/index.rb', line 41
def initialize
@index = {}
@logger = ReVIEW.logger
@image_finder = nil
end
|
Class Method Details
.parse(src, *args) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/review/book/index.rb', line 20
def self.parse(src, *args)
index = self.new(*args)
seq = 1
src.grep(%r{\A//#{item_type}}) do |line|
if id = line.slice(/\[(.*?)\]/, 1)
index.add_item(ReVIEW::Book::Index::Item.new(id, seq))
seq += 1
if id.empty?
ReVIEW.logger.warn "warning: no ID of #{item_type} in #{line}"
end
end
end
index
end
|
Instance Method Details
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/review/book/index.rb', line 57
def [](id)
@index.fetch(id)
rescue
if @index.keys.map { |i| i.split('|').last }.flatten. each_with_object(Hash.new(0)) { |i, h| h[i] += 1 }. select { |k, v| k == id && v > 1 }.present? raise KeyError, "key '#{id}' is ambiguous for #{self.class}"
end
@index.values.each do |item|
if item.id.split('|').include?(id)
return item
end
end
raise KeyError, "not found key '#{id}' for #{self.class}"
end
|
#add_item(item) ⇒ Object
47
48
49
50
51
52
53
54
55
|
# File 'lib/review/book/index.rb', line 47
def add_item(item)
if @index[item.id]
@logger.warn "warning: duplicate ID: #{item.id} (#{item})"
end
@index[item.id] = item
if item.class != ReVIEW::Book::Chapter
item.index = self
end
end
|
#each(&block) ⇒ Object
78
79
80
|
# File 'lib/review/book/index.rb', line 78
def each(&block)
@index.values.each(&block)
end
|
#item_type ⇒ Object
37
38
39
|
# File 'lib/review/book/index.rb', line 37
def item_type
self.class.item_type
end
|
#key?(id) ⇒ Boolean
Also known as:
has_key?
82
83
84
|
# File 'lib/review/book/index.rb', line 82
def key?(id)
@index.key?(id)
end
|
#number(id) ⇒ Object
74
75
76
|
# File 'lib/review/book/index.rb', line 74
def number(id)
self[id].number.to_s
end
|