Module: ApiResource::Typecast::DateTypecaster

Defined in:
lib/api_resource/typecasters/date_typecaster.rb

Class Method Summary collapse

Class Method Details

.from_api(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/api_resource/typecasters/date_typecaster.rb', line 9

def self.from_api(value)
  return value if value.is_a?(Date)

  value = value.to_s
  if value =~ ApiResource::Typecast::ISO_DATE
    return self.new_date($1.to_i, $2.to_i, $3.to_i)
  end

  self.new_date(*::Date._parse(value, false).values_at(:year, :mon, :mday))
end

.new_date(year, month, day) ⇒ Object



26
27
28
29
# File 'lib/api_resource/typecasters/date_typecaster.rb', line 26

def self.new_date(year, month, day)
  return nil unless year && year != 0
  return Date.new(year, month, day) rescue nil
end

.to_api(value) ⇒ Object



20
21
22
# File 'lib/api_resource/typecasters/date_typecaster.rb', line 20

def self.to_api(value)
  return value.to_s
end