class Avromatic::Model::Types::CustomType

Constants

IDENTITY_PROC

Attributes

custom_type_configuration[R]
default_type[R]
value_classes[R]

Public Class Methods

new(custom_type_configuration:, default_type:) click to toggle source
Calls superclass method
# File lib/avromatic/model/types/custom_type.rb, line 13
def initialize(custom_type_configuration:, default_type:)
  super()
  @custom_type_configuration = custom_type_configuration
  @default_type = default_type
  @deserializer = custom_type_configuration.deserializer || IDENTITY_PROC
  @serializer = custom_type_configuration.serializer || IDENTITY_PROC
  @value_classes = if custom_type_configuration.value_class
                     [custom_type_configuration.value_class].freeze
                   else
                     default_type.value_classes
                   end
end

Public Instance Methods

coerce(input) click to toggle source
# File lib/avromatic/model/types/custom_type.rb, line 38
def coerce(input)
  if input.nil?
    input
  else
    @deserializer.call(input)
  end
rescue StandardError => e
  # TODO: Don't swallow this
  raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}: #{e.message}")
end
coerced?(value) click to toggle source
# File lib/avromatic/model/types/custom_type.rb, line 56
def coerced?(value)
  # TODO: Delegate this to optional configuration
  coerce(value) == value
rescue ArgumentError
  false
end
coercible?(input) click to toggle source
# File lib/avromatic/model/types/custom_type.rb, line 49
def coercible?(input)
  # TODO: Delegate this to optional configuration
  input.nil? || !coerce(input).nil?
rescue ArgumentError
  false
end
input_classes() click to toggle source
# File lib/avromatic/model/types/custom_type.rb, line 26
def input_classes
  # We don't know the valid input classes for a custom type
end
name() click to toggle source
# File lib/avromatic/model/types/custom_type.rb, line 30
def name
  if custom_type_configuration.value_class
    custom_type_configuration.value_class.name.to_s.freeze
  else
    default_type.name
  end
end
referenced_model_classes() click to toggle source
# File lib/avromatic/model/types/custom_type.rb, line 67
def referenced_model_classes
  default_type.referenced_model_classes
end
serialize(value, _strict) click to toggle source
# File lib/avromatic/model/types/custom_type.rb, line 63
def serialize(value, _strict)
  @serializer.call(value)
end