pcreux / csv-importer

CSV Import for humans on Ruby / Ruby on Rails
MIT License
590 stars 67 forks source link

Add line number to after build hook #86

Closed yagudaev closed 4 years ago

yagudaev commented 5 years ago

Passed the line number so it can be consumed by the after_build callback.

In my own code, I'm using this line number to keep a reference to the original csv line that created the record.

yagudaev commented 5 years ago

Good question, let me try it against master and see if my tests pass :)

yagudaev commented 5 years ago

Damn you are good @pcreux. I owe you a beer🍺. line_number is accessible within the scope.

Do you think it is worth while passing it more explicitly as a second argument?

Something like

importer = UserImporter.new(path: 'myfile.csv') do
  after_build do |user, context|
    user.line_number = context.line_number
  end
end

line_number is not really part of the DSL and it almost looks like an unexpected side-effect that we have access to it.

pcreux commented 5 years ago

@yagudaev Sorry for the late reply. The block is instance_execed so you have access to the Row attributes such as line_number, header, etc. I believe that it's something that we could document and test.