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

Instance Attribute Summary collapse

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:, type_name:, record:, cache:, internal: {}, 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
49
# File 'lib/avro/builder/field.rb', line 22

def initialize(name:, 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?(type_name)
            create_and_configure_builtin_type(type_name,
                                              field: self,
                                              cache: cache,
                                              internal: internal,
                                              options: type_options)
          else
            cache.lookup_named_type(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



57
58
59
# File 'lib/avro/builder/field.rb', line 57

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

Instance Attribute Details

#name(value = nil) ⇒ Object (readonly)

Delegate setting name explicitly via DSL to type



78
79
80
# File 'lib/avro/builder/field.rb', line 78

def name
  @name
end

Instance Method Details

#name_fragmentObject



61
62
63
# File 'lib/avro/builder/field.rb', line 61

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.



67
68
69
70
71
72
73
74
75
# File 'lib/avro/builder/field.rb', line 67

def namespace(value = nil)
  if value
    raise UnsupportedBlockAttributeError.new(attribute: :namespace,
                                             field: @name,
                                             type: type.type_name)
  else
    record.namespace
  end
end

#respond_to_missing?(id, _include_all) ⇒ Boolean

Delegate additional DSL calls to the type

Returns:

  • (Boolean)


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

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

#serialize(reference_state) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/avro/builder/field.rb', line 89

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