class ReVIEW::Book::Index

Public Class Methods

new() click to toggle source
# File lib/review/book/index.rb, line 26
def initialize
  @index = {}
  @logger = ReVIEW.logger
  @image_finder = nil
end

Public Instance Methods

[](id) click to toggle source
# File lib/review/book/index.rb, line 46
def [](id)
  @index.fetch(id)
rescue
  index_keys = @index.keys.map { |i| i.split('|').last }.flatten # unfold all ids
  if index_keys.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

  @index.each_value 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) click to toggle source
# File lib/review/book/index.rb, line 36
def add_item(item)
  if @index[item.id] && self.class != ReVIEW::Book::IconIndex
    @logger.warn "warning: duplicate ID: #{item.id} (#{item.inspect})"
  end
  @index[item.id] = item
  if item.class != ReVIEW::Book::Chapter
    item.index = self
  end
end
each(&block) click to toggle source
# File lib/review/book/index.rb, line 67
def each(&block)
  @index.values.each(&block)
end
has_key?(id)
Alias for: key?
item_type() click to toggle source
# File lib/review/book/index.rb, line 22
def item_type
  self.class.item_type
end
key?(id) click to toggle source
# File lib/review/book/index.rb, line 71
def key?(id)
  @index.key?(id)
end
Also aliased as: has_key?
number(id) click to toggle source
# File lib/review/book/index.rb, line 63
def number(id)
  self[id].number.to_s
end
size() click to toggle source
# File lib/review/book/index.rb, line 32
def size
  @index.size
end