Sha256: 9f6b55b895ae0908249c66e2255b932a7ffc175c666a40a61c52345ea7efdbef

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 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.
      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

      private

      attr_reader :custom_types
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
avromatic-0.8.0 lib/avromatic/model/type_registry.rb
avromatic-0.7.1 lib/avromatic/model/type_registry.rb
avromatic-0.7.0 lib/avromatic/model/type_registry.rb
avromatic-0.6.2 lib/avromatic/model/type_registry.rb
avromatic-0.6.1 lib/avromatic/model/type_registry.rb
avromatic-0.6.0 lib/avromatic/model/type_registry.rb
avromatic-0.5.0 lib/avromatic/model/type_registry.rb
avromatic-0.4.0 lib/avromatic/model/type_registry.rb