Class: CSVBox::Box

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

Instance Method Summary collapse

Constructor Details

#initialize(fields, layout) ⇒ Box

Returns a new instance of Box.



43
44
45
46
47
48
49
# File 'lib/csv_box.rb', line 43

def initialize(fields, layout)
  @csv = CSV::Table.new([])
  @rows = []
  @layout = layout

  instance_eval(&fields)
end

Instance Method Details

#<<(record) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/csv_box.rb', line 51

def <<(record)
  @cache = record
  @headers = []
  @fields = []

  instance_exec(self, &@layout)

  @rows << CSV::Row.new(@headers, @fields)
end

#to_csvObject



61
62
63
64
65
66
67
68
69
# File 'lib/csv_box.rb', line 61

def to_csv
  return @raw if @raw

  @rows.each do |row|
    @csv << row
  end

  @raw = @csv.to_csv
end