Class: CsvSerializer::Serializer
- Inherits:
-
Object
- Object
- CsvSerializer::Serializer
- Defined in:
- lib/csv_serializer/serializer.rb
Overview
Provides functions to generate csv string and file from column definition object.
Instance Attribute Summary collapse
-
#definitions ⇒ Object
readonly
Returns the value of attribute definitions.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#initialize(definitions) ⇒ Serializer
constructor
A new instance of Serializer.
- #serialize ⇒ Object
- #serialize_to(io) ⇒ Object
Constructor Details
#initialize(definitions) ⇒ Serializer
Returns a new instance of Serializer.
9 10 11 |
# File 'lib/csv_serializer/serializer.rb', line 9 def initialize(definitions) @definitions = definitions end |
Instance Attribute Details
#definitions ⇒ Object (readonly)
Returns the value of attribute definitions.
7 8 9 |
# File 'lib/csv_serializer/serializer.rb', line 7 def definitions @definitions end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
7 8 9 |
# File 'lib/csv_serializer/serializer.rb', line 7 def records @records end |
Instance Method Details
#serialize ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/csv_serializer/serializer.rb', line 13 def serialize CSV.generate do |csv| csv << definitions.header definitions.target_records.each do |record| csv << definitions.process(record) end end end |
#serialize_to(io) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/csv_serializer/serializer.rb', line 22 def serialize_to(io) io << CSV.generate_line(definitions.header) definitions.target_records.each do |record| io << CSV.generate_line(definitions.process(record)) end end |