Class: Avro::Builder::DefinitionCache

Inherits:
Object
  • Object
show all
Defined in:
lib/avro/builder/definition_cache.rb

Overview

This class is used to cache previously defined schema objects preventing direct access via the DSL.

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ DefinitionCache


7
8
9
10
11
# File 'lib/avro/builder/definition_cache.rb', line 7

def initialize(builder)
  @builder = builder
  @schema_objects = {}
  @schema_names = Set.new
end

Instance Method Details

#add_schema_object(object) ⇒ Object

Cache and schema object by name (for convenience) and fullname. The schema object is only available by name if the unqualified name is unique.


15
16
17
18
# File 'lib/avro/builder/definition_cache.rb', line 15

def add_schema_object(object)
  store_by_name(object) if object.namespace
  store_by_fullname(object)
end

#add_type_by_name(type_object, name, namespace = nil) ⇒ Object

Add a type object directly with the specified name. The type_object may not have a name or namespace.


37
38
39
40
41
42
43
# File 'lib/avro/builder/definition_cache.rb', line 37

def add_type_by_name(type_object, name, namespace = nil)
  fullname = Avro::Name.make_fullname(name.to_s, namespace && namespace.to_s)
  name = fullname.split('.').last

  store_by_name(type_object, name)
  store_by_fullname(type_object, fullname) if name != fullname
end

#lookup_named_type(key, namespace = nil) ⇒ Object

Lookup an Avro schema object by name, possibly fully qualified by namespace.


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/avro/builder/definition_cache.rb', line 21

def lookup_named_type(key, namespace = nil)
  key_str = Avro::Name.make_fullname(key.to_s, namespace && namespace.to_s)
  object = schema_objects[key_str]

  if object.nil? && !schema_names.include?(key.to_s)
    object = builder.import(key)
  end

  raise DefinitionNotFoundError.new(key) if object.nil? && namespace.nil?

  # Return object or retry without namespace
  object || lookup_named_type(key, nil)
end