Class: Avromatic::Model::CustomTypeRegistry

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

Instance Method Summary collapse

Constructor Details

#initializeCustomTypeRegistry

Returns a new instance of CustomTypeRegistry.



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

def initialize
  @custom_types = {}
end

Instance Method Details

#fetch(object) ⇒ Object



36
37
38
39
40
# File 'lib/avromatic/model/custom_type_registry.rb', line 36

def fetch(object)
  field_type = object.is_a?(Avro::Schema::Field) ? object.type : object
  fullname = field_type.fullname if field_type.is_a?(Avro::Schema::NamedSchema)
  custom_types.fetch(fullname)
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.



20
21
22
23
24
# File 'lib/avromatic/model/custom_type_registry.rb', line 20

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

#registered?(object) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/avromatic/model/custom_type_registry.rb', line 28

def registered?(object)
  field_type = object.is_a?(Avro::Schema::Field) ? object.type : object
  custom_types.include?(field_type.fullname) if field_type.is_a?(Avro::Schema::NamedSchema)
end