class ReVIEW::Book::Index

Constants

Item

Public Class Methods

item_class() click to toggle source
# File ../../../../../lib/review/book/index.rb, line 34
def self.item_class
  self::Item
end
new(items) click to toggle source
# File ../../../../../lib/review/book/index.rb, line 44
def initialize(items)
  @items = items
  @index = {}
  @logger = ReVIEW.logger
  items.each do |i|
    @logger.warn "warning: duplicate ID: #{i.id} (#{i})" if @index[i.id]
    @index[i.id] = i
  end
  @image_finder = nil
end
parse(src, *args) click to toggle source
# File ../../../../../lib/review/book/index.rb, line 19
def self.parse(src, *args)
  items = []
  seq = 1
  src.grep(%r{\A//#{item_type}}) do |line|
    if id = line.slice(/\[(.*?)\]/, 1)
      items.push item_class.new(id, seq)
      seq += 1
      ReVIEW.logger.warn "warning: no ID of #{item_type} in #{line}" if id.empty?
    end
  end
  new(items, *args)
end

Public Instance Methods

[](id) click to toggle source
# File ../../../../../lib/review/book/index.rb, line 55
def [](id)
  @index.fetch(id)
rescue
  if @index.keys.map { |i| i.split('|').last }.flatten. # unfold all ids
     each_with_object(Hash.new(0)) { |i, h| h[i] += 1 }. # number of occurrences
     select { |k, v| k == id && v > 1 }.present? # detect duplicated
    raise KeyError, "key '#{id}' is ambiguous for #{self.class}"
  end

  @items.each do |i|
    return i if i.id.split('|').include?(id)
  end
  raise KeyError, "not found key '#{id}' for #{self.class}"
end
each(&block) click to toggle source
# File ../../../../../lib/review/book/index.rb, line 74
def each(&block)
  @items.each(&block)
end
has_key?(id)
Alias for: key?
item_type() click to toggle source
# File ../../../../../lib/review/book/index.rb, line 40
def item_type
  self.class.item_type
end
key?(id) click to toggle source
# File ../../../../../lib/review/book/index.rb, line 78
def key?(id)
  @index.key?(id)
end
Also aliased as: has_key?, has_key?
number(id) click to toggle source
# File ../../../../../lib/review/book/index.rb, line 70
def number(id)
  self[id].number.to_s
end