Class: CSVConverter::Converters::BooleanConverter

Inherits:
BaseConverter
  • Object
show all
Defined in:
lib/csv_converter/converters/boolean_converter.rb

Overview

Converts a string into a boolean

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 = { truthy_values: %w[true false] }) ⇒ BooleanConverter

A new instance of BooleanConverter.

Parameters:

  • raw_data (String)

    the raw data of the attribute being processed.

  • options (Hash) (defaults to: { truthy_values: %w[true false] })

    the options for the converter provided in the mappings. Additionally, contains the details of the data being processed. See BaseConverter#option. The truthy_values key is required. If truthy_values is blank then an error is raised.



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

def initialize(raw_data, options = { truthy_values: %w[true false] })
  super(raw_data, options)

  validate_options
end

Instance Method Details

#callBoolean

Converts data into a Boolean by checking if data is contained in the list of truthy_values provided in the mappings.

Returns:

  • (Boolean)

    if an error occurs during conversion ‘false` is returned.



21
22
23
24
25
# File 'lib/csv_converter/converters/boolean_converter.rb', line 21

def call
  call!
rescue CSVConverter::Error
  nullable_object
end

#call!Boolean

Converts data into a Boolean by checking if data is contained in the list of truthy_values provided in the mappings.

Returns:

  • (Boolean)

    if an error occurs during conversion an error is raised.



30
31
32
33
34
# File 'lib/csv_converter/converters/boolean_converter.rb', line 30

def call!
  options[:truthy_values].include?(data)
rescue StandardError => e
  raise CSVConverter::Error.new(e.message, options)
end