Class: ReeMapper::Time

Inherits:
AbstractType show all
Defined in:
lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/time.rb

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/time.rb', line 16

def cast(value)
  if value.class == Time
    value
  elsif value.class == DateTime
    value.to_time
  elsif value.is_a?(String)
    begin
      Time.parse(value)
    rescue ArgumentError
      raise ReeMapper::CoercionError.new("is invalid time, got `#{truncate(value.inspect)}`")
    end
  else
    raise ReeMapper::TypeError.new("should be a time, got `#{truncate(value.inspect)}`")
  end
end

#db_dump(value) ⇒ Object



33
34
35
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/time.rb', line 33

def db_dump(value)
  serialize(value)
end

#db_load(value) ⇒ Object



38
39
40
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/time.rb', line 38

def db_load(value)
  cast(value)
end

#serialize(value) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/time.rb', line 7

def serialize(value)
  if value.class == Time
    value
  else
    raise ReeMapper::TypeError.new("should be a time, got `#{truncate(value.inspect)}`")
  end
end