Sha256: 9d50c55ca4420d7bc6b60379c5fc509b5c213f6041b0aa6d1b68bc7d93e5add0

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Avro
  module Builder

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

      COMPLEX_TYPES = Set.new(%w(array enum fixed map record union).map(&:freeze)).freeze
      BUILTIN_TYPES = Avro::Schema::PRIMITIVE_TYPES.union(COMPLEX_TYPES).freeze

      private

      # Return a new Type instance
      def create_builtin_type(type_name)
        name = type_name.to_s.downcase
        case
        when Avro::Schema::PRIMITIVE_TYPES.include?(name)
          Avro::Builder::Types::Type.new(name)
        when COMPLEX_TYPES.include?(name)
          Avro::Builder::Types.const_get("#{name.capitalize}Type").new
        else
          raise "Invalid builtin type: #{type_name}"
        end
      end

      # Return a new Type instance, including propagating internal state
      # and setting attributes via the DSL
      def create_and_configure_builtin_type(type_name,
                                            field: nil,
                                            builder: nil,
                                            internal: {},
                                            options: {},
                                            &block)
        create_builtin_type(type_name).tap do |type|
          type.field = field
          type.builder = builder
          type.configure_options(internal.merge(options))
          type.instance_eval(&block) if block_given?
        end
      end

      def builtin_type?(type_name)
        BUILTIN_TYPES.include?(type_name.to_s)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
avro-builder-0.4.0 lib/avro/builder/type_factory.rb
avro-builder-0.3.2 lib/avro/builder/type_factory.rb