Class: CsvFile::RowBase
- Inherits:
-
Object
- Object
- CsvFile::RowBase
- Includes:
- ActiveModel::Validations
- Defined in:
- app/models/labware_creators/common_file_handling/csv_file/row_base.rb
Overview
This is an abstract class for handling rows within csv files. It provides a simple wrapper for handling and validating an individual row.
Direct Known Subclasses
Constant Summary collapse
- UNEXPECTED_NUMBER_OF_COLUMNS =
'contains an unexpected number of columns (%s expecting %s) at %s'
Instance Method Summary collapse
-
#check_for_invalid_characters ⇒ Object
Checking for use of only basic UTF-8 characters (within Rails valid UTF-8 encoding) See secure.wikimedia.org/wikipedia/en/wiki/Utf8 This check was added to prevent the error ‘malformed UTF-8’ when writing metadata to the database in labware creator classes e.g.
-
#check_has_expected_number_of_columns ⇒ Object
Validation to check if the number of columns in the row matches the expected number.
-
#empty? ⇒ Boolean
Check for whether the row is empty Here all? returns true for an empty array, and nil? returns true for nil elements.
-
#expected_number_of_columns ⇒ Object
Override in subclass Returns expected number of columns for this file tyoe e.g.
-
#initialize(index, row_data) ⇒ RowBase
constructor
A new instance of RowBase.
-
#initialize_context_specific_fields ⇒ Object
Override in subclass Example:.
-
#to_s ⇒ Object
Override in subclass For use in error messages e.g.
Constructor Details
#initialize(index, row_data) ⇒ RowBase
Returns a new instance of RowBase.
18 19 20 21 22 23 |
# File 'app/models/labware_creators/common_file_handling/csv_file/row_base.rb', line 18 def initialize(index, row_data) @index = index @row_data = row_data initialize_context_specific_fields end |
Instance Method Details
#check_for_invalid_characters ⇒ Object
Checking for use of only basic UTF-8 characters (within Rails valid UTF-8 encoding) See secure.wikimedia.org/wikipedia/en/wiki/Utf8 This check was added to prevent the error ‘malformed UTF-8’ when writing metadata to the database in labware creator classes e.g. we saw an unusual hyphen which looked like “Twist exome xE2x80x93 humgen” instead of “Twist exome - humgen” This was valid UTF-8 according to rails, but not valid UTF-8 according to the mysql2 gem that was writing to the database (which has a more restricted understanding of what is valid)
75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/labware_creators/common_file_handling/csv_file/row_base.rb', line 75 def check_for_invalid_characters return if empty? @row_data.each_with_index do |cell, i| next if cell_is_blank?(cell) next if cell_is_valid_utf8?(cell) add_invalid_character_error(i) end end |
#check_has_expected_number_of_columns ⇒ Object
Validation to check if the number of columns in the row matches the expected number. Skip if row is empty, or if we don’t want to do the check for this subclass
56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/labware_creators/common_file_handling/csv_file/row_base.rb', line 56 def check_has_expected_number_of_columns return if empty? # ignore check if expected_number_of_columns is set to -1 by subclass return if expected_number_of_columns == -1 return if @row_data.size == expected_number_of_columns errors.add(:base, format(UNEXPECTED_NUMBER_OF_COLUMNS, @row_data.size, expected_number_of_columns, to_s)) end |
#empty? ⇒ Boolean
Check for whether the row is empty Here all? returns true for an empty array, and nil? returns true for nil elements. So if @row_data is either empty or all nil, empty? will return true.
43 44 45 |
# File 'app/models/labware_creators/common_file_handling/csv_file/row_base.rb', line 43 def empty? @row_data.all?(&:nil?) end |
#expected_number_of_columns ⇒ Object
Override in subclass Returns expected number of columns for this file tyoe e.g. 3
50 51 52 |
# File 'app/models/labware_creators/common_file_handling/csv_file/row_base.rb', line 50 def expected_number_of_columns raise 'Method should be implemented within subclasses' end |
#initialize_context_specific_fields ⇒ Object
Override in subclass Example:
29 30 31 |
# File 'app/models/labware_creators/common_file_handling/csv_file/row_base.rb', line 29 def initialize_context_specific_fields raise 'Method should be implemented within subclasses' end |
#to_s ⇒ Object
Override in subclass For use in error messages e.g. “row #+ 2 [#@my_field_1]”
36 37 38 |
# File 'app/models/labware_creators/common_file_handling/csv_file/row_base.rb', line 36 def to_s raise 'Method should be implemented within subclasses' end |