class Avro::Builder::Field

This class represents a field in a record. A field must be initialized with a type.

Constants

INTERNAL_ATTRIBUTES

Attributes

cache[RW]
field_type[RW]
optional_field[RW]
record[RW]

Public Class Methods

new(name:, avro_type_or_name:, record:, cache:, internal: {}, options: {}, &block) click to toggle source
# File lib/avro/builder/field.rb, line 22
def initialize(name:, avro_type_or_name:, record:, cache:, internal: {}, options: {}, &block)
  @cache = cache
  @record = record
  @name = name.to_s

  internal.each do |key, value|
    send("#{key}=", value) if INTERNAL_ATTRIBUTES.include?(key)
  end

  type_options = options.dup
  options.keys.each do |key|
    send(key, type_options.delete(key)) if dsl_attribute?(key)
  end

  # Find existing Type or build a new instance of a builtin Type using
  # the supplied block
  @field_type = type_lookup(avro_type_or_name, namespace) do |avro_type_name|
    create_and_configure_builtin_type(avro_type_name,
                                      field: self,
                                      cache: cache,
                                      internal: internal,
                                      validate_type: false,
                                      options: type_options)
  end

  # DSL calls must be evaluated after the type has been constructed
  instance_eval(&block) if block_given?
  @field_type.validate!
end

Public Instance Methods

method_missing(id, *args, &block) click to toggle source
Calls superclass method
# File lib/avro/builder/field.rb, line 58
def method_missing(id, *args, &block)
  field_type.dsl_respond_to?(id) ? field_type.send(id, *args, &block) : super
end
name(value = nil) click to toggle source

Delegate setting name explicitly via DSL to type

# File lib/avro/builder/field.rb, line 77
def name(value = nil)
  if value
    field_type.name(value)
  else
    # Return the name of the field
    @name
  end
end
name_fragment() click to toggle source
# File lib/avro/builder/field.rb, line 62
def name_fragment
  record.name_fragment
end
namespace(value = nil) click to toggle source

Delegate setting namespace explicitly via DSL to type and return the namespace value from the enclosing record.

# File lib/avro/builder/field.rb, line 68
def namespace(value = nil)
  if value
    field_type.namespace(value)
  else
    record.namespace
  end
end
respond_to_missing?(id, _include_all) click to toggle source

Delegate additional DSL calls to the type

Calls superclass method
# File lib/avro/builder/field.rb, line 54
def respond_to_missing?(id, _include_all)
  field_type.dsl_respond_to?(id) || super
end
serialize(reference_state) click to toggle source
# File lib/avro/builder/field.rb, line 86
def serialize(reference_state)
  # TODO: order is not included here
  {
    name: name,
    type: serialized_type(reference_state),
    doc: doc,
    default: default,
    aliases: aliases
  }.reject { |_, v| v.nil? }.tap do |result|
    result.merge!(default: nil) if optional_field
  end
end

Private Instance Methods

serialized_type(reference_state) click to toggle source

Optional fields must be serialized as a union – an array of types.

# File lib/avro/builder/field.rb, line 104
def serialized_type(reference_state)
  result = field_type.serialize(reference_state)
  optional_field ? field_type.class.union_with_null(result) : result
end