Sha256: 9f399760ccf538a0a287ef5b9dbf1d643c13eacd1fd77dffb10b69ff1e4d6eaf

Contents?: true

Size: 1015 Bytes

Versions: 5

Compression:

Stored size: 1015 Bytes

Contents

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

      attr_reader :object, :root, :meta, :meta_key

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cheap_ams-0.10.11 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.10 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.8 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.7 lib/active_model/serializer/array_serializer.rb
cheap_ams-0.10.6 lib/active_model/serializer/array_serializer.rb