Sha256: 62fc88cac4587822369606eb57a600c7e1694ee8bbdf5e49716e97a586a0205d

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module ActiveModel
  class Serializer
    module Adapter
      class Base
        # Automatically register adapters when subclassing
        def self.inherited(subclass)
          ActiveModel::Serializer::Adapter.register(subclass)
        end

        attr_reader :serializer, :instance_options

        def initialize(serializer, options = {})
          @serializer = serializer
          @instance_options = options
        end

        def serializable_hash(_options = nil)
          fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
        end

        def as_json(options = nil)
          hash = serializable_hash(options)
          include_meta(hash)
          hash
        end

        def fragment_cache(*_args)
          fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
        end

        def cache_check(serializer)
          CachedSerializer.new(serializer).cache_check(self) do
            yield
          end
        end

        private

        def meta
          instance_options.fetch(:meta, nil)
        end

        def meta_key
          instance_options.fetch(:meta_key, 'meta'.freeze)
        end

        def root
          serializer.json_key.to_sym if serializer.json_key
        end

        def include_meta(json)
          json[meta_key] = meta if meta
          json
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_serializers-0.10.0.rc4 lib/active_model/serializer/adapter/base.rb