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

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

Constant Summary

Constants included from DslAttributes

DslAttributes::ATTRIBUTE_NAMES

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 inherited from ComplexType

avro_type_name, #initialize

Methods inherited from Type

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

Methods included from Metadata

attribute?, #method_missing, register, reset!, #respond_to_missing?

Methods included from DslAttributes

#dsl_attribute?, included

Methods included from DslOptions

#dsl_option?, included

Constructor Details

This class inherits a constructor from Avro::Builder::Types::ComplexType

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Avro::Builder::Metadata

Instance Method Details

#cache! ⇒ Object



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

def cache!
  cache.add_schema_object(self)
end

#name(value = nil) ⇒ Object



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

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

#name_fragment ⇒ Object

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



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

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

#namespace(value = nil) ⇒ Object



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

def namespace(value = nil)
  if value.nil?
    @namespace
  else
    type_namespace_instead_of_namespace_error!
  end
end

#serialize(reference_state) ⇒ Object

As a type for a field



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

def serialize(reference_state)
  # Return the full type definition if it hasn't been seen yet.
  # Otherwise, just return its name.
  reference_state.definition_or_reference(fullname) do
    super
  end
end

#to_h(reference_state) ⇒ Object

As a top-level, named type



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

def to_h(reference_state)
  # Always return the type definition. It's an error if the type has already been seen.
  reference_state.definition(fullname) do
    super
  end
end

#validate! ⇒ Object



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

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