Class: CSVModel::Row

Inherits:
Object
  • Object
show all
Includes:
Utilities::Options
Defined in:
lib/csv_model/row.rb

Instance Attribute Summary collapse

Attributes included from Utilities::Options

#options

Instance Method Summary collapse

Methods included from Utilities::Options

#option

Constructor Details

#initialize(header, data, model_index, options = {}) ⇒ Row

Returns a new instance of Row.



9
10
11
12
13
14
# 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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/csv_model/row.rb', line 7

def data
  @data
end

#headerObject (readonly)

Returns the value of attribute header.



7
8
9
# File 'lib/csv_model/row.rb', line 7

def header
  @header
end

#marked_as_duplicateObject (readonly)

Returns the value of attribute marked_as_duplicate.



7
8
9
# File 'lib/csv_model/row.rb', line 7

def marked_as_duplicate
  @marked_as_duplicate
end

#model_indexObject (readonly) Also known as: csv_index

Returns the value of attribute model_index.



7
8
9
# File 'lib/csv_model/row.rb', line 7

def model_index
  @model_index
end

Instance Method Details

#errorsObject



24
25
26
27
28
29
# 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) ⇒ Object Also known as: []



18
19
20
21
# 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

#keyObject



31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



42
43
44
# File 'lib/csv_model/row.rb', line 42

def map_all_attributes(attrs)
  attrs
end

#map_key_attributes(attrs) ⇒ Object



46
47
48
# File 'lib/csv_model/row.rb', line 46

def map_key_attributes(attrs)
  attrs
end

#mark_as_duplicateObject



54
55
56
# File 'lib/csv_model/row.rb', line 54

def mark_as_duplicate
  @marked_as_duplicate = true
end

#marked_as_duplicate?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/csv_model/row.rb', line 50

def marked_as_duplicate?
  !!marked_as_duplicate
end

#process_rowObject



58
59
60
61
62
63
64
65
66
# 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

#statusObject



68
69
70
# File 'lib/csv_model/row.rb', line 68

def status
  model_instance.status
end

#valid?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/csv_model/row.rb', line 72

def valid?
  errors.empty?
end