CSV Model is a bulk import library for ActiveRecord. It parses a CSV into memory, validates the structure, can check for duplicates, and creates or updates the relevant record for each row.
CSV Model requires Ruby version >= 2.0.0
CSVModel is distributed as a gem, which is how it should be used in your app.
Include the gem in your Gemfile:
gem "csv_model", "~> 1.0"
Or, if you want to get the latest, you can get master from the main repository:
gem "csv_model", :git => "git://github.com/Scrimmage/csv_model.git"
options = {
dry_run: is_dry_run?,
legal_columns: structure.legal_columns,
primary_key: structure.primary_key_columns,
required_columns: structure.required_columns,
row_model_finder: model_finder,
row_model_mapper: model_mapper,
}
cm = CSVModel::Model.new(data, options)
if cm.structure_valid?
puts "Found structural errors: #{csv.structure_errors.inspect}"
else
cm.rows.each do |row|
status = row.process_row
puts "Found row #{row.key} with status #{status} and errors #{row.errors.inspect}"
end
end
Deployment:
gem build csv_model.gemspec
gem push csv_model-x.y.z.gem