Sha256: 7f6b39506f364b0966359ad2a8faf1148bdbce3e6a220356d68b77670813e67b

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'active_model/default_serializer'
require 'active_model/serializable'
require 'active_model/serializer'

module ActiveModel
  class ArraySerializer
    include Serializable

    class << self
      attr_accessor :_root
      alias root  _root=
      alias root= _root=
    end

    def initialize(object, options={})
      @object          = object
      @root            = options[:root]
      @root            = self.class._root if @root.nil?
      @root            = options[:resource_name] if @root.nil?
      @meta_key        = options[:meta_key] || :meta
      @meta            = options[@meta_key]
      @each_serializer = options[:each_serializer]
      @options         = options.merge(root: nil)
    end
    attr_accessor :object, :root, :meta_key, :meta

    def serializable_array
      @object.map do |item|
        serializer = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
        serializer.new(item, @options).serializable_object
      end
    end
    alias_method :serializable_object, :serializable_array
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_serializers_rails_2.3-0.9.0.pre1 lib/active_model/array_serializer.rb