Sha256: 14f6e1fe72eb35e505a77df84618681a8301b1b97444f619213ce7b8f5dcb246

Contents?: true

Size: 995 Bytes

Versions: 6

Compression:

Stored size: 995 Bytes

Contents

module ActiveModel
  class Serializer
    class ArraySerializer
      NoSerializerError = Class.new(StandardError)
      include Enumerable
      delegate :each, to: :@objects

      attr_reader :root, :meta, :meta_key

      def initialize(objects, options = {})
        @root = options[:root]
        @resource = objects
        @objects  = objects.map do |object|
          serializer_class = options.fetch(
            :serializer,
            ActiveModel::Serializer.serializer_for(object)
          )

          if serializer_class.nil?
            fail NoSerializerError, "No serializer found for object: #{object.inspect}"
          else
            serializer_class.new(object, options.except(:serializer))
          end
        end
        @meta     = options[:meta]
        @meta_key = options[:meta_key]
      end

      def json_key
        key = root || @objects.first.try(:json_key) || @resource.try(:name).try(:underscore)
        key.try(:pluralize)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cheap_ams-0.10.5 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.4 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.3 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.2 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.1 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.0.rc2 lib/active_model/serializer/array_serializer.rb