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.



39
40
41
42
43
44
45
# File 'lib/csv_box.rb', line 39

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

  instance_eval(&fields)
end

Instance Method Details

#<<(record) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/csv_box.rb', line 47

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

  instance_exec(self, &@layout)

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

#to_csvObject



57
58
59
60
61
62
63
64
65
# File 'lib/csv_box.rb', line 57

def to_csv
  return @raw if @raw

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

  @raw = @csv.to_csv
end