Class: GEPUB::Manifest

Inherits:
Object
  • Object
show all
Includes:
InspectMixin, 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 InspectMixin

#inspect

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:



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

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.



8
9
10
# File 'lib/gepub/manifest.rb', line 8

def opf_version
  @opf_version
end

Class Method Details

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



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

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



57
58
59
60
61
62
# File 'lib/gepub/manifest.rb', line 57

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



32
33
34
# File 'lib/gepub/manifest.rb', line 32

def id
  @attributes['id']
end

#id=(val) ⇒ Object



28
29
30
# File 'lib/gepub/manifest.rb', line 28

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

#item_by_href(href) ⇒ Object



53
54
55
# File 'lib/gepub/manifest.rb', line 53

def item_by_href(href)
  @items_by_href[href]
end

#item_listObject



45
46
47
# File 'lib/gepub/manifest.rb', line 45

def item_list
  @items.dup
end

#itemsObject



49
50
51
# File 'lib/gepub/manifest.rb', line 49

def items
  @items.dup      
end

#register_item(item) ⇒ Object



73
74
75
76
# File 'lib/gepub/manifest.rb', line 73

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



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

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

#unregister_item(item) ⇒ Object



78
79
80
81
82
# File 'lib/gepub/manifest.rb', line 78

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