Class: CSVModel::Row
- Inherits:
-
Object
- Object
- CSVModel::Row
- Includes:
- Utilities::Options
- Defined in:
- lib/csv_model/row.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#marked_as_duplicate ⇒ Object
readonly
Returns the value of attribute marked_as_duplicate.
-
#model_index ⇒ Object
(also: #csv_index)
readonly
Returns the value of attribute model_index.
Attributes included from Utilities::Options
Instance Method Summary collapse
- #errors ⇒ Object
- #index(value) ⇒ Object (also: #[])
-
#initialize(header, data, model_index, options = {}) ⇒ Row
constructor
A new instance of Row.
- #key ⇒ Object
- #map_all_attributes(attrs) ⇒ Object
- #map_key_attributes(attrs) ⇒ Object
- #mark_as_duplicate ⇒ Object
- #marked_as_duplicate? ⇒ Boolean
- #process_row ⇒ Object
- #status ⇒ Object
- #valid? ⇒ Boolean
Methods included from Utilities::Options
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, = {}) @header = header @data = data @model_index = model_index @options = end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/csv_model/row.rb', line 7 def data @data end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
7 8 9 |
# File 'lib/csv_model/row.rb', line 7 def header @header end |
#marked_as_duplicate ⇒ Object (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_index ⇒ Object (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
#errors ⇒ Object
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 |
#key ⇒ Object
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_duplicate ⇒ Object
54 55 56 |
# File 'lib/csv_model/row.rb', line 54 def mark_as_duplicate @marked_as_duplicate = true end |
#marked_as_duplicate? ⇒ Boolean
50 51 52 |
# File 'lib/csv_model/row.rb', line 50 def marked_as_duplicate? !!marked_as_duplicate end |
#process_row ⇒ Object
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 |
#status ⇒ Object
68 69 70 |
# File 'lib/csv_model/row.rb', line 68 def status model_instance.status end |
#valid? ⇒ Boolean
72 73 74 |
# File 'lib/csv_model/row.rb', line 72 def valid? errors.empty? end |