Created
February 16, 2012 22:42
-
-
Save ringe/1848449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mama | |
csv = open("lookahere") | |
qtys=[] | |
fail=[] | |
csv.each_line do |l| | |
# Replace comma with period for float value | |
ln = l.gsub(/((?<=,"\d)(,)(?=\d",))/,".") | |
# Split line on comma | |
dt = ln.split(",") | |
b = Biditem.find_by_tu_no(dt[0]) | |
fail << ln if b.blank? | |
# Parse date | |
e = Date.strptime(dt[1], "%m.%d.%y") + 12.hours | |
q = dt[2].gsub(/"(?!")/,'') | |
# Add notes | |
n = dt[3..(dt.size-2)].join(", ").gsub(/"(?!")/,'').chomp("\n") | |
qtys << Quantity.new(biditem_id: (b ? b.id : nil), entered_at: e, quantity: q, notes: n) | |
end | |
return qtys, fail | |
end | |
job = Job.find(9) | |
# Remove the old 490 qtys | |
qty_ids = job.quantities.collect {|q| q.id } | |
qty_ids = qty_ids[0..489] # Since I inserted wrong data at first (490 lines) | |
Quantity.destroy(qty_ids) | |
# Make new ones - don't save to check result first | |
qtys,fail = mama | |
# Store to DB | |
qtys.each {|q| q.save } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment