class CSVConverter::Converters::BooleanConverter

Converts a string into a boolean

Public Class Methods

new(raw_data, options = { truthy_values: %w[true false] }) click to toggle source

A new instance of BooleanConverter. @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 *truthy_values* key is required. If *truthy_values* is blank then an error is raised.
# 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

Public Instance Methods

call() click to toggle source

Converts data into a Boolean by checking if data is contained in the list of truthy_values provided in the mappings. @return [Boolean] if an error occurs during conversion `false` is returned.

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

Converts data into a Boolean by checking if data is contained in the list of truthy_values provided in the mappings. @return [Boolean] if an error occurs during conversion an error is raised.

# 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

Private Instance Methods

nullable_object() click to toggle source
# File lib/csv_converter/converters/boolean_converter.rb, line 44
def nullable_object
  false
end
validate_options() click to toggle source
# File lib/csv_converter/converters/boolean_converter.rb, line 38
def validate_options
  return if options && options[:truthy_values].present?

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