Class: ActiveModel::ArraySerializer
- Inherits:
-
Object
- Object
- ActiveModel::ArraySerializer
- Includes:
- Serializable
- Defined in:
- lib/active_model/array_serializer.rb
Class Attribute Summary collapse
-
._root ⇒ Object
Returns the value of attribute _root.
Instance Attribute Summary collapse
-
#key_format ⇒ Object
Returns the value of attribute key_format.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#meta_key ⇒ Object
Returns the value of attribute meta_key.
-
#object ⇒ Object
Returns the value of attribute object.
-
#root ⇒ Object
Returns the value of attribute root.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #embedded_in_root_associations ⇒ Object
-
#initialize(object, options = {}) ⇒ ArraySerializer
constructor
A new instance of ArraySerializer.
- #json_key ⇒ Object
- #serializable_object ⇒ Object (also: #serializable_array)
- #serializer_for(item) ⇒ Object
Methods included from Serializable
Constructor Details
#initialize(object, options = {}) ⇒ ArraySerializer
Returns a new instance of ArraySerializer.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_model/array_serializer.rb', line 14 def initialize(object, ={}) @object = object @scope = [:scope] @root = .fetch(:root, self.class._root) @meta_key = [:meta_key] || :meta @meta = [@meta_key] @each_serializer = [:each_serializer] @resource_name = [:resource_name] @only = [:only] ? Array([:only]) : nil @except = [:except] ? Array([:except]) : nil @key_format = [:key_format] || [:each_serializer].try(:key_format) end |
Class Attribute Details
._root ⇒ Object
Returns the value of attribute _root.
9 10 11 |
# File 'lib/active_model/array_serializer.rb', line 9 def _root @_root end |
Instance Attribute Details
#key_format ⇒ Object
Returns the value of attribute key_format.
26 27 28 |
# File 'lib/active_model/array_serializer.rb', line 26 def key_format @key_format end |
#meta ⇒ Object
Returns the value of attribute meta.
26 27 28 |
# File 'lib/active_model/array_serializer.rb', line 26 def @meta end |
#meta_key ⇒ Object
Returns the value of attribute meta_key.
26 27 28 |
# File 'lib/active_model/array_serializer.rb', line 26 def @meta_key end |
#object ⇒ Object
Returns the value of attribute object.
26 27 28 |
# File 'lib/active_model/array_serializer.rb', line 26 def object @object end |
#root ⇒ Object
Returns the value of attribute root.
26 27 28 |
# File 'lib/active_model/array_serializer.rb', line 26 def root @root end |
#scope ⇒ Object
Returns the value of attribute scope.
26 27 28 |
# File 'lib/active_model/array_serializer.rb', line 26 def scope @scope end |
Instance Method Details
#embedded_in_root_associations ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_model/array_serializer.rb', line 46 def @object.each_with_object({}) do |item, hash| serializer_for(item)..each_pair do |type, objects| next if !objects || objects.flatten.empty? if hash.has_key?(type) hash[type].concat(objects).uniq! else hash[type] = objects end end end end |
#json_key ⇒ Object
28 29 30 31 32 |
# File 'lib/active_model/array_serializer.rb', line 28 def json_key key = root.nil? ? @resource_name : root key_format == :lower_camel && key.present? ? key.camelize(:lower) : key end |
#serializable_object ⇒ Object Also known as: serializable_array
39 40 41 42 43 |
# File 'lib/active_model/array_serializer.rb', line 39 def serializable_object @object.map do |item| serializer_for(item).serializable_object end end |
#serializer_for(item) ⇒ Object
34 35 36 37 |
# File 'lib/active_model/array_serializer.rb', line 34 def serializer_for(item) serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except) end |