Class: ReVIEW::Book::Index

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/review/book/index.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.



45
46
47
48
49
50
51
52
# File 'lib/review/book/index.rb', line 45

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
end

Class Method Details

.item_classObject



35
36
37
# File 'lib/review/book/index.rb', line 35

def Index.item_class
  self::Item
end

.parse(src, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/review/book/index.rb', line 18

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

Instance Method Details

#[](id) ⇒ Object



54
55
56
57
58
# File 'lib/review/book/index.rb', line 54

def [](id)
  @index.fetch(id)
rescue
  raise KeyError
end

#each(&block) ⇒ Object



64
65
66
# File 'lib/review/book/index.rb', line 64

def each(&block)
  @items.each(&block)
end

#has_key?(id) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/review/book/index.rb', line 68

def has_key?(id)
  return @index.has_key?(id)
end

#item_typeObject



41
42
43
# File 'lib/review/book/index.rb', line 41

def item_type
  self.class.item_type
end

#number(id) ⇒ Object



60
61
62
# File 'lib/review/book/index.rb', line 60

def number(id)
  @index.fetch(id).number.to_s
end