Class: ScopedSerializer::ArraySerializer

Inherits:
BaseSerializer show all
Defined in:
lib/scoped_serializer/array_serializer.rb

Direct Known Subclasses

CollectionSerializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSerializer

#as_json, #default_root_key, #meta_hash, #set_scope

Constructor Details

#initialize(array, scope_name = :default, options = {}) ⇒ ArraySerializer

Returns a new instance of ArraySerializer.



6
7
8
9
10
# File 'lib/scoped_serializer/array_serializer.rb', line 6

def initialize(array, scope_name=:default, options={})
  @array = array
  @scope_name = scope_name
  @options = options || {}
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



4
5
6
# File 'lib/scoped_serializer/array_serializer.rb', line 4

def array
  @array
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/scoped_serializer/array_serializer.rb', line 4

def options
  @options
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/scoped_serializer/array_serializer.rb', line 4

def scope
  @scope
end

#scope_nameObject (readonly)

Returns the value of attribute scope_name.



4
5
6
# File 'lib/scoped_serializer/array_serializer.rb', line 4

def scope_name
  @scope_name
end

Instance Method Details

#metaObject



18
19
20
21
22
23
24
25
26
# File 'lib/scoped_serializer/array_serializer.rb', line 18

def meta
  data = super

  if @array.respond_to?(:total_count)
    data = {}.merge(data).merge({ :total_count => @array.total_count })
  end

  data
end

#serializable_hash(options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/scoped_serializer/array_serializer.rb', line 12

def serializable_hash(options={})
  array.collect do |object|
    ScopedSerializer.for(object, @scope_name, @options.merge(:root => false)).as_json
  end
end