Class: Rosetta::Serializers::CSV

Inherits:
Base
  • Object
show all
Defined in:
lib/rosetta/serializers/csv.rb

Instance Attribute Summary

Attributes inherited from Base

#elements

Instance Method Summary collapse

Methods inherited from Base

call, inherited, #initialize, serialize, to_proc, #to_proc

Constructor Details

This class inherits a constructor from Rosetta::Serializers::Base

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
# File 'lib/rosetta/serializers/csv.rb', line 9

def call
  ::CSV.generate do |csv|
    csv << headers
    elements.each do |element|
      csv << headers.map { |header| serialize_value(element[header]) }
    end
  end
end

#headersObject



18
19
20
21
# File 'lib/rosetta/serializers/csv.rb', line 18

def headers
  head, *_ = elements.map(&:properties).uniq
  head
end

#validate_input!Object

Raises:



23
24
25
26
27
28
29
# File 'lib/rosetta/serializers/csv.rb', line 23

def validate_input!
  _, *others = elements.map(&:properties).uniq

  raise SerializationError, <<-ERROR.strip unless others.none?
    All objects need to share their structure to be serialized to CSV.
  ERROR
end