Class: Avro::Builder::Field

Inherits:
Object
  • Object
show all
Includes:
Aliasable, DslAttributes, DslOptions, TypeFactory
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 collapse

INTERNAL_ATTRIBUTES =
%i(optional_field).to_set.freeze

Constants included from TypeFactory

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

Instance Method Summary collapse

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_name:, record:, cache:, internal: {}, options: {}, &block) ⇒ Field

Returns a new instance of Field.



20
21
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
# File 'lib/avro/builder/field.rb', line 20

def initialize(name:, avro_type_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

  @type = if builtin_type?(avro_type_name)
            create_and_configure_builtin_type(avro_type_name,
                                              field: self,
                                              cache: cache,
                                              internal: internal,
                                              options: type_options)
          else
            cache.lookup_named_type(avro_type_name, namespace)
          end

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



55
56
57
# File 'lib/avro/builder/field.rb', line 55

def method_missing(id, *args, &block)
  type.dsl_respond_to?(id) ? type.send(id, *args, &block) : super
end

Instance Method Details

#name(value = nil) ⇒ Object

Delegate setting name explicitly via DSL to type



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

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

#name_fragmentObject



59
60
61
# File 'lib/avro/builder/field.rb', line 59

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.



65
66
67
68
69
70
71
# File 'lib/avro/builder/field.rb', line 65

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

#respond_to_missing?(id, _include_all) ⇒ Boolean

Delegate additional DSL calls to the type

Returns:

  • (Boolean)


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

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

#serialize(reference_state) ⇒ Object



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

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