class ReVIEW::Book::Index

Constants

Item

Public Class Methods

item_class() click to toggle source
# File lib/review/book/index.rb, line 36
def Index.item_class
  self::Item
end
new(items) click to toggle source
# File lib/review/book/index.rb, line 46
def initialize(items)
  @items = items
  @index = {}
  items.each do |i|
    warn "warning: duplicate ID: #{i.id} (#{i})" unless @index[i.id].nil?
    @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 Index.parse(src, *args)
  items = []
  seq = 1
  src.grep(%r<^//#{item_type()}>) do |line|
    if id = line.slice(/\[(.*?)\]/, 1)
      items.push item_class().new(id, seq)
      seq += 1
      if id == ""
        warn "warning: no ID of #{item_type()} in #{line}"
      end
    end
  end
  new(items, *args)
end

Public Instance Methods

[](id) click to toggle source
# File lib/review/book/index.rb, line 56
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|
    if i.id.split(/\|/).include?(id)
      return i
    end
  end
  raise KeyError, "not found key '#{id}' for #{self.class}"
end
each(&block) click to toggle source
# File lib/review/book/index.rb, line 77
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 42
def item_type
  self.class.item_type
end
key?(id) click to toggle source
# File lib/review/book/index.rb, line 81
def key?(id)
  return @index.key?(id)
end
Also aliased as: has_key?
number(id) click to toggle source
# File lib/review/book/index.rb, line 73
def number(id)
  self[id].number.to_s
end