lib/csv_importer/row.rb in csv-importer-0.5.0 vs lib/csv_importer/row.rb in csv-importer-0.6.0
- old
+ new
@@ -39,28 +39,30 @@
value = value.dup if value
rescue TypeError
# can't dup Symbols, Integer etc...
end
- column_definition = column.definition
- next if column_definition.nil?
+ next if column.definition.nil?
- set_attribute(model, column_definition, value)
+ set_attribute(model, column, value)
end
model
end
# Set the attribute using the column_definition and the csv_value
- def set_attribute(model, column_definition, csv_value)
+ def set_attribute(model, column, csv_value)
+ column_definition = column.definition
if column_definition.to && column_definition.to.is_a?(Proc)
to_proc = column_definition.to
case to_proc.arity
when 1 # to: ->(email) { email.downcase }
model.public_send("#{column_definition.name}=", to_proc.call(csv_value))
when 2 # to: ->(published, post) { post.published_at = Time.now if published == "true" }
to_proc.call(csv_value, model)
+ when 3 # to: ->(field_value, post, column) { post.hash_field[column.name] = field_value }
+ to_proc.call(csv_value, model, column)
else
raise ArgumentError, "`to` proc can only have 1 or 2 arguments"
end
else
attribute = column_definition.attribute