Sha256: 7edd7ffefb87e88235dabccd17e9d94f9e1948fdcc2b9f0765e18c7807a31c5a

Contents?: true

Size: 1.41 KB

Versions: 27

Compression:

Stored size: 1.41 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 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

27 entries across 17 versions & 2 rubygems

Version Path
riak-client-1.4.5 lib/riak/serializers.rb
riak-client-1.4.4.1 lib/riak/serializers.rb
riak-client-1.4.4 lib/riak/serializers.rb
riak-client-1.4.3 lib/riak/serializers.rb
riak-client-1.4.2 lib/riak/serializers.rb
riak-client-1.4.1 lib/riak/serializers.rb
riak-client-1.4.0 lib/riak/serializers.rb
riak-client-1.2.0 lib/riak/serializers.rb
riak-client-1.1.1 lib/riak/serializers.rb
riak-client-1.1.0 lib/riak/serializers.rb
better-riak-client-1.0.7 lib/riak/serializers.rb
better-riak-client-1.0.6 lib/riak/serializers.rb
riak-client-1.0.5 lib/riak/serializers.rb
better-riak-client-1.0.5 lib/riak/serializers.rb
riak-client-1.0.3 pkg/riak-client-1.0.2/pkg/riak-client-1.0.1/lib/riak/serializers.rb
riak-client-1.0.3 pkg/riak-client-1.0.2/pkg/riak-client-1.0.0/lib/riak/serializers.rb
riak-client-1.0.3 pkg/riak-client-1.0.2/lib/riak/serializers.rb
riak-client-1.0.3 pkg/riak-client-1.0.1/pkg/riak-client-1.0.0/lib/riak/serializers.rb
riak-client-1.0.3 pkg/riak-client-1.0.1/lib/riak/serializers.rb
riak-client-1.0.3 pkg/riak-client-1.0.0/lib/riak/serializers.rb