Sha256: a68b8a07859243491d2ee064615b85b03016cd89b50f52c61303e87cd2b06817

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

module Riak
  module Serializers
    include Util::Translation
    extend self

    def [](content_type)
      serializers[content_type]
    end

    def []=(content_type, serializer)
      serializers[content_type] = serializer
    end

    def serialize(content_type, content)
      serializer_for(content_type).dump(content)
    end

    def deserialize(content_type, content)
      serializer_for(content_type).load(content)
    end

    private

    def serializer_for(content_type)
      serializers.fetch(content_type[/^[^;\s]+/]) do
        raise IOError.new(t('serializer_not_implemented', :content_type => content_type.inspect))
      end
    end

    def serializers
      @serializers ||= {}
    end

    module TextPlain
      extend self

      def dump(object)
        object.to_s
      end

      def load(string)
        string
      end
    end

    module ApplicationJSON
      extend self

      def dump(object)
        object.to_json(Riak.json_options)
      end

      def load(string)
        Riak::JSON.parse(string)
      end
    end

    Serializers['text/plain'] = TextPlain
    Serializers['application/json'] = ApplicationJSON
    Serializers['application/x-ruby-marshal'] = ::Marshal

    YAML_MIME_TYPES = %w[
      text/yaml
      text/x-yaml
      application/yaml
      application/x-yaml
    ]

    YAML_MIME_TYPES.each do |mime_type|
      Serializers[mime_type] = ::YAML
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
riak-client-2.2.0.pre1 lib/riak/serializers.rb
riak-client-2.1.0 lib/riak/serializers.rb
riak-client-2.0.0 lib/riak/serializers.rb
riak-client-2.0.0.rc2 lib/riak/serializers.rb
riak-client-2.0.0.rc1 lib/riak/serializers.rb