I have the following code that retrieves the rows from a txt file, I would like to sum all the values of item_price to be able to use it in my application. I'd like this value to be retrieved out of the block:
CSV.foreach(dir, col_sep: "\t", headers: true).map do |row|
Follow the code:
module ParseText
def self.parse
require 'csv'
dir = Dir.glob("./public/system/file_attaches/files/*/*/*/*/*").max_by {|f| File.mtime(f)}
CSV.foreach(dir, col_sep: "\t", headers: true).map do |row|
records = row.to_h
purchaser = records["purchaser name"]
item_description = records["item description"]
item_price = records["item price"]
purchase_count = records["purchase count"]
merchant_address = records["merchant_address"]
merchant_name = records["merchant name"]
Merchant.create(name: merchant_name)
MerchantAddress.create(street: merchant_address, merchant_id: Merchant.last.id)
Purchaser.create(name: purchaser, count: purchase_count, merchant_id: Merchant.last.id)
Item.create(description: item_description, price: item_price, merchant_id: Merchant.last.id, purchaser_id: Purchaser.last.id )
end
end
end