I have written method to load data to DB from CSV. For me it's a little bit unsexy and it's far away from DRY:
def self.from_csv(data)
c = Company.new
FasterCSV.parse(data) do |row|
c.name = row[0]
c.street = row[1]
c.street_number = row[2]
c.apartament_number = row[3]
c.city = row[4]
c.post_code = row[5]
c.post_office_city = row[6]
c.nip = row[7]
c.regon = row[8]
c.vatin = row[9]
end
end
How to make it more sexy or may be there is already any lib to load data?
You can put a sexy lady on top of it:
But, seriously, the only problem I see with your code is that you cannot rearrange attributes easily because you will have to manually update all indexes. I'd prefer to have an ordered list of attributes in an array and use some Ruby's dynamic method calling:
Also note that you need to return a constucted company instance at the end otherwise you will get some random value when calling
Company.from_csv.