class CSVConverter::Converters::ArrayConverter

Converts a string separated by a given char into an array of strings.

Public Class Methods

new(raw_data, options = { separator: ',' }) click to toggle source

A new instance of ArrayConverter. @param raw_data [String] the raw data of the attribute being processed. @param options [Hash] the options for the converter provided in the mappings.

Additionally, contains the details of the data being processed. See BaseConverter#option.
The *separator* key is required. If *separator* is nil then an error is raised.
Calls superclass method
# File lib/csv_converter/converters/array_converter.rb, line 12
def initialize(raw_data, options = { separator: ',' })
  super(raw_data, options)

  validate_options
end

Public Instance Methods

call() click to toggle source

Converts data into an array by splitting the string on the separator provided in the mappings. @return [Array] if an error occurs during conversion an empty array is returned.

# File lib/csv_converter/converters/array_converter.rb, line 20
def call
  call!
rescue CSVConverter::Error
  nullable_object
end
call!() click to toggle source

Converts data into an array by splitting the string on the separator provided in the mappings. @return [Array] if an error occurs during conversion an error is raised.

# File lib/csv_converter/converters/array_converter.rb, line 28
def call!
  data.split(options[:separator]).map(&:strip)
rescue StandardError => e
  raise CSVConverter::Error.new(e.message, options)
end

Private Instance Methods

nullable_object() click to toggle source
# File lib/csv_converter/converters/array_converter.rb, line 42
def nullable_object
  []
end
validate_options() click to toggle source
# File lib/csv_converter/converters/array_converter.rb, line 36
def validate_options
  return if options && !options[:separator].nil?

  raise CSVConverter::Error.new('no `key_value_separator` provided', options)
end