Class: Lutaml::Model::TypeRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/type_registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

TypeRegistry is a pure data store for type mappings.

This is an INTERNAL class. Users should use Register and GlobalRegister.

Responsibility: Store and retrieve type mappings (Symbol => Class)

This class has NO knowledge of:

  • Type resolution logic (see TypeResolver)

  • Type substitution (see TypeSubstitution)

  • Fallback chains (see TypeContext)

  • Caching (see CachedTypeResolver)

Examples:

Basic usage

registry = TypeRegistry.new
registry.register(:string, Lutaml::Model::Type::String)
registry.lookup(:string) #=> Lutaml::Model::Type::String
registry.registered?(:string) #=> true

Iterating over registered types

registry.names #=> [:string, :integer, :boolean]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypeRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new empty TypeRegistry



32
33
34
# File 'lib/lutaml/model/type_registry.rb', line 32

def initialize
  @types = {}
end

Instance Attribute Details

#typesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
# File 'lib/lutaml/model/type_registry.rb', line 29

def types
  @types
end

Instance Method Details

#clearHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clear all registered types

This is primarily useful for testing to ensure isolation.

Examples:

registry.clear #=> {}

Returns:

  • (Hash)

    Empty hash



105
106
107
# File 'lib/lutaml/model/type_registry.rb', line 105

def clear
  @types.clear
end

#dupTypeRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a copy of this registry

Returns:



137
138
139
140
141
142
143
# File 'lib/lutaml/model/type_registry.rb', line 137

def dup
  new_registry = self.class.new
  @types.each do |name, klass|
    new_registry.register(name, klass)
  end
  new_registry
end

#each {|Symbol, Class| ... } ⇒ Enumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterate over all registered types

Examples:

registry.each { |name, klass| puts "#{name}: #{klass}" }

Yields:

  • (Symbol, Class)

    Yields name and class for each registered type

Returns:

  • (Enumerator)

    If no block given



130
131
132
# File 'lib/lutaml/model/type_registry.rb', line 130

def each(&)
  @types.each(&)
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the registry is empty

Returns:

  • (Boolean)

    true if no types are registered



112
113
114
# File 'lib/lutaml/model/type_registry.rb', line 112

def empty?
  @types.empty?
end

#lookup(name) ⇒ Class?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Look up a type class by name. If the type was registered as a Proc (lazy registration), the Proc is called and the result is cached.

Examples:

registry.lookup(:string) #=> Lutaml::Model::Type::String
registry.lookup(:unknown) #=> nil

Parameters:

  • name (Symbol, String)

    The name of the type to look up

Returns:

  • (Class, nil)

    The type class, or nil if not found



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lutaml/model/type_registry.rb', line 62

def lookup(name)
  key = name.to_sym
  value = @types[key]

  if value.is_a?(Proc)
    resolved = value.call
    @types[key] = resolved
    resolved
  else
    value
  end
end

#merge(other) ⇒ TypeRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new registry by merging this one with another

Parameters:

Returns:



160
161
162
# File 'lib/lutaml/model/type_registry.rb', line 160

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ TypeRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Merge another registry into this one

Parameters:

Returns:



149
150
151
152
153
154
# File 'lib/lutaml/model/type_registry.rb', line 149

def merge!(other)
  other.types.each do |name, klass|
    register(name, klass) unless registered?(name)
  end
  self
end

#namesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get all registered type names

Examples:

registry.names #=> [:string, :integer, :boolean]

Returns:

  • (Array<Symbol>)

    List of all registered type names



93
94
95
# File 'lib/lutaml/model/type_registry.rb', line 93

def names
  @types.keys
end

#register(name, klass) ⇒ Class, Proc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register a type class with a given name. Accepts a Class for eager registration or a Proc for lazy registration.

Examples:

Eager registration

registry.register(:custom_text, MyCustomText)

Lazy registration with Proc

autoload :Divergence, "mml/v3/vector_calculus"
registry.register(:divergence, -> { Mml::V3::Divergence })

Parameters:

  • name (Symbol, String)

    The name to register the type under

  • klass (Class, Proc)

    The type class, or a Proc returning it lazily

Returns:

  • (Class, Proc)

    The registered value



49
50
51
# File 'lib/lutaml/model/type_registry.rb', line 49

def register(name, klass)
  @types[name.to_sym] = klass
end

#registered?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if a type is registered

Examples:

registry.registered?(:string) #=> true
registry.registered?(:unknown) #=> false

Parameters:

  • name (Symbol, String)

    The name of the type to check

Returns:

  • (Boolean)

    true if the type is registered, false otherwise



83
84
85
# File 'lib/lutaml/model/type_registry.rb', line 83

def registered?(name)
  @types.key?(name.to_sym)
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the number of registered types

Returns:

  • (Integer)

    Number of registered types



119
120
121
# File 'lib/lutaml/model/type_registry.rb', line 119

def size
  @types.size
end