Sha256: 0aa395f72e9365f169d2fb93998b1d6336525952fd54a282b3d6ddae06f600ce

Contents?: true

Size: 1.6 KB

Versions: 34

Compression:

Stored size: 1.6 KB

Contents

require 'rubygems'
require 'nokogiri'
module GEPUB
  class Bindings
    include XMLUtil
    class MediaType
      attr_accessor :handler, :media_type
      def initialize(handler, media_type)
        @handler = handler
        @media_type = media_type
      end
      def to_xml(builder)
        builder.mediaType({'handler' => @handler, 'media-type' => @media_type})
      end
    end

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

    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

    def media_types
      return @media_types.dup
    end

    def handler_by_media_type
      return @handler_by_media_type.dup
    end

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

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

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
gepub-1.0.14 lib/gepub/bindings.rb
gepub-1.0.13 lib/gepub/bindings.rb
gepub-1.0.12 lib/gepub/bindings.rb
gepub-1.0.11 lib/gepub/bindings.rb
gepub-1.0.10 lib/gepub/bindings.rb
gepub-1.0.9 lib/gepub/bindings.rb
gepub-1.0.8 lib/gepub/bindings.rb
gepub-1.0.7 lib/gepub/bindings.rb
gepub-1.0.6 lib/gepub/bindings.rb
gepub-1.0.5 lib/gepub/bindings.rb
gepub-1.0.4 lib/gepub/bindings.rb
gepub-1.0.3 lib/gepub/bindings.rb
gepub-1.0.2 lib/gepub/bindings.rb
gepub-1.0.1 lib/gepub/bindings.rb
gepub-1.0.0 lib/gepub/bindings.rb
gepub-1.0.0rc1 lib/gepub/bindings.rb
gepub-1.0.0beta1 lib/gepub/bindings.rb
gepub-0.7.1 lib/gepub/bindings.rb
gepub-0.7.0 lib/gepub/bindings.rb
gepub-0.7.0beta4 lib/gepub/bindings.rb