Class: GEPUB::Bindings

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

Defined Under Namespace

Classes: MediaType

Constant Summary

Constants included from XMLUtil

XMLUtil::DC_NS, XMLUtil::OPF_NS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XMLUtil

#attr_to_hash, #ns_prefix, #raw_prefix

Constructor Details

#initialize {|_self| ... } ⇒ Bindings

Returns a new instance of Bindings

Yields:

  • (_self)

Yield Parameters:


17
18
19
20
21
# File 'lib/gepub/bindings.rb', line 17

def initialize
  @media_types = []
  @handler_by_media_type = {}
  yield self if block_given?
end

Class Method Details

.parse(bindings_xml) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gepub/bindings.rb', line 23

def self.parse(bindings_xml)
  Bindings.new {
    |bindings|
    bindings.instance_eval {
      if !bindings_xml.nil?
        @xml = bindings_xml
        @namespaces = @xml.namespaces
        @attributes = attr_to_hash(@xml.attributes)
        @xml.xpath("//#{ns_prefix(OPF_NS)}:bindings/#{ns_prefix(OPF_NS)}:mediaType", @namespaces).map {
          |mediaType|
          @media_types <<  MediaType.new(mediaType['handler'], mediaType['media-type'])
          @handler_by_media_type[mediaType['media-type']] = mediaType['handler']
        }
      end
    }
  }
end

Instance Method Details

#add(id, media_type) ⇒ Object


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

def add(id, media_type)
  @media_types << MediaType.new(id, media_type)
  @handler_by_media_type[media_type] =  id
end

#handler_by_media_typeObject


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

def handler_by_media_type
  return @handler_by_media_type.dup
end

#media_typesObject


41
42
43
# File 'lib/gepub/bindings.rb', line 41

def media_types
  return @media_types.dup
end

#to_xml(builder) ⇒ Object


54
55
56
57
58
59
60
61
62
63
# File 'lib/gepub/bindings.rb', line 54

def to_xml(builder)
  if (media_types.size > 0)
    builder.bindings {
      @media_types.each {
        |mediaType|
        mediaType.to_xml(builder)
      }
    }
  end
end