Class: Csv

Inherits:
Object show all
Defined in:
lib/helpers/tmc_helpers/csv_helper/csv_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



4
5
6
# File 'lib/helpers/tmc_helpers/csv_helper/csv_helper.rb', line 4

def rows
  @rows
end

Class Method Details

.parse(data, args = {}) ⇒ Object

Public: Parses raw CSV data into a usable format.

data - String data to parse. header - Boolean indicating whether the data contains a header row (default: false).

Returns a Csv instance.



12
13
14
# File 'lib/helpers/tmc_helpers/csv_helper/csv_helper.rb', line 12

def self.parse(data, args={})
  Csv.new(data, args)
end

Instance Method Details

#to_sObject

Public: Converts the instance to a raw CSV string.

Returns a String of CSV data.



19
20
21
22
23
# File 'lib/helpers/tmc_helpers/csv_helper/csv_helper.rb', line 19

def to_s
  rows = @header.nil? ? [] : [@header]
  rows += @rows.map {|obj| obj.values}
  rows.map {|row| row.join(',')}.join("\n")
end