Class: Dina::DateCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/dina/casters/date_caster.rb

Class Method Summary collapse

Class Method Details

.cast(value, default) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/dina/casters/date_caster.rb', line 3

def self.cast(value, default)
  if value.is_a?(String)
    y, m, d = value.split '-'
    raise PropertyValueInvalid, "Invalid date" unless Date.valid_date?(y.to_i, m.to_i, d.to_i)
    Date.parse(value).to_s
  elsif value.is_a?(Date)
    value.to_s
  else
    default
  end
end