Class: CSVConverter::Converters::ArrayConverter

Inherits:
BaseConverter show all
Defined in:
lib/csv_converter/converters/array_converter.rb

Overview

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

Instance Attribute Summary

Attributes inherited from BaseConverter

#options, #raw_data

Instance Method Summary collapse

Methods inherited from BaseConverter

#data, #empty_value?

Constructor Details

#initialize(raw_data, options = { separator: ',' }) ⇒ ArrayConverter

A new instance of ArrayConverter.

Parameters:

  • raw_data (String)

    the raw data of the attribute being processed.

  • options (Hash) (defaults to: { separator: ',' })

    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.



12
13
14
15
16
# File 'lib/csv_converter/converters/array_converter.rb', line 12

def initialize(raw_data, options = { separator: ',' })
  super(raw_data, options)

  validate_options
end

Instance Method Details

#callArray

Converts data into an array by splitting the string on the separator provided in the mappings.

Returns:

  • (Array)

    if an error occurs during conversion an empty array is returned.



20
21
22
23
24
# File 'lib/csv_converter/converters/array_converter.rb', line 20

def call
  call!
rescue CSVConverter::Error
  nullable_object
end

#call!Array

Converts data into an array by splitting the string on the separator provided in the mappings.

Returns:

  • (Array)

    if an error occurs during conversion an error is raised.



28
29
30
31
32
# 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