Module: XlsFunction::Converters::DateConverter

Defined in:
lib/xls_function/converters/date_converter.rb

Class Method Summary collapse

Class Method Details

.convert(obj) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/xls_function/converters/date_converter.rb', line 15

def convert(obj)
  case obj
  when Date
    obj
  when Time
    obj.to_date
  when BigDecimal
    NumberConverter.decimal_to_date(obj)
  else
    Date.parse(obj)
  end
end

.invalid_date(date_value) ⇒ Object



11
12
13
# File 'lib/xls_function/converters/date_converter.rb', line 11

def invalid_date(date_value)
  I18n.t('xls_function.errors.cannot_convert_to_date', source: date_value)
end

.try_convert(date_value) ⇒ Object



5
6
7
8
9
# File 'lib/xls_function/converters/date_converter.rb', line 5

def try_convert(date_value)
  [true, convert(date_value)]
rescue Date::Error
  [false, invalid_date(date_value)]
end