Class: Avromatic::Model::Types::RecordType

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_class:) ⇒ RecordType

Returns a new instance of RecordType.



11
12
13
14
15
# File 'lib/avromatic/model/types/record_type.rb', line 11

def initialize(record_class:)
  @record_class = record_class
  @value_classes = [record_class].freeze
  @input_classes = [record_class, Hash].freeze
end

Instance Attribute Details

#input_classesObject (readonly)

Returns the value of attribute input_classes.



9
10
11
# File 'lib/avromatic/model/types/record_type.rb', line 9

def input_classes
  @input_classes
end

#record_classObject (readonly)

Returns the value of attribute record_class.



9
10
11
# File 'lib/avromatic/model/types/record_type.rb', line 9

def record_class
  @record_class
end

#value_classesObject (readonly)

Returns the value of attribute value_classes.



9
10
11
# File 'lib/avromatic/model/types/record_type.rb', line 9

def value_classes
  @value_classes
end

Instance Method Details

#coerce(input) ⇒ Object



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

def coerce(input)
  if input.nil? || input.is_a?(record_class)
    input
  elsif input.is_a?(Hash)
    record_class.new(input)
  else
    raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
  end
end

#coerced?(value) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/avromatic/model/types/record_type.rb', line 38

def coerced?(value)
  value.nil? || value.is_a?(record_class)
end

#coercible?(input) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/avromatic/model/types/record_type.rb', line 31

def coercible?(input)
  # TODO: Is there a better way to figure this out?
  input.nil? || input.is_a?(record_class) || coerce(input).valid?
rescue StandardError
  false
end

#nameObject



17
18
19
# File 'lib/avromatic/model/types/record_type.rb', line 17

def name
  record_class.name.to_s.freeze
end

#referenced_model_classesObject



55
56
57
# File 'lib/avromatic/model/types/record_type.rb', line 55

def referenced_model_classes
  [record_class].freeze
end

#serialize(value, strict:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/avromatic/model/types/record_type.rb', line 42

def serialize(value, strict:)
  if value.nil?
    value
  elsif !strict && Avromatic.use_custom_datum_writer && Avromatic.use_encoding_providers? && !record_class.config.mutable
    # n.b. Ideally we'd just return value here instead of wrapping it in a
    # hash but then we'd have no place to stash the union member index...
    { Avromatic::IO::ENCODING_PROVIDER => value }
  else
    # This is only used for recursive serialization so validation has already been done
    strict ? value.avro_value_datum(validate: false) : value.value_attributes_for_avro(validate: false)
  end
end