Class: Delineate::Serializers::CsvSerializer
- Inherits:
-
MapSerializer
- Object
- MapSerializer
- Delineate::Serializers::CsvSerializer
- Defined in:
- lib/delineate/serializers/csv_serializer.rb
Overview
AttributeMap serializer that handles CSV as the external data format.
Instance Method Summary collapse
-
#serializable_header(prefix = '') ⇒ Object
Returns the header row as an array of strings, one for each mapped attribute, including nested assoications.
-
#serializable_record(prefix = [], top_level = true) ⇒ Object
Returns the record’s mapped attributes in the serializer’s “internal” format.
-
#serialize(options = {}) ⇒ Object
Returns the record’s mapped attributes as a CSV string.
-
#serialize_header(options = {}) ⇒ Object
Returns the header row as a CSV string.
-
#serialize_in(csv_string, options = {}) ⇒ Object
Not implemented yet.
Methods inherited from MapSerializer
Constructor Details
This class inherits a constructor from Delineate::MapSerializer
Instance Method Details
#serializable_header(prefix = '') ⇒ Object
Returns the header row as an array of strings, one for each mapped attribute, including nested assoications. The items appear in the array in the same order as their corresponding attribute values.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/delineate/serializers/csv_serializer.rb', line 76 def serializable_header(prefix = '') returning(serializable_header = serializable_attribute_names) do serializable_header.map! {|h| headerize(prefix + h.to_s)} add_includes(:one_to_one) do |association, record, opts| assoc_map = association_attribute_map(association) assoc_prefix = prefix + association.to_s + '_' serializable_header.concat self.class.new(record, assoc_map, opts).serializable_header(assoc_prefix) end add_includes(:one_to_many) do |association, records, opts| assoc_map = association_attribute_map(association) assoc_prefix = prefix + association.to_s.singularize + '_' serializable_header.concat self.class.new(records.first, assoc_map, opts).serializable_header(assoc_prefix) end end end |
#serializable_record(prefix = [], top_level = true) ⇒ Object
Returns the record’s mapped attributes in the serializer’s “internal” format. For this class the representation is an array of one or more rows, one row for each item in teh record’s has_many collections. Each row is an array of values ordered as follows:
1. All the record's mapped attributes in map order.
2. All one-to-one mapped association attributes in map order.
3. All one-to-many mapped association attributes in map order
Do not specify any method parameters when calling serializable_record
.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/delineate/serializers/csv_serializer.rb', line 49 def serializable_record(prefix = [], top_level = true) new_rows = [] prefix += serializable_attribute_names.map do |name| @attribute_map.attribute_value(@record, name) end add_includes(:one_to_one) do |association, record, opts, nil_record| assoc_map = association_attribute_map(association) prefix, new_rows = self.class.new(record, assoc_map, opts).serializable_record(prefix, false) end add_includes(:one_to_many) do |association, records, opts, nil_record| assoc_map = association_attribute_map(association) records.each do |r| p, next_rows = self.class.new(r, assoc_map, opts).serializable_record(prefix, false) new_rows << (next_rows.empty? ? p : next_rows) unless nil_record end end top_level ? (new_rows << prefix if new_rows.empty?; new_rows) : [prefix, new_rows] end |
#serialize(options = {}) ⇒ Object
Returns the record’s mapped attributes as a CSV string. If you specify a truthy value for the :include_header option, the CSV header is output as the first line.
See the description in serializable_record
for the order of the attributes.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/delineate/serializers/csv_serializer.rb', line 15 def serialize( = {}) opts = [:include_header] ? {:write_headers => true, :headers => serializable_header, :encoding => "UTF-8"} : {:encoding => "UTF-8"} opts = ().merge(opts) opts.delete(:include_header) CSV.generate(opts) do |csv| serializable_record.each {|r| csv << r} end end |
#serialize_header(options = {}) ⇒ Object
Returns the header row as a CSV string.
29 30 31 32 |
# File 'lib/delineate/serializers/csv_serializer.rb', line 29 def serialize_header( = {}) opts = {:encoding => "UTF-8"}.merge(()) CSV.generate_line(serializable_header, opts) end |
#serialize_in(csv_string, options = {}) ⇒ Object
Not implemented yet.
35 36 37 |
# File 'lib/delineate/serializers/csv_serializer.rb', line 35 def serialize_in(csv_string, = {}) raise "Serializing from CSV is not supported at this time. You can inherit a class from CsvSerializer to write a custom importer." end |