class CSVBox::Box

Public Class Methods

new(fields, layout) click to toggle source
# File lib/csv_box.rb, line 43
def initialize(fields, layout)
  @csv = CSV::Table.new([])
  @rows = []
  @layout = layout

  instance_eval(&fields)
end

Public Instance Methods

<<(record) click to toggle source
# 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_csv() click to toggle source
# 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

Private Instance Methods

field(field_name) click to toggle source
# File lib/csv_box.rb, line 73
def field(field_name)
  define_singleton_method field_name do
    @headers << field_name
    @fields << @cache.public_send(field_name)
  end
end