Class: Lutaml::Model::ContextRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/context_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.

ContextRegistry stores and retrieves named TypeContext instances.

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

Responsibility: Store and manage named TypeContext instances

This class:

  • Simple key-value store (Symbol => TypeContext)

  • NOT a singleton - can have multiple registries

  • Thread-safe with Mutex protection

  • Always contains :default context after initialization

Examples:

Basic usage

registry = ContextRegistry.new
registry.register(my_context)
registry.lookup(:my_context)  #=> my_context

Creating and registering a context

registry = ContextRegistry.new
registry.create(id: :my_app, fallback_to: [:default])
registry.lookup(:my_app)  #=> TypeContext with id :my_app

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContextRegistry

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 ContextRegistry. Automatically registers the :default context.



35
36
37
38
39
40
# File 'lib/lutaml/model/context_registry.rb', line 35

def initialize
  @contexts = {}
  @mutex = Mutex.new
  # Always register the default context
  register(TypeContext.default)
end

Instance Attribute Details

#contextsHash<Symbol, TypeContext> (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.

Returns The registered contexts.

Returns:



31
32
33
# File 'lib/lutaml/model/context_registry.rb', line 31

def contexts
  @contexts
end

Instance Method Details

#clearvoid

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.

This method returns an undefined value.

Clear all contexts except default.



165
166
167
168
169
170
# File 'lib/lutaml/model/context_registry.rb', line 165

def clear
  @mutex.synchronize do
    @contexts.clear
    @contexts[:default] = TypeContext.default
  end
end

#context_idsArray<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 context IDs.

Returns:

  • (Array<Symbol>)

    The context IDs



90
91
92
93
94
# File 'lib/lutaml/model/context_registry.rb', line 90

def context_ids
  @mutex.synchronize do
    @contexts.keys
  end
end

#create(id:, registry: nil, fallback_to: [], substitutions: []) ⇒ TypeContext

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 and register a new derived context.

Parameters:

  • id (Symbol)

    The context ID

  • registry (TypeRegistry) (defaults to: nil)

    Optional type registry (new one created if not provided)

  • fallback_to (Array<Symbol, TypeContext>) (defaults to: [])

    Fallback contexts

  • substitutions (Array<TypeSubstitution, Hash>) (defaults to: [])

    Type substitutions

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/lutaml/model/context_registry.rb', line 142

def create(id:, registry: nil, fallback_to: [], substitutions: [])
  # Resolve fallback symbols to contexts
  resolved_fallbacks = Array(fallback_to).filter_map do |ctx|
    resolve_fallback(ctx)
  end

  # Create new registry if not provided
  type_registry = registry || TypeRegistry.new

  context = TypeContext.derived(
    id: id,
    registry: type_registry,
    fallback_to: resolved_fallbacks,
    substitutions: substitutions,
  )

  register(context)
  context
end

#each {|Symbol, TypeContext| ... } ⇒ 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 contexts.

Yields:

Returns:

  • (Enumerator)

    If no block given



176
177
178
179
180
# File 'lib/lutaml/model/context_registry.rb', line 176

def each(&block)
  @mutex.synchronize do
    @contexts.each(&block)
  end
end

#each_key {|Symbol| ... } ⇒ 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 context IDs.

Yields:

  • (Symbol)

    Yields context id

Returns:

  • (Enumerator)

    If no block given



186
187
188
189
190
# File 'lib/lutaml/model/context_registry.rb', line 186

def each_key(&block)
  @mutex.synchronize do
    @contexts.each_key(&block)
  end
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 registry is empty (excluding default).

Returns:

  • (Boolean)

    true if only default context exists



108
109
110
111
112
# File 'lib/lutaml/model/context_registry.rb', line 108

def empty?
  @mutex.synchronize do
    @contexts.size == 1 && @contexts.key?(:default)
  end
end

#exists?(id) ⇒ 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 context exists.

Parameters:

  • id (Symbol, String)

    The context ID

Returns:

  • (Boolean)

    true if context exists



81
82
83
84
85
# File 'lib/lutaml/model/context_registry.rb', line 81

def exists?(id)
  @mutex.synchronize do
    @contexts.key?(id.to_sym)
  end
end

#lookup(id) ⇒ TypeContext?

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 context by ID.

Parameters:

  • id (Symbol, String)

    The context ID

Returns:

  • (TypeContext, nil)

    The context or nil if not found



61
62
63
64
65
# File 'lib/lutaml/model/context_registry.rb', line 61

def lookup(id)
  @mutex.synchronize do
    @contexts[id.to_sym]
  end
end

#register(context) ⇒ void

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.

This method returns an undefined value.

Register a context.

Parameters:

Raises:

  • (ArgumentError)

    If context is not a TypeContext



47
48
49
50
51
52
53
54
55
# File 'lib/lutaml/model/context_registry.rb', line 47

def register(context)
  unless context.is_a?(TypeContext)
    raise ArgumentError, "Expected TypeContext, got #{context.class}"
  end

  @mutex.synchronize do
    @contexts[context.id] = context
  end
end

#register_substitution(context_id, from_type, to_type) ⇒ TypeContext?

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.

Add a type substitution to an existing context.

Since TypeContext is immutable, this creates a new context with the substitution added and replaces the old one.

Parameters:

  • context_id (Symbol)

    The context ID

  • from_type (Class)

    Type to substitute from

  • to_type (Class)

    Type to substitute to

Returns:

  • (TypeContext, nil)

    The new context or nil if not found



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lutaml/model/context_registry.rb', line 123

def register_substitution(context_id, from_type, to_type)
  @mutex.synchronize do
    context = @contexts[context_id.to_sym]
    return nil unless context

    new_context = context.add_substitution(from_type: from_type,
                                           to_type: to_type)
    @contexts[context_id.to_sym] = new_context
    new_context
  end
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 contexts.

Returns:

  • (Integer)

    The number of contexts



99
100
101
102
103
# File 'lib/lutaml/model/context_registry.rb', line 99

def size
  @mutex.synchronize do
    @contexts.size
  end
end

#unregister(id) ⇒ TypeContext?

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.

Remove a context by ID.

Parameters:

  • id (Symbol, String)

    The context ID

Returns:

  • (TypeContext, nil)

    The removed context or nil if not found



71
72
73
74
75
# File 'lib/lutaml/model/context_registry.rb', line 71

def unregister(id)
  @mutex.synchronize do
    @contexts.delete(id.to_sym)
  end
end