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
16
# File 'lib/avromatic/model/types/record_type.rb', line 11

def initialize(record_class:)
  super()
  @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



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

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)


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

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

#coercible?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

def name
  record_class.name.to_s.freeze
end

#referenced_model_classesObject



53
54
55
# File 'lib/avromatic/model/types/record_type.rb', line 53

def referenced_model_classes
  [record_class].freeze
end

#serialize(value, strict) ⇒ Object



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

def serialize(value, strict)
  if value.nil?
    value
  elsif strict
    value.avro_value_datum
  else
    value.value_attributes_for_avro
  end
end