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.
-
#substitution_hash ⇒ Hash{Class => Class}
readonly
private
Pre-built Hash for O(1) substitution lookup.
-
#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.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lutaml/model/type_context.rb', line 62 def initialize(id:, registry:, substitutions: [], fallback_contexts: []) @id = id.to_sym @registry = registry @substitutions = Array(substitutions).freeze @substitution_hash = @substitutions.to_h do |s| [s.from_type, s.to_type] end.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).
54 55 56 |
# File 'lib/lutaml/model/type_context.rb', line 54 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 |
#substitution_hash ⇒ Hash{Class => Class} (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 Pre-built Hash for O(1) substitution lookup.
51 52 53 |
# File 'lib/lutaml/model/type_context.rb', line 51 def substitution_hash @substitution_hash 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.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lutaml/model/type_context.rb', line 79 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.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/lutaml/model/type_context.rb', line 119 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.
100 101 102 103 104 105 106 107 |
# File 'lib/lutaml/model/type_context.rb', line 100 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.
270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/lutaml/model/type_context.rb', line 270 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.
251 252 253 254 |
# File 'lib/lutaml/model/type_context.rb', line 251 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.
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/lutaml/model/type_context.rb', line 257 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.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.
232 233 234 235 236 |
# File 'lib/lutaml/model/type_context.rb', line 232 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.
145 146 147 148 149 150 151 152 153 |
# File 'lib/lutaml/model/type_context.rb', line 145 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.
182 183 184 |
# File 'lib/lutaml/model/type_context.rb', line 182 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.
175 176 177 |
# File 'lib/lutaml/model/type_context.rb', line 175 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.
190 191 192 |
# File 'lib/lutaml/model/type_context.rb', line 190 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.
243 244 245 |
# File 'lib/lutaml/model/type_context.rb', line 243 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.
198 199 200 |
# File 'lib/lutaml/model/type_context.rb', line 198 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.
206 207 208 209 210 211 212 213 214 |
# File 'lib/lutaml/model/type_context.rb', line 206 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.
219 220 221 222 |
# File 'lib/lutaml/model/type_context.rb', line 219 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.
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/lutaml/model/type_context.rb', line 159 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 |