Class: Avro::Builder::Types::Type

Inherits:
Object
  • Object
show all
Includes:
DslAttributes, DslOptions
Defined in:
lib/avro/builder/types/type.rb

Overview

Base class for simple types. The type name is specified when the type is constructed. The type has no additional attributes, and the type is serialized as just the type name.

Direct Known Subclasses

ArrayType, MapType, NamedType, UnionType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DslAttributes

#dsl_attribute?, included

Methods included from DslOptions

#dsl_option?, included

Constructor Details

#initialize(avro_type_name, cache:, field: nil) ⇒ Type


15
16
17
18
19
# File 'lib/avro/builder/types/type.rb', line 15

def initialize(avro_type_name, cache:, field: nil)
  @avro_type_name = avro_type_name
  @cache = cache
  @field = field
end

Instance Attribute Details

#avro_type_nameObject (readonly)

Returns the value of attribute avro_type_name


13
14
15
# File 'lib/avro/builder/types/type.rb', line 13

def avro_type_name
  @avro_type_name
end

Class Method Details

.union_with_null(serialized) ⇒ Object

Optional fields are represented as a union of the type with :null.


49
50
51
# File 'lib/avro/builder/types/type.rb', line 49

def self.union_with_null(serialized)
  [:null, serialized]
end

Instance Method Details

#abstract?Boolean


21
22
23
# File 'lib/avro/builder/types/type.rb', line 21

def abstract?
  !!abstract
end

#cache!Object

Subclasses should override this method if the type definition should be cached for reuse.


60
61
# File 'lib/avro/builder/types/type.rb', line 60

def cache!
end

#configure_options(options = {}) ⇒ Object


42
43
44
45
46
# File 'lib/avro/builder/types/type.rb', line 42

def configure_options(options = {})
  options.each do |key, value|
    send("#{key}=", value) if dsl_option?(key)
  end
end

#dsl_method?(_name) ⇒ Boolean

Subclasses can override this method to indicate that the name is a method that the type exposes in the DSL. These methods are in addition to the methods for setting attributes on a type.


66
67
68
# File 'lib/avro/builder/types/type.rb', line 66

def dsl_method?(_name)
  false
end

#dsl_respond_to?(name) ⇒ Boolean


70
71
72
# File 'lib/avro/builder/types/type.rb', line 70

def dsl_respond_to?(name)
  dsl_attribute?(name) || dsl_method?(name)
end

#namespaceObject


38
39
40
# File 'lib/avro/builder/types/type.rb', line 38

def namespace
  nil
end

#serialize(_reference_state) ⇒ Object


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

def serialize(_reference_state)
  if logical_type
    { type: avro_type_name, logicalType: logical_type }
  else
    avro_type_name
  end
end

#to_h(_reference_state) ⇒ Object


33
34
35
36
# File 'lib/avro/builder/types/type.rb', line 33

def to_h(_reference_state)
  { type: avro_type_name, logicalType: logical_type }
    .reject { |_, v| v.nil? }
end

#validate!Object

Subclasses should override this method to check for the presence of required DSL attributes.


55
56
# File 'lib/avro/builder/types/type.rb', line 55

def validate!
end