Module: Sipity::Conversions::ConvertToYear

Defined in:
app/conversions/sipity/conversions/convert_to_year.rb

Overview

Exposes a conversion method to take an input and transform it into a year.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(input) ⇒ Object

A convenience method so that you don't need to include the conversion module in your base class.



8
9
10
# File 'app/conversions/sipity/conversions/convert_to_year.rb', line 8

def self.call(input)
  convert_to_year(input)
end

.convert_to_year(input) ⇒ Object (private)

Does its best to convert the input into a year.



17
18
19
20
21
22
23
24
25
# File 'app/conversions/sipity/conversions/convert_to_year.rb', line 17

def convert_to_year(input)
  return input if input.is_a?(Fixnum)
  return input.to_year if input.respond_to?(:to_year)
  return input.year if input.respond_to?(:year)
  return convert_to_year(input.to_date) if input.respond_to?(:to_date)
  return convert_to_year(input.to_time) if input.respond_to?(:to_time)
rescue ArgumentError
  return input.to_i == 0 ? nil : input.to_i if input.respond_to?(:to_i)
end

Instance Method Details

#convert_to_year(input) ⇒ Object (private)

Does its best to convert the input into a year.



17
18
19
20
21
22
23
24
25
# File 'app/conversions/sipity/conversions/convert_to_year.rb', line 17

def convert_to_year(input)
  return input if input.is_a?(Fixnum)
  return input.to_year if input.respond_to?(:to_year)
  return input.year if input.respond_to?(:year)
  return convert_to_year(input.to_date) if input.respond_to?(:to_date)
  return convert_to_year(input.to_time) if input.respond_to?(:to_time)
rescue ArgumentError
  return input.to_i == 0 ? nil : input.to_i if input.respond_to?(:to_i)
end