Class: GEPUB::Manifest

Inherits:
Object
  • Object
show all
Includes:
XMLUtil
Defined in:
lib/gepub/manifest.rb

Constant Summary

Constants included from XMLUtil

XMLUtil::DC_NS, XMLUtil::OPF_NS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XMLUtil

#attr_to_hash, #ns_prefix, #raw_prefix

Constructor Details

#initialize(opf_version = '3.0', id_pool = Package::IDPool.new) {|_self| ... } ⇒ Manifest

Returns a new instance of Manifest

Yields:

  • (_self)

Yield Parameters:


34
35
36
37
38
39
40
41
# File 'lib/gepub/manifest.rb', line 34

def initialize(opf_version = '3.0', id_pool = Package::IDPool.new)
  @id_pool = id_pool
  @attributes = {}
  @items = {}
  @items_by_href = {}
  @opf_version = opf_version
  yield self if block_given?
end

Instance Attribute Details

#opf_versionObject

Returns the value of attribute opf_version


6
7
8
# File 'lib/gepub/manifest.rb', line 6

def opf_version
  @opf_version
end

Class Method Details

.parse(manifest_xml, opf_version = '3.0', id_pool = Package::IDPool.new) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gepub/manifest.rb', line 7

def self.parse(manifest_xml, opf_version = '3.0', id_pool = Package::IDPool.new)
  Manifest.new(opf_version, id_pool) {
    |manifest|
    manifest.instance_eval {
      @xml = manifest_xml
      @namespaces = @xml.namespaces
      @attributes = attr_to_hash(@xml.attributes)
      @items = {}
      @items_by_href = {}
      @xml.xpath("//#{ns_prefix(OPF_NS)}:manifest/#{ns_prefix(OPF_NS)}:item", @namespaces).map {
        |item|
        i = Item.create(self, attr_to_hash(item.attributes))
        @items[i.id] = i
        @items_by_href[i.href] = i
      }
    }
  }
end

Instance Method Details

#add_item(id, href, media_type, attributes = {}) ⇒ Object


55
56
57
58
59
60
# File 'lib/gepub/manifest.rb', line 55

def add_item(id,href,media_type, attributes = {})
  id ||= @id_pool.generate_key(:prefix=>'item_'+ File.basename(href,'.*'), :without_count => true)
  @items[id] = item = Item.new(id,href,media_type,self, attributes)
  @items_by_href[href] = item
  item
end

#idObject


30
31
32
# File 'lib/gepub/manifest.rb', line 30

def id
  @attributes['id']
end

#id=(val) ⇒ Object


26
27
28
# File 'lib/gepub/manifest.rb', line 26

def id=(val)
  @attributes['id'] = val
end

#item_by_href(href) ⇒ Object


51
52
53
# File 'lib/gepub/manifest.rb', line 51

def item_by_href(href)
  @items_by_href[href]
end

#item_listObject


43
44
45
# File 'lib/gepub/manifest.rb', line 43

def item_list
  @items.dup
end

#itemsObject


47
48
49
# File 'lib/gepub/manifest.rb', line 47

def items
  @items.dup      
end

#register_item(item) ⇒ Object


71
72
73
74
# File 'lib/gepub/manifest.rb', line 71

def register_item(item)
  raise "id '#{item.id}' is already in use." if @id_pool[item.id]
  @id_pool[item.id] = true
end

#to_xml(builder) ⇒ Object


62
63
64
65
66
67
68
69
# File 'lib/gepub/manifest.rb', line 62

def to_xml(builder)
  builder.manifest(@attributes) {
    @items.each {
      |_itemid, item|
      item.to_xml(builder, @opf_version)
    }
  }
end

#unregister_item(item) ⇒ Object


76
77
78
79
80
# File 'lib/gepub/manifest.rb', line 76

def unregister_item(item)
  @items.delete(item.id)
  @items_by_href.delete(item.href)
  @id_pool[item.id] = nil
end