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



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_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)



51
52
53
# File 'lib/lutaml/model/type_context.rb', line 51

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

#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:



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.

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:



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.

Parameters:

  • id (Symbol)

    The context identifier

  • registry (TypeRegistry)

    The type registry

Returns:



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.

Parameters:



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.

Parameters:

  • other (Object)

    Object to compare

Returns:

  • (Boolean)

    true if equal



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.

Parameters:

  • from_type (Class)

    Type to substitute from

  • to_type (Class)

    Type to substitute to

Returns:

  • (TypeContext)

    New context with the substitution added



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_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



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.

Returns:

  • (Boolean)

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

Parameters:

  • name (Symbol, String)

    Type name

Returns:

  • (Boolean)

    true if type is registered



184
185
186
# File 'lib/lutaml/model/type_context.rb', line 184

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



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.

Parameters:

  • name (Symbol, String)

    Type name

Returns:

  • (Class, nil)

    Type class or nil



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.

Parameters:

  • from_type (Class, Symbol, String)

    The type to find substitutions for

Returns:



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_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



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.

Parameters:

  • fallback_to (Array<Symbol, TypeContext>)

    New fallback contexts

Returns:



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