Sha256: 9c16117b9da1491e48926e067d662930e36e81e864cbded4acb4f658f06128ed

Contents?: true

Size: 1.49 KB

Versions: 13

Compression:

Stored size: 1.49 KB

Contents

require 'avromatic/model/custom_type'

module Avromatic
  module Model
    class TypeRegistry

      delegate :clear, to: :custom_types

      def initialize
        @custom_types = {}
      end

      # @param fullname [String] The fullname of the Avro type.
      # @param value_class [Class] Optional class to use for the attribute.
      #   If unspecified then the default class for the Avro field is used.
      # @block If a block is specified then the CustomType is yielded for
      #   additional configuration.
      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

      # @object [Avro::Schema] Custom type may be fetched based on a Avro field
      #   or schema. If there is no custom type, then NullCustomType is returned.
      # @field_class [Object] Value class that has been determined for a field.
      def fetch(object, field_class = nil)
        field_type = object.is_a?(Avro::Schema::Field) ? object.type : object

        if field_class && field_type.type_sym == :union && !(field_class < Avromatic::Model::AttributeType::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

      private

      attr_reader :custom_types
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
avromatic-0.11.1 lib/avromatic/model/type_registry.rb
avromatic-0.11.0 lib/avromatic/model/type_registry.rb
avromatic-0.10.0 lib/avromatic/model/type_registry.rb
avromatic-0.10.0.rc1 lib/avromatic/model/type_registry.rb
avromatic-0.10.0.rc0 lib/avromatic/model/type_registry.rb
avromatic-0.9.0 lib/avromatic/model/type_registry.rb
avromatic-0.9.0.rc7 lib/avromatic/model/type_registry.rb
avromatic-0.9.0.rc6 lib/avromatic/model/type_registry.rb
avromatic-0.9.0.rc4 lib/avromatic/model/type_registry.rb
avromatic-0.9.0.rc3 lib/avromatic/model/type_registry.rb
avromatic-0.9.0.rc2 lib/avromatic/model/type_registry.rb
avromatic-0.9.0.rc1 lib/avromatic/model/type_registry.rb
avromatic-0.9.0.rc0 lib/avromatic/model/type_registry.rb