# File lib/csv_model/row.rb, line 136 def primary_key_columns header.primary_key_columns end
class CSVModel::Row
Attributes
csv_index[R]
data[R]
header[R]
marked_as_duplicate[R]
model_index[R]
Public Class Methods
new(header, data, model_index, options = {})
click to toggle source
# File lib/csv_model/row.rb, line 9 def initialize(header, data, model_index, options = {}) @header = header @data = data @model_index = model_index @options = options end
Public Instance Methods
errors()
click to toggle source
# File lib/csv_model/row.rb, line 24 def errors errors = [] errors << duplicate_row_error if is_dry_run? && marked_as_duplicate? errors << model_instance.errors if !model_instance.valid? errors.flatten end
index(value)
click to toggle source
# File lib/csv_model/row.rb, line 18 def index(value) index = column_index(value) || value data[index] if index.is_a?(Integer) && index >= 0 end
Also aliased as: []
key()
click to toggle source
# File lib/csv_model/row.rb, line 31 def key cols = primary_key_columns if cols.one? index(cols.first.key) elsif cols.any? cols.collect { |x| index(x.key) } else data end end
map_all_attributes(attrs)
click to toggle source
# File lib/csv_model/row.rb, line 42 def map_all_attributes(attrs) attrs end
map_key_attributes(attrs)
click to toggle source
# File lib/csv_model/row.rb, line 46 def map_key_attributes(attrs) attrs end
mark_as_duplicate()
click to toggle source
# File lib/csv_model/row.rb, line 54 def mark_as_duplicate @marked_as_duplicate = true end
marked_as_duplicate?()
click to toggle source
# File lib/csv_model/row.rb, line 50 def marked_as_duplicate? !!marked_as_duplicate end
process_row()
click to toggle source
# File lib/csv_model/row.rb, line 58 def process_row return model_instance.status if @processed @processed = true model_instance.assign_attributes(all_attributes) model_instance.mark_as_duplicate if marked_as_duplicate? model_instance.save(dry_run: is_dry_run?) model_instance.status end
status()
click to toggle source
# File lib/csv_model/row.rb, line 68 def status model_instance.status end
valid?()
click to toggle source
# File lib/csv_model/row.rb, line 72 def valid? errors.empty? end
Private Instance Methods
all_attributes()
click to toggle source
# File lib/csv_model/row.rb, line 86 def all_attributes @all_attributes ||= model_mapper.map_all_attributes(column_attributes_with_values(columns)) end
column_attributes_with_values(cols)
click to toggle source
# File lib/csv_model/row.rb, line 94 def column_attributes_with_values(cols) Hash[cols.collect { |col| [col.model_attribute, index(col.key)] }] end
column_index(key)
click to toggle source
# File lib/csv_model/row.rb, line 98 def column_index(key) header.column_index(key) end
columns()
click to toggle source
# File lib/csv_model/row.rb, line 90 def columns header.columns end
duplicate_row_error()
click to toggle source
# File lib/csv_model/row.rb, line 102 def duplicate_row_error names = primary_key_columns.collect { |x| x.name } names.any? ? "Duplicate #{names.join(', ')}" : "Duplicate row" end
inherit_or_delegate(method, *args)
click to toggle source
# File lib/csv_model/row.rb, line 142 def inherit_or_delegate(method, *args) try(method, *args) || model_finder.try(method, *args) end
is_dry_run?()
click to toggle source
# File lib/csv_model/row.rb, line 107 def is_dry_run? option(:dry_run, false) end
key_attributes()
click to toggle source
# File lib/csv_model/row.rb, line 131 def key_attributes cols = primary_key_columns.any? ? primary_key_columns : columns @key_attributes ||= model_mapper.map_key_attributes(column_attributes_with_values(cols)) end
model_adaptor()
click to toggle source
# File lib/csv_model/row.rb, line 111 def model_adaptor CSVModel::RowActiveRecordAdaptor end
model_finder()
click to toggle source
# File lib/csv_model/row.rb, line 115 def model_finder option(:row_model_finder) end
model_instance()
click to toggle source
# File lib/csv_model/row.rb, line 119 def model_instance @model_instance ||= begin x = inherit_or_delegate(:find_row_model, key_attributes) x ||= inherit_or_delegate(:new_row_model, key_attributes) x = model_adaptor.new(x) end end
model_mapper()
click to toggle source
# File lib/csv_model/row.rb, line 127 def model_mapper option(:row_model_mapper, self) end
primary_key_columns()
click to toggle source