Class: DateValues::Rails::TimeOfDayType

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/date_values/rails/time_of_day_type.rb

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/date_values/rails/time_of_day_type.rb', line 10

def cast(value)
  case value
  when TimeOfDay then value
  when Hash      then cast_hash(value)
  when Time      then TimeOfDay.new(value.hour, value.min, value.sec)
  when String    then TimeOfDay.parse(value)
  when nil       then nil
  end
rescue ArgumentError
  nil
end

#deserialize(value) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/date_values/rails/time_of_day_type.rb', line 26

def deserialize(value)
  case value
  when nil       then nil
  when Time      then TimeOfDay.new(value.hour, value.min, value.sec)
  when String    then TimeOfDay.parse(value)
  end
end

#serialize(value) ⇒ Object



22
23
24
# File 'lib/date_values/rails/time_of_day_type.rb', line 22

def serialize(value)
  value&.to_s
end

#typeObject



6
7
8
# File 'lib/date_values/rails/time_of_day_type.rb', line 6

def type
  :time_of_day
end