Class: Lutaml::Model::ContextRegistry Private
- Inherits:
-
Object
- Object
- Lutaml::Model::ContextRegistry
- 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
Instance Attribute Summary collapse
-
#contexts ⇒ Hash<Symbol, TypeContext>
readonly
private
The registered contexts.
Instance Method Summary collapse
-
#clear ⇒ void
private
Clear all contexts except default.
-
#context_ids ⇒ Array<Symbol>
private
Get all registered context IDs.
-
#create(id:, registry: nil, fallback_to: [], substitutions: []) ⇒ TypeContext
private
Create and register a new derived context.
-
#each {|Symbol, TypeContext| ... } ⇒ Enumerator
private
Iterate over all contexts.
-
#each_key {|Symbol| ... } ⇒ Enumerator
private
Iterate over all context IDs.
-
#empty? ⇒ Boolean
private
Check if registry is empty (excluding default).
-
#exists?(id) ⇒ Boolean
private
Check if a context exists.
-
#initialize ⇒ ContextRegistry
constructor
private
Create a new ContextRegistry.
-
#lookup(id) ⇒ TypeContext?
private
Look up a context by ID.
-
#register(context) ⇒ void
private
Register a context.
-
#register_substitution(context_id, from_type, to_type) ⇒ TypeContext?
private
Add a type substitution to an existing context.
-
#size ⇒ Integer
private
Get the number of registered contexts.
-
#unregister(id) ⇒ TypeContext?
private
Remove a context by ID.
Constructor Details
#initialize ⇒ ContextRegistry
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
#contexts ⇒ Hash<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.
31 32 33 |
# File 'lib/lutaml/model/context_registry.rb', line 31 def contexts @contexts end |
Instance Method Details
#clear ⇒ 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.
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_ids ⇒ Array<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.
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.
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.
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.
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).
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.
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.
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.
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.
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 |
#size ⇒ Integer
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.
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.
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 |