Class: ActiveModel::Serializer::ArraySerializer
- Inherits:
-
Object
- Object
- ActiveModel::Serializer::ArraySerializer
- Includes:
- Enumerable
- Defined in:
- lib/active_model/serializer/array_serializer.rb
Constant Summary collapse
- NoSerializerError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#meta_key ⇒ Object
readonly
Returns the value of attribute meta_key.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#initialize(resources, options = {}) ⇒ ArraySerializer
constructor
A new instance of ArraySerializer.
- #json_key ⇒ Object
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, = {}) @root = [:root] @object = resources @serializers = resources.map do |resource| serializer_class = .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, .except(:serializer)) end end @meta = [:meta] @meta_key = [:meta_key] end |
Instance Attribute Details
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
8 9 10 |
# File 'lib/active_model/serializer/array_serializer.rb', line 8 def @meta end |
#meta_key ⇒ Object (readonly)
Returns the value of attribute meta_key.
8 9 10 |
# File 'lib/active_model/serializer/array_serializer.rb', line 8 def @meta_key end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
8 9 10 |
# File 'lib/active_model/serializer/array_serializer.rb', line 8 def object @object end |
#root ⇒ Object (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_key ⇒ Object
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 |