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
24
# 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

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



38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# 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) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# 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_classesObject



26
27
28
# 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

#nameObject



30
31
32
33
34
35
36
# 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_classesObject



67
68
69
# File 'lib/avromatic/model/types/custom_type.rb', line 67

def referenced_model_classes
  default_type.referenced_model_classes
end

#serialize(value, _strict) ⇒ Object



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

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