Sha256: 69add2500368882c4410168784adc49e629487d659938236a946bdee1dd5cde5

Contents?: true

Size: 919 Bytes

Versions: 1

Compression:

Stored size: 919 Bytes

Contents

module Yaks
  class Serializer
    extend Forwardable
    include Util

    attr_reader :options
    def_delegators :resource, :links, :attributes, :subresources

    protected :links, :attributes, :subresources, :options

    def initialize(options = {})
      @options  = YAKS_DEFAULT_OPTIONS.merge(options)
    end

    def call(resource)
      serialize_resource(resource)
    end
    alias serialize call

    class << self
      def register(klass, name, mime_type)
        @serializers ||= {}
        @serializers[name] = klass

        @mime_types  ||= {}
        @mime_types[mime_type] = [name, klass]
      end

      def by_name(name)
        @serializers.fetch(name)
      end

      def by_mime_type(mime_type)
        @mime_types.fetch(mime_type)[1]
      end

      def mime_types
        @mime_types.inject({}) {|memo, (mime_type, (name, _))| memo[name] = mime_type ; memo }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-0.4.2 lib/yaks/serializer.rb