Class: ActiveModel::Serializer::ArraySerializer

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_model/serializer/array_serializer.rb

Constant Summary collapse

NoSerializerError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources, options = {}) ⇒ ArraySerializer

Returns a new instance of ArraySerializer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_model/serializer/array_serializer.rb', line 10

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

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



8
9
10
# File 'lib/active_model/serializer/array_serializer.rb', line 8

def meta
  @meta
end

#meta_keyObject (readonly)

Returns the value of attribute meta_key.



8
9
10
# File 'lib/active_model/serializer/array_serializer.rb', line 8

def meta_key
  @meta_key
end

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/active_model/serializer/array_serializer.rb', line 8

def object
  @object
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/active_model/serializer/array_serializer.rb', line 8

def root
  @root
end

Instance Method Details

#json_keyObject



28
29
30
31
# File 'lib/active_model/serializer/array_serializer.rb', line 28

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