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

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

#configure_options

Methods included from Namespaceable

#fullname

Methods included from ComplexType

included, #initialize

Methods inherited from Type

#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



46
47
48
# File 'lib/avro/builder/types/named_type.rb', line 46

def cache!
  cache.add_schema_object(self)
end

#name(value = nil) ⇒ Object



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

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.



52
53
54
55
# File 'lib/avro/builder/types/named_type.rb', line 52

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

#namespace(value = nil) ⇒ Object



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

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.



60
61
62
63
64
65
66
67
68
# File 'lib/avro/builder/types/named_type.rb', line 60

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



73
74
75
76
77
78
79
80
# File 'lib/avro/builder/types/named_type.rb', line 73

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

#validate!Object



42
43
44
# File 'lib/avro/builder/types/named_type.rb', line 42

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