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, options = {}) ⇒ Row

Returns a new instance of Row.



9
10
11
12
13
# File 'lib/csv_model/row.rb', line 9

def initialize(header, data, options = {})
  @header = header
  @data = data
  @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

Instance Method Details

#errorsObject



21
22
23
24
25
26
# File 'lib/csv_model/row.rb', line 21

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: []



15
16
17
18
# File 'lib/csv_model/row.rb', line 15

def index(value)
  index = column_index(value) || value
  data[index] if index.is_a?(Fixnum) && index >= 0
end

#keyObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/csv_model/row.rb', line 28

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

#mark_as_duplicateObject



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

def mark_as_duplicate
  @marked_as_duplicate = true
end

#marked_as_duplicate?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/csv_model/row.rb', line 39

def marked_as_duplicate?
  !!marked_as_duplicate
end

#statusObject



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

def status
  model_instance.status
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  errors.empty?
end