mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
33 lines
782 B
Ruby
33 lines
782 B
Ruby
module ActiveModel
|
|
class Serializer
|
|
class ArraySerializer
|
|
include Enumerable
|
|
delegate :each, to: :@objects
|
|
|
|
attr_reader :meta, :meta_key
|
|
|
|
def initialize(objects, options = {})
|
|
options.merge!(root: nil)
|
|
|
|
@objects = objects.map do |object|
|
|
serializer_class = options.fetch(
|
|
:serializer,
|
|
ActiveModel::Serializer.serializer_for(object)
|
|
)
|
|
serializer_class.new(object, options.except(:serializer))
|
|
end
|
|
@meta = options[:meta]
|
|
@meta_key = options[:meta_key]
|
|
end
|
|
|
|
def json_key
|
|
@objects.first.json_key.pluralize if @objects.first
|
|
end
|
|
|
|
def root=(root)
|
|
@objects.first.root = root if @objects.first
|
|
end
|
|
end
|
|
end
|
|
end
|