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

Inherits:
Type
  • Object
show all
Includes:
Aliasable, Namespaceable, ComplexType, NamedErrorHandling
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

#avro_type_name

Instance Method Summary collapse

Methods included from NamedErrorHandling

#type_name, #type_namespace

Methods included from Aliasable

included

Methods included from Namespaceable

#fullname

Methods included from ComplexType

included, #initialize

Methods inherited from Type

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

Methods included from DslAttributes

#dsl_attribute?, included

Methods included from DslOptions

#dsl_option?, included

Instance Method Details

#cache!Object


44
45
46
# File 'lib/avro/builder/types/named_type.rb', line 44

def cache!
  cache.add_schema_object(self)
end

#name(value = nil) ⇒ Object


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

def name(value = nil)
  if value.nil?
    @name || "__#{name_fragment}_#{avro_type_name}"
  else
    type_name_instead_of_name_error!
  end
end

#name_fragmentObject

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


50
51
52
53
# File 'lib/avro/builder/types/named_type.rb', line 50

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

#namespace(value = nil) ⇒ Object


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

def namespace(value = nil)
  if value.nil?
    @namespace
  else
    type_namespace_instead_of_namespace_error!
  end
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.


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

def serialize(reference_state, overrides: {})
  reference_state.definition_or_reference(fullname) do
    serialized_attribute_hash.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.


67
68
69
70
71
72
# File 'lib/avro/builder/types/named_type.rb', line 67

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

#validate!Object


40
41
42
# File 'lib/avro/builder/types/named_type.rb', line 40

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