Sha256: 6b8a306a716c40449a0e44101d27481b7007c48f6616694aa5c8c60076682ba0

Contents?: true

Size: 1.39 KB

Versions: 2

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) do
        raise NotImplementedError.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

2 entries across 2 versions & 2 rubygems

Version Path
seomoz-riak-client-1.0.0.pre lib/riak/serializers.rb
riak-client-1.0.0.beta lib/riak/serializers.rb