Class: Avromatic::Model::Types::AbstractTimestampType

Inherits:
AbstractType
  • Object
show all
Defined in:
lib/avromatic/model/types/abstract_timestamp_type.rb

Direct Known Subclasses

TimestampMicrosType, TimestampMillisType

Constant Summary collapse

VALUE_CLASSES =
[::Time].freeze
INPUT_CLASSES =
[::Time, ::DateTime, ::ActiveSupport::TimeWithZone].freeze

Instance Method Summary collapse

Methods inherited from AbstractType

#name, #referenced_model_classes

Instance Method Details

#coerce(input) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/avromatic/model/types/abstract_timestamp_type.rb', line 20

def coerce(input)
  if input.nil? || coerced?(input)
    input
  elsif input.is_a?(::Time) || input.is_a?(::DateTime)
    coerce_time(input)
  else
    raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
  end
end

#coerced?(value) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/avromatic/model/types/abstract_timestamp_type.rb', line 34

def coerced?(value)
  # ActiveSupport::TimeWithZone overrides is_a? is to make it look like a Time
  # even though it's not which can lead to unexpected behavior if we don't force
  # a coercion
  value.is_a?(::Time) && value.class != ActiveSupport::TimeWithZone && truncated?(value)
end

#coercible?(input) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/avromatic/model/types/abstract_timestamp_type.rb', line 30

def coercible?(input)
  input.nil? || input.is_a?(::Time) || input.is_a?(::DateTime)
end

#input_classesObject



16
17
18
# File 'lib/avromatic/model/types/abstract_timestamp_type.rb', line 16

def input_classes
  INPUT_CLASSES
end

#serialize(value, _strict) ⇒ Object



41
42
43
# File 'lib/avromatic/model/types/abstract_timestamp_type.rb', line 41

def serialize(value, _strict)
  value
end

#value_classesObject



12
13
14
# File 'lib/avromatic/model/types/abstract_timestamp_type.rb', line 12

def value_classes
  VALUE_CLASSES
end