Class: Lutaml::Model::TypeContext Private

Inherits:
Object
  • Object
show all
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)

Examples:

Default context (built-in types only)

context = TypeContext.default
# Has built-in types (string, integer, boolean, etc.)
# No fallbacks, no substitutions

Isolated context (no fallbacks)

registry = TypeRegistry.new
registry.register(:custom, MyCustomClass)
context = TypeContext.isolated(:my_app, registry)
# Has only :custom type, no fallback to default

Derived context (with fallbacks)

registry = TypeRegistry.new
registry.register(:custom, MyCustomClass)
context = TypeContext.derived(
  id: :my_app,
  registry: registry,
  fallback_to: [:default]
)
# Has :custom, falls back to default for built-in types

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • id (Symbol)

    The context identifier

  • registry (TypeRegistry)

    The primary type registry

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

    Type substitution rules

  • fallback_contexts (Array<TypeContext>) (defaults to: [])

    Fallback contexts



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_contextsArray<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).

Returns:

  • (Array<TypeContext>)

    Fallback contexts (in order)



54
55
56
# File 'lib/lutaml/model/type_context.rb', line 54

def fallback_contexts
  @fallback_contexts
end

#idSymbol (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.

Returns:

  • (Symbol)

    The context identifier



42
43
44
# File 'lib/lutaml/model/type_context.rb', line 42

def id
  @id
end

#registryTypeRegistry (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.

Returns:



45
46
47
# File 'lib/lutaml/model/type_context.rb', line 45

def registry
  @registry
end

#substitution_hashHash{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.

Returns:

  • (Hash{Class => Class})

    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

#substitutionsArray<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.

Returns:



48
49
50
# File 'lib/lutaml/model/type_context.rb', line 48

def substitutions
  @substitutions
end

Class Method Details

.defaultTypeContext

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.

Returns:



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.

Parameters:

  • id (Symbol)

    The context identifier

  • registry (TypeRegistry)

    The primary type registry

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

    Fallback context IDs or contexts

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

    Substitution rules

Returns:



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.

Parameters:

  • id (Symbol)

    The context identifier

  • registry (TypeRegistry)

    The type registry

Returns:



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.

Parameters:



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.

Parameters:

  • other (Object)

    Object to compare

Returns:

  • (Boolean)

    true if equal



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.

Parameters:

  • from_type (Class)

    Type to substitute from

  • to_type (Class)

    Type to substitute to

Returns:

  • (TypeContext)

    New context with the substitution added



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_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 fallback context IDs.

Returns:

  • (Array<Symbol>)

    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.

Returns:

  • (Boolean)

    true if has 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.

Parameters:

  • name (Symbol, String)

    Type name

Returns:

  • (Boolean)

    true if type is registered



190
191
192
# File 'lib/lutaml/model/type_context.rb', line 190

def has_type?(name)
  registry.registered?(name)
end

#hashInteger

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.

Returns:

  • (Integer)

    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.

Parameters:

  • name (Symbol, String)

    Type name

Returns:

  • (Class, nil)

    Type class or nil



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.

Parameters:

  • from_type (Class, Symbol, String)

    The type to find substitutions for

Returns:



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_sString 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.

Returns:

  • (String)

    String 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.

Parameters:

  • fallback_to (Array<Symbol, TypeContext>)

    New fallback contexts

Returns:



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