class CSVConverter::Converters::DateConverter

Converts a string into a date

Public Class Methods

new(raw_data, options = { date_format: '%m/%d/%y' }) click to toggle source

A new instance of DateConverter. @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 *date_format* key is required. If *date_format* is blank then an error is raised.
# File lib/csv_converter/converters/date_converter.rb, line 12
def initialize(raw_data, options = { date_format: '%m/%d/%y' })
  super(raw_data, options)

  validate_options
end

Public Instance Methods

call() click to toggle source

Converts data into a Date using the format provided in the mappings. @return [Date] if an error occurs during conversion `nil` is returned.

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

Converts data into a Date using the format provided in the mappings. @return [Date] if an error occurs during conversion an error is raised.

# File lib/csv_converter/converters/date_converter.rb, line 28
def call!
  Date.strptime(data, options[:date_format])
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/date_converter.rb, line 42
def nullable_object
  nil
end
validate_options() click to toggle source
# File lib/csv_converter/converters/date_converter.rb, line 36
def validate_options
  return if options && options[:date_format].present?

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