Class: GEPUB::Spine

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

Defined Under Namespace

Classes: Itemref

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| ... } ⇒ Spine

Returns a new instance of Spine.

Yields:

  • (_self)

Yield Parameters:

  • _self (GEPUB::Spine)

    the object that the method was called on



102
103
104
105
106
107
108
109
# File 'lib/gepub/spine.rb', line 102

def initialize(opf_version = '3.0', id_pool  = Package::IDPool.new)
  @id_pool = id_pool
  @attributes = {}
  @item_refs = []
  @itemref_by_id = {}
  @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/spine.rb', line 6

def opf_version
  @opf_version
end

Class Method Details

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gepub/spine.rb', line 85

def self.parse(spine_xml, opf_version = '3.0', id_pool  = Package::IDPool.new)
  Spine.new(opf_version, id_pool) {
    |spine|
    spine.instance_eval {
      @xml = spine_xml
      @namespaces = @xml.namespaces
      @attributes = attr_to_hash(@xml.attributes)
      @item_refs = []
      @xml.xpath("//#{ns_prefix(OPF_NS)}:spine/#{ns_prefix(OPF_NS)}:itemref", @namespaces).map {
        |itemref|
        i = Itemref.create(self, attr_to_hash(itemref.attributes))
        @item_refs << i
      }
    }
  }
end

Instance Method Details

#<<(item) ⇒ Object



132
133
134
# File 'lib/gepub/spine.rb', line 132

def <<(item)
  push item
end

#itemref_by_idObject



122
123
124
# File 'lib/gepub/spine.rb', line 122

def itemref_by_id
  @itemref_by_id.dup
end

#itemref_listObject



118
119
120
# File 'lib/gepub/spine.rb', line 118

def itemref_list
  @item_refs.dup
end

#push(item) ⇒ Object



126
127
128
129
130
# File 'lib/gepub/spine.rb', line 126

def push(item)
  @item_refs << i = Itemref.new(item.id, self)
  @itemref_by_id[item.id] = i
  i
end

#register_itemref(itemref) ⇒ Object



149
150
151
152
# File 'lib/gepub/spine.rb', line 149

def register_itemref(itemref)
  raise "id '#{itemref.id}' is already in use." if @id_pool[itemref.id]
  @id_pool[itemref.id] = true unless itemref.id.nil?
end

#remove_with_idlist(ids) ⇒ Object



159
160
161
162
163
164
# File 'lib/gepub/spine.rb', line 159

def remove_with_idlist(ids)
  @item_refs = @item_refs.select {
    |ref|
    !ids.member? ref.idref
  }
end

#rendition_specified?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/gepub/spine.rb', line 136

def rendition_specified?
  @item_refs.select { |itemref| itemref.rendition_specified? }.size > 0
end

#to_xml(builder) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/gepub/spine.rb', line 140

def to_xml(builder)
  builder.spine(@attributes) {
    @item_refs.each {
      |ref|
      ref.to_xml(builder, @opf_version)
    }
  }
end

#unregister_itemref(itemref) ⇒ Object



154
155
156
157
# File 'lib/gepub/spine.rb', line 154

def unregister_itemref(itemref)
  @item_refs.delete itemref
  @id_pool[itemref.id] = nil
end