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(items) ⇒ Index
Returns a new instance of Index.
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/review/book/index.rb', line 41
def initialize(items)
@items = items
@index = {}
@logger = ReVIEW.logger
items.each do |item|
if @index[item.id]
@logger.warn "warning: duplicate ID: #{item.id} (#{item})"
end
@index[item.id] = item
end
@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)
items = []
seq = 1
src.grep(%r{\A//#{item_type}}) do |line|
if id = line.slice(/\[(.*?)\]/, 1)
items.push(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
new(items, *args)
end
|
Instance Method Details
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/review/book/index.rb', line 54
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
@items.each do |item|
if item.id.split('|').include?(id)
return item
end
end
raise KeyError, "not found key '#{id}' for #{self.class}"
end
|
#each(&block) ⇒ Object
75
76
77
|
# File 'lib/review/book/index.rb', line 75
def each(&block)
@items.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?
79
80
81
|
# File 'lib/review/book/index.rb', line 79
def key?(id)
@index.key?(id)
end
|
#number(id) ⇒ Object
71
72
73
|
# File 'lib/review/book/index.rb', line 71
def number(id)
self[id].number.to_s
end
|