Class: Avro::Builder::Types::NamedType

Inherits:
Type
  • Object
show all
Includes:
Aliasable, Namespaceable, ComplexType, ConfigurableType
Defined in:
lib/avro/builder/types/named_type.rb

Overview

This is an abstract class that represents a type that can be defined with a name, outside a record.

Direct Known Subclasses

EnumType, FixedType, RecordType

Instance Attribute Summary

Attributes inherited from Type

#type_name

Instance Method Summary collapse

Methods included from Aliasable

included

Methods included from ConfigurableType

#configure_options

Methods included from Namespaceable

#fullname

Methods included from ComplexType

included, #initialize, #namespace

Methods inherited from Type

#configure_options, #dsl_method?, #dsl_respond_to?, #initialize, #namespace, union_with_null

Methods included from DslAttributes

#dsl_attribute?, included

Methods included from DslOptions

#dsl_option?, included

Instance Method Details

#cache!Object



28
29
30
# File 'lib/avro/builder/types/named_type.rb', line 28

def cache!
  cache.add_schema_object(self)
end

#name_fragmentObject

Named types that do not have an explicit name are assigned a named based on the field and its nesting.



34
35
36
37
# File 'lib/avro/builder/types/named_type.rb', line 34

def name_fragment
  [field && field.name_fragment,
   @name || (field && field.name)].compact.join('_')
end

#serialize(reference_state, overrides: {}) ⇒ Object

As a type for a field Subclasses may call super with additional overrides to be added to the serialized value.



42
43
44
45
46
47
48
49
50
# File 'lib/avro/builder/types/named_type.rb', line 42

def serialize(reference_state, overrides: {})
  reference_state.definition_or_reference(fullname) do
    {
      name: name,
      type: type_name,
      namespace: namespace
    }.merge(overrides).reject { |_, v| v.nil? }
  end
end

#to_h(_reference_state, overrides: {}) ⇒ Object

As a top-level, named type Subclasses may call super with additional overrides to be added to the hash representation.



55
56
57
58
59
60
61
62
# File 'lib/avro/builder/types/named_type.rb', line 55

def to_h(_reference_state, overrides: {})
  {
    name: name,
    type: type_name,
    namespace: namespace,
    aliases: aliases
  }.merge(overrides).reject { |_, v| v.nil? }
end

#validate!Object



24
25
26
# File 'lib/avro/builder/types/named_type.rb', line 24

def validate!
  required_attribute_error!(:name) if field.nil? && @name.nil?
end