Class: Lutaml::Model::TypeContext Private
- Inherits:
-
Object
- Object
- Lutaml::Model::TypeContext
- Defined in:
- lib/lutaml/model/type_context.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.
TypeContext bundles all information needed for type resolution.
This is an INTERNAL class. Users should use Register and GlobalRegister.
Responsibility: Encapsulate the resolution context (registry + substitutions + fallbacks)
This class:
-
Bundles all info needed for type resolution
-
Provides factory methods for common patterns
-
Used internally by Register and GlobalContext
-
Does NOT perform resolution (delegates to TypeResolver)
Instance Attribute Summary collapse
-
#fallback_contexts ⇒ Array<TypeContext>
readonly
private
Fallback contexts (in order).
-
#id ⇒ Symbol
readonly
private
The context identifier.
-
#registry ⇒ TypeRegistry
readonly
private
The primary type registry.
-
#substitutions ⇒ Array<TypeSubstitution>
readonly
private
Type substitution rules.
Class Method Summary collapse
-
.default ⇒ TypeContext
private
Factory: Create the default context with built-in types.
-
.derived(id:, registry:, fallback_to: [], substitutions: []) ⇒ TypeContext
private
Factory: Create a derived context with fallbacks.
-
.isolated(id, registry) ⇒ TypeContext
private
Factory: Create an isolated context with no fallbacks.
- .normalize_substitution(s) ⇒ Object private
-
.register_builtin_types_in(registry) ⇒ void
private
Register built-in types in a registry.
- .resolve_fallback_context(ctx) ⇒ Object private
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
private
Equality check.
-
#add_substitution(from_type:, to_type:) ⇒ TypeContext
private
Add a type substitution to this context.
-
#fallback_ids ⇒ Array<Symbol>
private
Get all fallback context IDs.
-
#has_fallbacks? ⇒ Boolean
private
Check if this context has any fallbacks.
-
#has_type?(name) ⇒ Boolean
private
Check if a type is directly in this context’s registry.
-
#hash ⇒ Integer
private
Hash code.
-
#initialize(id:, registry:, substitutions: [], fallback_contexts: []) ⇒ TypeContext
constructor
private
Create a new TypeContext.
-
#lookup_local(name) ⇒ Class?
private
Look up a type in this context’s registry only.
-
#substitution_for(from_type) ⇒ Array<TypeSubstitution>
private
Find substitutions for a given type.
-
#to_s ⇒ String
(also: #inspect)
private
Human-readable representation.
-
#with_fallbacks(fallback_to:) ⇒ TypeContext
private
Create a copy with different fallbacks.
Constructor Details
#initialize(id:, registry:, substitutions: [], fallback_contexts: []) ⇒ 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 a new TypeContext.
59 60 61 62 63 64 65 |
# File 'lib/lutaml/model/type_context.rb', line 59 def initialize(id:, registry:, substitutions: [], fallback_contexts: []) @id = id.to_sym @registry = registry @substitutions = Array(substitutions).freeze @fallback_contexts = Array(fallback_contexts).freeze freeze end |
Instance Attribute Details
#fallback_contexts ⇒ Array<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 Fallback contexts (in order).
51 52 53 |
# File 'lib/lutaml/model/type_context.rb', line 51 def fallback_contexts @fallback_contexts end |
#id ⇒ Symbol (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 context identifier.
42 43 44 |
# File 'lib/lutaml/model/type_context.rb', line 42 def id @id end |
#registry ⇒ TypeRegistry (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 primary type registry.
45 46 47 |
# File 'lib/lutaml/model/type_context.rb', line 45 def registry @registry end |
#substitutions ⇒ Array<TypeSubstitution> (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 Type substitution rules.
48 49 50 |
# File 'lib/lutaml/model/type_context.rb', line 48 def substitutions @substitutions end |
Class Method Details
.default ⇒ 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.
Factory: Create the default context with built-in types.
The default context contains all built-in types (string, integer, etc.) and has no fallbacks or substitutions.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/lutaml/model/type_context.rb', line 73 def self.default @default ||= begin registry = TypeRegistry.new register_builtin_types_in(registry) new( id: :default, registry: registry, substitutions: [], fallback_contexts: [], ) end end |
.derived(id:, registry:, 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.
Factory: Create a derived context with fallbacks.
A derived context has its own types but can fall back to other contexts when a type is not found.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/lutaml/model/type_context.rb', line 113 def self.derived(id:, registry:, fallback_to: [], substitutions: []) # Resolve fallback context IDs to actual contexts fallback_contexts = Array(fallback_to).filter_map do |ctx| resolve_fallback_context(ctx) end # Normalize substitutions sub_objects = Array(substitutions).map do |s| normalize_substitution(s) end new( id: id, registry: registry, substitutions: sub_objects, fallback_contexts: fallback_contexts, ) end |
.isolated(id, registry) ⇒ 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.
Factory: Create an isolated context with no fallbacks.
An isolated context only has the types explicitly registered in its registry. It does not fall back to any other context.
94 95 96 97 98 99 100 101 |
# File 'lib/lutaml/model/type_context.rb', line 94 def self.isolated(id, registry) new( id: id, registry: registry, substitutions: [], fallback_contexts: [], ) end |
.normalize_substitution(s) ⇒ Object
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.
264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/lutaml/model/type_context.rb', line 264 def self.normalize_substitution(s) case s when TypeSubstitution s when ::Hash TypeSubstitution.new( from_type: s[:from_type] || s["from_type"], to_type: s[:to_type] || s["to_type"], ) else raise ArgumentError, "Invalid substitution: #{s.inspect}" end end |
.register_builtin_types_in(registry) ⇒ 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 built-in types in a registry.
245 246 247 248 |
# File 'lib/lutaml/model/type_context.rb', line 245 def self.register_builtin_types_in(registry) # Delegate to Type module's new method Type.register_builtin_types_in(registry) end |
.resolve_fallback_context(ctx) ⇒ Object
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.
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/lutaml/model/type_context.rb', line 251 def self.resolve_fallback_context(ctx) case ctx when TypeContext ctx when Symbol, String # Try to look up from GlobalContext if available if defined?(GlobalContext) && GlobalContext.respond_to?(:registry) GlobalContext.registry.lookup(ctx) end end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
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.
Equality check.
Two contexts are equal if they have the same id.
226 227 228 229 230 |
# File 'lib/lutaml/model/type_context.rb', line 226 def ==(other) return false unless other.is_a?(TypeContext) id == other.id end |
#add_substitution(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 this context.
Note: This creates a new context since TypeContext is immutable.
139 140 141 142 143 144 145 146 147 |
# File 'lib/lutaml/model/type_context.rb', line 139 def add_substitution(from_type:, to_type:) new_sub = TypeSubstitution.new(from_type: from_type, to_type: to_type) self.class.new( id: id, registry: registry, substitutions: substitutions + [new_sub], fallback_contexts: fallback_contexts, ) end |
#fallback_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 fallback context IDs.
176 177 178 |
# File 'lib/lutaml/model/type_context.rb', line 176 def fallback_ids fallback_contexts.map(&:id) end |
#has_fallbacks? ⇒ 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 this context has any fallbacks.
169 170 171 |
# File 'lib/lutaml/model/type_context.rb', line 169 def has_fallbacks? !fallback_contexts.empty? end |
#has_type?(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 directly in this context’s registry.
184 185 186 |
# File 'lib/lutaml/model/type_context.rb', line 184 def has_type?(name) registry.registered?(name) end |
#hash ⇒ 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.
Hash code.
237 238 239 |
# File 'lib/lutaml/model/type_context.rb', line 237 def hash id.hash end |
#lookup_local(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 in this context’s registry only.
192 193 194 |
# File 'lib/lutaml/model/type_context.rb', line 192 def lookup_local(name) registry.lookup(name) end |
#substitution_for(from_type) ⇒ Array<TypeSubstitution>
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.
Find substitutions for a given type.
200 201 202 203 204 205 206 207 208 |
# File 'lib/lutaml/model/type_context.rb', line 200 def substitution_for(from_type) from_type_class = from_type.is_a?(Class) ? from_type : nil substitutions.select do |sub| sub.from_type == from_type || sub.from_type == from_type_class || (from_type_class && sub.from_type == from_type_class.to_s) end end |
#to_s ⇒ String Also known as: inspect
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.
Human-readable representation.
213 214 215 216 |
# File 'lib/lutaml/model/type_context.rb', line 213 def to_s fallback_str = has_fallbacks? ? " fallbacks=#{fallback_ids}" : "" "#<#{self.class.name} id=#{id}#{fallback_str}>" end |
#with_fallbacks(fallback_to:) ⇒ 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 a copy with different fallbacks.
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lutaml/model/type_context.rb', line 153 def with_fallbacks(fallback_to:) fallback_contexts = Array(fallback_to).filter_map do |ctx| self.class.resolve_fallback_context(ctx) end self.class.new( id: id, registry: registry, substitutions: substitutions, fallback_contexts: fallback_contexts, ) end |