Class: AsCSV::CSVBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/as_csv/csv_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, options = {}) ⇒ CSVBuilder

Returns a new instance of CSVBuilder.



5
6
7
8
# File 'lib/as_csv/csv_builder.rb', line 5

def initialize(records, options={})
  @records = [*records]
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/as_csv/csv_builder.rb', line 3

def options
  @options
end

#recordsObject (readonly)

Returns the value of attribute records.



3
4
5
# File 'lib/as_csv/csv_builder.rb', line 3

def records
  @records
end

Instance Method Details

#data_rowsObject



22
23
24
# File 'lib/as_csv/csv_builder.rb', line 22

def data_rows
  @data_rows ||= csv_hashes.collect { |csv_hash| data_row csv_hash } if valid?
end

#headersObject



18
19
20
# File 'lib/as_csv/csv_builder.rb', line 18

def headers
  @headers ||= csv_hashes.collect(&:keys).flatten.uniq if valid?
end

#rowsObject



14
15
16
# File 'lib/as_csv/csv_builder.rb', line 14

def rows
  @rows ||= [headers] + data_rows if valid?
end

#to_csvObject



10
11
12
# File 'lib/as_csv/csv_builder.rb', line 10

def to_csv
  rows.collect { |row| CSVProxy.generate_line row }.join if valid?
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/as_csv/csv_builder.rb', line 26

def valid?
  csv_hashes.any? && validate_hashes!
end