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 =
%i(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) ⇒ Object
Adds fields from the record with the specified name to the current record.
-
#initialize(name = nil, options: {}, cache:, field: nil, &block) ⇒ RecordType
constructor
A new instance of RecordType.
-
#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
#cache!, #configure_options, #dsl_respond_to?, #namespace, union_with_null, #validate!
Methods included from DslAttributes
Methods included from DslOptions
Constructor Details
#initialize(name = nil, options: {}, cache:, field: nil, &block) ⇒ RecordType
Returns a new instance of RecordType.
14 15 16 17 18 19 20 21 22 |
# File 'lib/avro/builder/types/record_type.rb', line 14 def initialize(name = nil, options: {}, cache:, field: nil, &block) @avro_type_name = :record @name = name @cache = cache @field = field () instance_eval(&block) if block_given? end |
Instance Method Details
#dsl_method?(name) ⇒ Boolean
24 25 26 |
# File 'lib/avro/builder/types/record_type.rb', line 24 def dsl_method?(name) DSL_METHODS.include?(name) end |
#extends(name) ⇒ Object
Adds fields from the record with the specified name to the current record.
56 57 58 |
# File 'lib/avro/builder/types/record_type.rb', line 56 def extends(name) fields.merge!(cache.lookup_named_type(name, 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.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/avro/builder/types/record_type.rb', line 42 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
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/avro/builder/types/record_type.rb', line 29 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
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/avro/builder/types/record_type.rb', line 60 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 |