module Avro::Builder::TypeFactory

This concern is used by classes that create new Type instances.

Constants

BUILTIN_TYPES
COMPLEX_TYPES
NAMED_TYPES

Private Instance Methods

builtin_type?(avro_type_name) click to toggle source
# File lib/avro/builder/type_factory.rb, line 63
def builtin_type?(avro_type_name)
  BUILTIN_TYPES.include?(avro_type_name.to_s)
end
create_and_configure_builtin_type(avro_type_name, field: nil, cache: nil, internal: {}, options: {}, validate_type: true, &block) click to toggle source

Return a new Type instance, including propagating internal state and setting attributes via the DSL

# File lib/avro/builder/type_factory.rb, line 48
def create_and_configure_builtin_type(avro_type_name,
                                      field: nil,
                                      cache: nil,
                                      internal: {},
                                      options: {},
                                      validate_type: true,
                                      &block)
  create_builtin_type(avro_type_name, field: field, cache: cache).tap do |type|
    type.configure_options(internal.merge(options))
    type.cache!
    type.instance_eval(&block) if block_given?
    type.validate! if validate_type
  end
end
create_builtin_type(avro_type_name, field:, cache:) click to toggle source

Return a new Type instance

# File lib/avro/builder/type_factory.rb, line 31
def create_builtin_type(avro_type_name, field:, cache:)
  name = avro_type_name.to_s.downcase
  if name == 'bytes'
    Avro::Builder::Types::BytesType.new(field: field, cache: cache)
  elsif Avro::Schema::PRIMITIVE_TYPES.include?(name)
    Avro::Builder::Types::Type.new(name, field: field, cache: cache)
  elsif field.nil? && NAMED_TYPES.include?(name)
    Avro::Builder.const_get(name.capitalize).new(cache: cache)
  elsif COMPLEX_TYPES.include?(name)
    Avro::Builder::Types.const_get("#{name.capitalize}Type").new(field: field, cache: cache)
  else
    raise "Invalid builtin type: #{avro_type_name}"
  end
end
type_lookup(avro_type_or_name, namespace = nil) { |avro_type_or_name| ... } click to toggle source

Determine if avro_type_or_name is an existing Type, the name of a builtin type or a previously defined named type. If avro_type_or_name is the name of a builtin type, then that type name is yielded to build the type using a block provided by the caller using, for example, create_builtin_type or create_and_configure_builtin_type.

# File lib/avro/builder/type_factory.rb, line 20
def type_lookup(avro_type_or_name, namespace = nil)
  if avro_type_or_name.is_a?(Avro::Builder::Types::Type)
    avro_type_or_name
  elsif builtin_type?(avro_type_or_name)
    yield(avro_type_or_name)
  else
    cache.lookup_named_type(avro_type_or_name, namespace)
  end
end