class Avro::Builder::Types::RecordType

This class represents a record in an Avro schema. Records may be defined at the top-level or as the type for a field in a record.

Constants

DSL_METHODS

Public Class Methods

new(name = nil, cache:, options: {}, field: nil, &block) click to toggle source
# File lib/avro/builder/types/record_type.rb, line 16
def initialize(name = nil, cache:, options: {}, field: nil, &block) # rubocop:disable Lint/MissingSuper
  # TODO: Fix missing call to super
  @avro_type_name = :record
  @name = name
  @cache = cache
  @field = field

  configure_options(options)
  instance_eval(&block) if block_given?
end

Public Instance Methods

dsl_method?(name) click to toggle source
# File lib/avro/builder/types/record_type.rb, line 27
def dsl_method?(name)
  DSL_METHODS.include?(name)
end
extends(name, options = {}) click to toggle source

Adds fields from the record with the specified name to the current record.

# File lib/avro/builder/types/record_type.rb, line 59
def extends(name, options = {})
  fields.merge!(cache.lookup_named_type(name, options.delete(:namespace) || namespace).duplicated_fields)
end
optional(name, avro_type_or_name, options = {}, &block) click to toggle source

Add an optional field to the record. In Avro this is represented as a union of null and the type specified here.

# File lib/avro/builder/types/record_type.rb, line 45
def optional(name, avro_type_or_name, options = {}, &block)
  new_field = Avro::Builder::Field.new(name: name,
                                       avro_type_or_name: avro_type_or_name,
                                       record: self,
                                       cache: cache,
                                       internal: { type_namespace: namespace,
                                                   optional_field: true },
                                       options: options,
                                       &block)
  add_field(new_field)
end
required(name, avro_type_or_name, options = {}, &block) click to toggle source

Add a required field to the record

# File lib/avro/builder/types/record_type.rb, line 32
def required(name, avro_type_or_name, options = {}, &block)
  new_field = Avro::Builder::Field.new(name: name,
                                       avro_type_or_name: avro_type_or_name,
                                       record: self,
                                       cache: cache,
                                       internal: { type_namespace: namespace },
                                       options: options,
                                       &block)
  add_field(new_field)
end
serialize(reference_state = SchemaSerializerReferenceState.new)
Alias for: to_h
to_h(reference_state = SchemaSerializerReferenceState.new) click to toggle source
# File lib/avro/builder/types/record_type.rb, line 63
def to_h(reference_state = SchemaSerializerReferenceState.new)
  reference_state.definition_or_reference(fullname) do
    {
      type: :record,
      name: name,
      namespace: namespace,
      doc: doc,
      aliases: aliases,
      logicalType: logical_type,
      fields: fields.values.map { |field| field.serialize(reference_state) }
    }.reject { |_, v| v.nil? }
  end
end
Also aliased as: serialize

Protected Instance Methods

duplicated_fields() click to toggle source
# File lib/avro/builder/types/record_type.rb, line 80
def duplicated_fields
  fields.each_with_object(Hash.new) do |(name, field), result|
    field_copy = field.dup
    result[name] = field_copy
  end
end

Private Instance Methods

add_field(field) click to toggle source

Add field, replacing any existing field with the same name.

# File lib/avro/builder/types/record_type.rb, line 90
def add_field(field)
  fields[field.name] = field
end
fields() click to toggle source
# File lib/avro/builder/types/record_type.rb, line 94
def fields
  @fields ||= {}
end