Class: ActiveFields::Casters::DateTimeCaster

Inherits:
BaseCaster
  • Object
show all
Defined in:
lib/active_fields/casters/date_time_caster.rb

Direct Known Subclasses

DateTimeArrayCaster

Constant Summary collapse

MAX_PRECISION =

AR max precision is 6 for old Rubies

RUBY_VERSION >= "3.2" ? 9 : 6

Instance Attribute Summary

Attributes inherited from BaseCaster

#options

Instance Method Summary collapse

Methods inherited from BaseCaster

#initialize

Constructor Details

This class inherits a constructor from ActiveFields::Casters::BaseCaster

Instance Method Details

#deserialize(value) ⇒ Object



15
16
17
18
19
20
# File 'lib/active_fields/casters/date_time_caster.rb', line 15

def deserialize(value)
  value = value.iso8601 if value.is_a?(Date)
  casted_value = caster.deserialize(value)

  apply_precision(casted_value).in_time_zone if casted_value.acts_like?(:time)
end

#serialize(value) ⇒ Object



8
9
10
11
12
13
# File 'lib/active_fields/casters/date_time_caster.rb', line 8

def serialize(value)
  value = value.iso8601 if value.is_a?(Date)
  casted_value = caster.serialize(value)

  casted_value.iso8601(precision) if casted_value.acts_like?(:time)
end