Class: Avro::Builder::Field
- Inherits:
-
Object
- Object
- Avro::Builder::Field
- Includes:
- Aliasable, AnonymousTypes, DslAttributes, DslOptions
- 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 =
[:optional_field].to_set.freeze
Constants included from TypeFactory
TypeFactory::BUILTIN_TYPES, TypeFactory::COMPLEX_TYPES, TypeFactory::NAMED_TYPES
Instance Method Summary collapse
-
#initialize(name:, avro_type_or_name:, record:, cache:, internal: {}, options: {}, &block) ⇒ Field
constructor
A new instance of Field.
- #method_missing(id, *args, &block) ⇒ Object
-
#name(value = nil) ⇒ Object
Delegate setting name explicitly via DSL to type.
- #name_fragment ⇒ Object
-
#namespace(value = nil) ⇒ Object
Delegate setting namespace explicitly via DSL to type and return the namespace value from the enclosing record.
-
#respond_to_missing?(id, _include_all) ⇒ Boolean
Delegate additional DSL calls to the type.
- #serialize(reference_state) ⇒ Object
Methods included from AnonymousTypes
Methods included from Aliasable
Methods included from DslAttributes
Methods included from DslOptions
Constructor Details
#initialize(name:, avro_type_or_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 50 |
# 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 = .dup .keys.each do |key| send(key, .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: ) 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
58 59 60 |
# 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 |
Instance Method Details
#name(value = nil) ⇒ Object
Delegate setting name explicitly via DSL to type
77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
62 63 64 |
# File 'lib/avro/builder/field.rb', line 62 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.
68 69 70 71 72 73 74 |
# 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) ⇒ Boolean
Delegate additional DSL calls to the type
54 55 56 |
# 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) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# 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 |