Class: Avromatic::Model::Types::CustomType

Inherits:
AbstractType show all
Defined in:
lib/avromatic/model/types/custom_type.rb

Constant Summary collapse

IDENTITY_PROC =
Proc.new { |value| value }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_type_configuration:, default_type:) ⇒ CustomType

Returns a new instance of CustomType.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/avromatic/model/types/custom_type.rb', line 13

def initialize(custom_type_configuration:, default_type:)
  @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

Instance Attribute Details

#custom_type_configurationObject (readonly)

Returns the value of attribute custom_type_configuration.



11
12
13
# File 'lib/avromatic/model/types/custom_type.rb', line 11

def custom_type_configuration
  @custom_type_configuration
end

#default_typeObject (readonly)

Returns the value of attribute default_type.



11
12
13
# File 'lib/avromatic/model/types/custom_type.rb', line 11

def default_type
  @default_type
end

#value_classesObject (readonly)

Returns the value of attribute value_classes.



11
12
13
# File 'lib/avromatic/model/types/custom_type.rb', line 11

def value_classes
  @value_classes
end

Instance Method Details

#coerce(input) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/avromatic/model/types/custom_type.rb', line 33

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) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/avromatic/model/types/custom_type.rb', line 51

def coerced?(value)
  # TODO: Delegate this to optional configuration
  coerce(value) == value
rescue ArgumentError
  false
end

#coercible?(input) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/avromatic/model/types/custom_type.rb', line 44

def coercible?(input)
  # TODO: Delegate this to optional configuration
  input.nil? || !coerce(input).nil?
rescue ArgumentError
  false
end

#input_classesObject



25
26
27
# File 'lib/avromatic/model/types/custom_type.rb', line 25

def input_classes
  # We don't know the valid input classes for a custom type
end

#nameObject



29
30
31
# File 'lib/avromatic/model/types/custom_type.rb', line 29

def name
  custom_type_configuration.value_class ? custom_type_configuration.value_class.name.to_s.freeze : default_type.name
end

#referenced_model_classesObject



62
63
64
# File 'lib/avromatic/model/types/custom_type.rb', line 62

def referenced_model_classes
  default_type.referenced_model_classes
end

#serialize(value, _strict) ⇒ Object



58
59
60
# File 'lib/avromatic/model/types/custom_type.rb', line 58

def serialize(value, _strict)
  @serializer.call(value)
end