Sha256: 738e1ad957f22cdc209eef40999fa0b70324a66f081165db3367a477be926d49

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'avro/builder/types/configurable_type'
require 'avro/builder/namespaceable'

module Avro
  module Builder
    module Types

      # This is an abstract class that represents a type that can be defined
      # with a name, outside a record.
      class NamedType < Type
        include Avro::Builder::Types::ComplexType
        include Avro::Builder::Namespaceable
        include Avro::Builder::Types::ConfigurableType

        dsl_attributes :namespace, :aliases

        dsl_attribute :name do |value = nil|
          if value
            @name = value
          else
            @name || "__#{name_fragment}_#{type_name}"
          end
        end

        # Named types that do not have an explicit name are assigned
        # a named based on the field and its nesting.
        def name_fragment
          [field && field.name_fragment,
           @name || (field && field.name)].compact.join('_')
        end

        # As a type for a field
        # Subclasses may call super with additional overrides to be added
        # to the serialized value.
        def serialize(reference_state, overrides: {})
          reference_state.definition_or_reference(fullname) do
            {
              name: name,
              type: type_name,
              namespace: namespace
            }.merge(overrides).reject { |_, v| v.nil? }
          end
        end

        # As a top-level, named type
        # Subclasses may call super with additional overrides to be added
        # to the hash representation.
        def to_h(_reference_state, overrides: {})
          {
            name: name,
            type: type_name,
            namespace: namespace,
            aliases: aliases
          }.merge(overrides).reject { |_, v| v.nil? }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
avro-builder-0.3.2 lib/avro/builder/types/named_type.rb