Sha256: 7842ae9bd1fa4cbdae8838b59260bcbc7bdbb88589b8c39344ab9fed94b15e3e

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module Avro
  module Builder
    module Types
      # Base class for simple types. The type name is specified when the
      # type is constructed. The type has no additional attributes, and
      # the type is serialized as just the type name.
      class Type
        include Avro::Builder::DslAttributes

        attr_reader :type_name

        def initialize(type_name, cache:, field: nil)
          @type_name = type_name
          @cache = cache
          @field = field
        end

        def serialize(_reference_state)
          type_name
        end

        def namespace
          nil
        end

        def configure_options(_options = {})
          # No-op
        end

        # Optional fields are represented as a union of the type with :null.
        def self.union_with_null(serialized)
          [:null, serialized]
        end

        # Subclasses should override this method to check for the presence
        # of required DSL attributes.
        def validate!
        end

        # Subclasses should override this method if the type definition should
        # be cached for reuse.
        def cache!
        end

        private

        def required_attribute_error!(attribute_name)
          raise RequiredAttributeError.new(type: type_name,
                                           attribute: attribute_name,
                                           field: field && field.name,
                                           name: @name)
        end

        def validate_required_attribute!(attribute_name)
          value = public_send(attribute_name)
          if value.nil? || value.respond_to?(:empty?) && value.empty?
            required_attribute_error!(attribute_name)
          end
        end

        private

        attr_accessor :field, :cache
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
avro-builder-0.5.0 lib/avro/builder/types/type.rb