Class: Avromatic::Model::TypeRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/avromatic/model/type_registry.rb

Instance Method Summary collapse

Constructor Details

#initializeTypeRegistry

Returns a new instance of TypeRegistry.



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

def initialize
  @custom_types = {}
end

Instance Method Details

#fetch(object) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/avromatic/model/type_registry.rb', line 26

def fetch(object)
  field_type = object.is_a?(Avro::Schema::Field) ? object.type : object

  if field_type.type_sym == :union
    field_type = Avromatic::Model::Attributes.first_union_schema(field_type)
  end

  fullname = field_type.fullname if field_type.is_a?(Avro::Schema::NamedSchema)
  custom_types.fetch(fullname, NullCustomType)
end

#register_type(fullname, value_class = nil) ⇒ Object

Parameters:

  • fullname (String)

    The fullname of the Avro type.

  • value_class (Class) (defaults to: nil)

    Optional class to use for the attribute. If unspecified then the default class for the Avro field is used.



18
19
20
21
22
# File 'lib/avromatic/model/type_registry.rb', line 18

def register_type(fullname, value_class = nil)
  custom_types[fullname.to_s] = Avromatic::Model::CustomType.new(value_class).tap do |type|
    yield(type) if block_given?
  end
end