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
-
#context ⇒ Object
Returns the value of attribute context.
-
#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(options = {}) ⇒ Object (also: #serializable_array)
- #serializer_for(item) ⇒ Object
Methods included from Serializable
#as_json, included, #namespace, #serializable_data, #serializable_object_with_notification
Constructor Details
#initialize(object, options = {}) ⇒ ArraySerializer
Returns a new instance of ArraySerializer.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/active_model/array_serializer.rb', line 14 def initialize(object, ={}) @object = object @scope = [:scope] @root = .fetch(:root, self.class._root) @polymorphic = .fetch(:polymorphic, false) @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 @context = [:context] @namespace = [:namespace] @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
#context ⇒ Object
Returns the value of attribute context.
29 30 31 |
# File 'lib/active_model/array_serializer.rb', line 29 def context @context end |
#key_format ⇒ Object
Returns the value of attribute key_format.
29 30 31 |
# File 'lib/active_model/array_serializer.rb', line 29 def key_format @key_format end |
#meta ⇒ Object
Returns the value of attribute meta.
29 30 31 |
# File 'lib/active_model/array_serializer.rb', line 29 def @meta end |
#meta_key ⇒ Object
Returns the value of attribute meta_key.
29 30 31 |
# File 'lib/active_model/array_serializer.rb', line 29 def @meta_key end |
#object ⇒ Object
Returns the value of attribute object.
29 30 31 |
# File 'lib/active_model/array_serializer.rb', line 29 def object @object end |
#root ⇒ Object
Returns the value of attribute root.
29 30 31 |
# File 'lib/active_model/array_serializer.rb', line 29 def root @root end |
#scope ⇒ Object
Returns the value of attribute scope.
29 30 31 |
# File 'lib/active_model/array_serializer.rb', line 29 def scope @scope end |
Instance Method Details
#embedded_in_root_associations ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/active_model/array_serializer.rb', line 49 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) case hash[type] when Hash hash[type].deep_merge!(objects){ |key, old, new| (Array(old) + Array(new)).uniq } else hash[type].concat(objects).uniq! end else hash[type] = objects end end end end |
#json_key ⇒ Object
31 32 33 34 35 |
# File 'lib/active_model/array_serializer.rb', line 31 def json_key key = root.nil? ? @resource_name : root key_format == :lower_camel && key.present? ? key.camelize(:lower) : key end |
#serializable_object(options = {}) ⇒ Object Also known as: serializable_array
42 43 44 45 46 |
# File 'lib/active_model/array_serializer.rb', line 42 def serializable_object(={}) @object.map do |item| serializer_for(item).serializable_object_with_notification() end end |
#serializer_for(item) ⇒ Object
37 38 39 40 |
# File 'lib/active_model/array_serializer.rb', line 37 def serializer_for(item) serializer_class = @each_serializer || Serializer.serializer_for(item, namespace: @namespace) || DefaultSerializer serializer_class.new(item, scope: scope, key_format: key_format, context: @context, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace) end |