Class: CSVConverter::Converters::BaseConverter
- Inherits:
-
Object
- Object
- CSVConverter::Converters::BaseConverter
- Defined in:
- lib/csv_converter/converters/base_converter.rb
Overview
Defines the interface that all converters must implement
Direct Known Subclasses
ArrayConverter, BigDecimalConverter, BooleanConverter, DateConverter, FloatConverter, HashConverter, IntegerConverter, LowercaseConverter, StringConverter, UppercaseConverter
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Details of the data being processed.
-
#raw_data ⇒ String
readonly
The raw data of the attribute being processed.
Instance Method Summary collapse
-
#call ⇒ Object
Converts raw_data into the type specified in the mappings.
-
#call! ⇒ Object
Converts raw_data into the type specified in the mappings.
-
#data ⇒ String
Evaluates raw_data and returns the proper value for it.
-
#empty_value? ⇒ Boolean
Checks if raw_data is contained in the list of empty_values provided in the mapping.
-
#initialize(raw_data, options = {}) ⇒ BaseConverter
constructor
A new instance of BaseConverter.
Constructor Details
#initialize(raw_data, options = {}) ⇒ BaseConverter
A new instance of BaseConverter.
23 24 25 26 |
# File 'lib/csv_converter/converters/base_converter.rb', line 23 def initialize(raw_data, = {}) @raw_data = raw_data.to_s.strip @options = || {} end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Details of the data being processed. By default this includes:
filename: the name of the file being processed
row_num: number of the row being processed
entity: the name of the entity being processed as provided in the mappings
row: the raw data of the row being processed
attr: the name of the attribute being processed as provided in the mappings
Additionally it contains all the options provided to the converter in the mappings.
17 18 19 |
# File 'lib/csv_converter/converters/base_converter.rb', line 17 def @options end |
#raw_data ⇒ String (readonly)
Returns the raw data of the attribute being processed.
8 9 10 |
# File 'lib/csv_converter/converters/base_converter.rb', line 8 def raw_data @raw_data end |
Instance Method Details
#call ⇒ Object
Converts raw_data into the type specified in the mappings. Must be implemented by children
30 31 32 |
# File 'lib/csv_converter/converters/base_converter.rb', line 30 def call raise NotImplementedError end |
#call! ⇒ Object
Converts raw_data into the type specified in the mappings. Must be implemented by children
36 37 38 |
# File 'lib/csv_converter/converters/base_converter.rb', line 36 def call! raise NotImplementedError end |
#data ⇒ String
Evaluates raw_data and returns the proper value for it.
45 46 47 48 49 50 51 52 53 |
# File 'lib/csv_converter/converters/base_converter.rb', line 45 def data @data ||= begin return raw_data if raw_data.present? && !empty_value? return nullable_object if .dig(:default).blank? .dig(:default) end end |
#empty_value? ⇒ Boolean
Checks if raw_data is contained in the list of empty_values provided in the mapping.
57 58 59 60 61 |
# File 'lib/csv_converter/converters/base_converter.rb', line 57 def empty_value? return false unless .dig(:empty_values) .dig(:empty_values).include?(raw_data) end |