Class: ScopedSerializer::ArraySerializer
- Inherits:
-
BaseSerializer
- Object
- BaseSerializer
- ScopedSerializer::ArraySerializer
- Defined in:
- lib/scoped_serializer/array_serializer.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(array, options = {}) ⇒ ArraySerializer
constructor
A new instance of ArraySerializer.
- #meta ⇒ Object
- #serializable_hash(options = {}) ⇒ Object
-
#to_csv(options = {}) ⇒ Object
Returns attributes as a CSV string.
Methods inherited from BaseSerializer
#as_json, #data_hash, #default_root_key, #meta_hash, #set_scope, #to_xls
Constructor Details
#initialize(array, 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, ={}) @array = array @options = || {} @options[:each_serializer] ||= @options.delete(:serializer) end |
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
4 5 6 |
# File 'lib/scoped_serializer/array_serializer.rb', line 4 def array @array end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/scoped_serializer/array_serializer.rb', line 4 def @options end |
Instance Method Details
#meta ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/scoped_serializer/array_serializer.rb', line 16 def 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 |
# File 'lib/scoped_serializer/array_serializer.rb', line 12 def serializable_hash(={}) serializable_objects.collect(&:as_json) end |
#to_csv(options = {}) ⇒ Object
Returns attributes as a CSV string.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/scoped_serializer/array_serializer.rb', line 29 def to_csv(={}) columns = .delete(:columns) CSV.generate() do |csv| csv << columns if columns.present? serializable_objects.each do |object| csv << object.attributes_hash.values end end end |