Class: Avro::Builder::Types::RecordType
- Includes:
- AnonymousTypes
- Defined in:
- lib/avro/builder/types/record_type.rb
Overview
This class represents a record in an Avro schema. Records may be defined at the top-level or as the type for a field in a record.
Direct Known Subclasses
Constant Summary collapse
- DSL_METHODS =
[:required, :optional, :extends].to_set.freeze
Constants included from Avro::Builder::TypeFactory
Avro::Builder::TypeFactory::BUILTIN_TYPES, Avro::Builder::TypeFactory::COMPLEX_TYPES, Avro::Builder::TypeFactory::NAMED_TYPES
Instance Attribute Summary
Attributes inherited from Type
Instance Method Summary collapse
- #dsl_method?(name) ⇒ Boolean
-
#extends(name, options = {}) ⇒ Object
Adds fields from the record with the specified name to the current record.
-
#initialize(name = nil, cache:, options: {}, field: nil, &block) ⇒ RecordType
constructor
rubocop:disable Lint/MissingSuper.
-
#optional(name, avro_type_or_name, options = {}, &block) ⇒ Object
Add an optional field to the record.
-
#required(name, avro_type_or_name, options = {}, &block) ⇒ Object
Add a required field to the record.
- #to_h(reference_state = SchemaSerializerReferenceState.new) ⇒ Object (also: #serialize)
Methods included from AnonymousTypes
Methods inherited from NamedType
#cache!, #name, #name_fragment, #namespace, #validate!
Methods included from NamedErrorHandling
Methods included from Aliasable
Methods included from Namespaceable
Methods included from ComplexType
Methods inherited from Type
#abstract?, #cache!, #configure_options, #dsl_respond_to?, #namespace, union_with_null, #validate!
Methods included from DslAttributes
Methods included from DslOptions
Constructor Details
#initialize(name = nil, cache:, options: {}, field: nil, &block) ⇒ RecordType
rubocop:disable Lint/MissingSuper
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/avro/builder/types/record_type.rb', line 16 def initialize(name = nil, cache:, options: {}, field: nil, &block) # rubocop:disable Lint/MissingSuper # TODO: Fix missing call to super @avro_type_name = :record @name = name @cache = cache @field = field () instance_eval(&block) if block_given? end |
Instance Method Details
#dsl_method?(name) ⇒ Boolean
27 28 29 |
# File 'lib/avro/builder/types/record_type.rb', line 27 def dsl_method?(name) DSL_METHODS.include?(name) end |
#extends(name, options = {}) ⇒ Object
Adds fields from the record with the specified name to the current record.
59 60 61 |
# File 'lib/avro/builder/types/record_type.rb', line 59 def extends(name, = {}) fields.merge!(cache.lookup_named_type(name, .delete(:namespace) || namespace).duplicated_fields) end |
#optional(name, avro_type_or_name, options = {}, &block) ⇒ Object
Add an optional field to the record. In Avro this is represented as a union of null and the type specified here.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/avro/builder/types/record_type.rb', line 45 def optional(name, avro_type_or_name, = {}, &block) new_field = Avro::Builder::Field.new(name: name, avro_type_or_name: avro_type_or_name, record: self, cache: cache, internal: { type_namespace: namespace, optional_field: true }, options: , &block) add_field(new_field) end |
#required(name, avro_type_or_name, options = {}, &block) ⇒ Object
Add a required field to the record
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/avro/builder/types/record_type.rb', line 32 def required(name, avro_type_or_name, = {}, &block) new_field = Avro::Builder::Field.new(name: name, avro_type_or_name: avro_type_or_name, record: self, cache: cache, internal: { type_namespace: namespace }, options: , &block) add_field(new_field) end |
#to_h(reference_state = SchemaSerializerReferenceState.new) ⇒ Object Also known as: serialize
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/avro/builder/types/record_type.rb', line 63 def to_h(reference_state = SchemaSerializerReferenceState.new) reference_state.definition_or_reference(fullname) do { type: :record, name: name, namespace: namespace, doc: doc, aliases: aliases, logicalType: logical_type, fields: fields.values.map { |field| field.serialize(reference_state) } }.reject { |_, v| v.nil? } end end |