)
After that, the code loops through each line of the CSV file using FasterCSV.
FasterCSV handles the parsing and passes just one array per line to the code, as follows:
first = true
FasterCSV.foreach(paypal_source_file) do |line|
Inside this loop, the code is split into two parts. The first handles the first line of the
CSV file, which contains a list of column labels.
if first
first=false
line.each_with_index do |field_name, field_position|
next if field_name.strip ==''
cols[field_name.columnize] = field_position
end
CHAPTER 7 n TRACKING EXPENDITURES WITH PAYPAL 142
This loop creates a list of columns, along with their positions in the index.
The second part of the loop uses this list of column names to create a table to store
the data in if it does not already exist.
unless PaypalTransaction.table_exists?
ActiveRecord::Schema.define do
create_table PaypalTransaction.table_name do |t|
cols.each do |col, col_index|
if date_fields.include?(col)
t.column col, :date
elsif float_fields.include?(col)
t.
Pages:
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210