Class: Avro::Builder::Field

Inherits:
Object
  • Object
show all
Includes:
Aliasable, AnonymousTypes, DslAttributes, DslOptions, Metadata
Defined in:
lib/avro/builder/field.rb

Overview

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

Constant Summary

Constants included from TypeFactory

TypeFactory::BUILTIN_TYPES, TypeFactory::COMPLEX_TYPES, TypeFactory::NAMED_TYPES

Constants included from DslAttributes

DslAttributes::ATTRIBUTE_NAMES

Instance Method Summary collapse

Methods included from Metadata

attribute?, register, reset!

Methods included from AnonymousTypes

#array, #map, #type, #union

Methods included from Aliasable

included

Methods included from DslAttributes

#dsl_attribute?, included

Methods included from DslOptions

#dsl_option?, included

Constructor Details

#initialize(name:, avro_type_or_name:, record:, cache:, optional:, options: {}, &block) ⇒ Field

Returns a new instance of Field.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/avro/builder/field.rb', line 22

def initialize(name:, avro_type_or_name:, record:, cache:, optional:, options: {}, &block)
  @cache = cache
  @record = record
  @name = name.to_s
  @optional_field = optional

  type_options = options.dup
  options.keys.each do |key|
    send(key, type_options.delete(key)) if dsl_attribute?(key) || (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|
    type_internal = NAMED_TYPES.include?(avro_type_name.to_s) ? { type_namespace: namespace } : {}
    create_and_configure_builtin_type(avro_type_name,
                                      field: self,
                                      cache: cache,
                                      internal: type_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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/avro/builder/field.rb', line 56

def method_missing(id, *args, &block)
  # We can't just rely on Metadata#method_missing because we want to give precedence to defining
  # metadata on the field rather than the type
  if (id)
    (id, args)
  elsif field_type.dsl_respond_to?(id)
    field_type.send(id, *args, &block)
  else
    super
  end
end

Instance Method Details

#name(value = nil) ⇒ Object

Delegate setting name explicitly via DSL to type



83
84
85
86
87
88
89
90
# File 'lib/avro/builder/field.rb', line 83

def name(value = nil)
  if value
    field_type.name(value)
  else
    # Return the name of the field
    @name
  end
end

#name_fragment ⇒ Object



68
69
70
# File 'lib/avro/builder/field.rb', line 68

def name_fragment
  record.name_fragment
end

#namespace(value = nil) ⇒ Object

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



74
75
76
77
78
79
80
# File 'lib/avro/builder/field.rb', line 74

def namespace(value = nil)
  if value
    field_type.namespace(value)
  else
    record.namespace
  end
end

#respond_to_missing?(id, _include_all) ⇒ Boolean

Delegate additional DSL calls to the type

Returns:

  • (Boolean)


52
53
54
# File 'lib/avro/builder/field.rb', line 52

def respond_to_missing?(id, _include_all)
  field_type.dsl_respond_to?(id) || super
end

#serialize(reference_state) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/avro/builder/field.rb', line 92

def serialize(reference_state)
  {
    name: name,
    type: serialized_type(reference_state),
    doc: doc,
    default: default,
    aliases: aliases,
    order: order
  }.merge().compact.tap do |result|
    result[:default] = nil if optional_field
  end
end