Class: Lutaml::Model::ImportRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/import_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.

ImportRegistry manages deferred imports with explicit resolution.

This is an INTERNAL class. Users should use Register and GlobalRegister.

Responsibility: Track and resolve deferred type imports

This class:

  • Tracks pending imports per class

  • Resolves imports in correct order (topological)

  • NO TracePoint needed - explicit resolution

  • Tracks resolution state to prevent re-resolution

Examples:

Deferring an import

registry = ImportRegistry.new
registry.defer(MyClass, method: :author, symbol: :Person)

Resolving imports

registry.resolve(MyClass, context)
# Now MyClass.author type is resolved to Person class

Defined Under Namespace

Classes: DeferredImport

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImportRegistry

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



42
43
44
45
46
# File 'lib/lutaml/model/import_registry.rb', line 42

def initialize
  @pending_imports = {}
  @resolved_classes = Set.new
  @mutex = Mutex.new
end

Instance Attribute Details

#pending_importsHash<Class, Array<DeferredImport>> (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 Pending imports by owner class.

Returns:



36
37
38
# File 'lib/lutaml/model/import_registry.rb', line 36

def pending_imports
  @pending_imports
end

#resolved_classesSet<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 Classes whose imports have been resolved.

Returns:

  • (Set<Class>)

    Classes whose imports have been resolved



39
40
41
# File 'lib/lutaml/model/import_registry.rb', line 39

def resolved_classes
  @resolved_classes
end

Instance Method Details

#defer(owner_class, method:, symbol:) ⇒ DeferredImport

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.

Defer an import for later resolution.

Parameters:

  • owner_class (Class)

    The class that owns the import

  • method (Symbol)

    The method/attribute to resolve

  • symbol (Symbol)

    The symbol name to resolve

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lutaml/model/import_registry.rb', line 54

def defer(owner_class, method:, symbol:)
  import = DeferredImport.new(
    owner_class: owner_class,
    method: method,
    symbol: symbol,
    resolved: false,
  )

  @mutex.synchronize do
    @pending_imports[owner_class] ||= []
    @pending_imports[owner_class] << import
    # Mark as not resolved if we're adding new imports
    @resolved_classes.delete(owner_class)
  end

  import
end

#imports_for(owner_class) ⇒ Array<DeferredImport>

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 pending imports for a class.

Parameters:

  • owner_class (Class)

    The class to get imports for

Returns:



182
183
184
185
186
# File 'lib/lutaml/model/import_registry.rb', line 182

def imports_for(owner_class)
  @mutex.synchronize do
    @pending_imports[owner_class] || []
  end
end

#pending?(owner_class) ⇒ 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 class has pending (unresolved) imports.

Parameters:

  • owner_class (Class)

    The class to check

Returns:

  • (Boolean)

    true if class has pending imports



144
145
146
147
148
# File 'lib/lutaml/model/import_registry.rb', line 144

def pending?(owner_class)
  @mutex.synchronize do
    pending_without_lock?(owner_class)
  end
end

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

Get all classes with pending imports.

Returns:

  • (Array<Class>)

    The classes with pending imports



191
192
193
194
195
# File 'lib/lutaml/model/import_registry.rb', line 191

def pending_classes
  @mutex.synchronize do
    @pending_imports.keys.select { |k| pending_without_lock?(k) }
  end
end

#reset!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.

Reset all state (for testing).



200
201
202
203
204
205
# File 'lib/lutaml/model/import_registry.rb', line 200

def reset!
  @mutex.synchronize do
    @pending_imports.clear
    @resolved_classes.clear
  end
end

#resolve(owner_class, context) ⇒ Array<DeferredImport>

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.

Resolve all imports for a specific class.

Parameters:

  • owner_class (Class)

    The class to resolve imports for

  • context (TypeContext)

    The resolution context

Returns:

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lutaml/model/import_registry.rb', line 78

def resolve(owner_class, context)
  return [] unless pending?(owner_class)

  resolved = []

  @mutex.synchronize do
    imports = @pending_imports[owner_class] || []
    imports.each do |import|
      next if import.resolved?

      # Resolve the type
      resolved_type = TypeResolver.resolve(import.symbol, context)

      # Store the resolved type on the owner class's attribute
      apply_resolution(owner_class, import.method, resolved_type)

      import.resolved = true
      resolved << import
    end

    @resolved_classes << owner_class
  end

  resolved
end

#resolve_all!(context) ⇒ 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.

Resolve all pending imports.

Parameters:

Returns:

  • (Integer)

    Number of imports resolved



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lutaml/model/import_registry.rb', line 108

def resolve_all!(context)
  resolved_count = 0

  @mutex.synchronize do
    @pending_imports.each_key do |owner_class|
      next if @resolved_classes.include?(owner_class)

      imports = @pending_imports[owner_class] || []
      imports.each do |import|
        next if import.resolved?

        begin
          resolved_type = TypeResolver.resolve(import.symbol, context)
          apply_resolution(owner_class, import.method, resolved_type)
          import.resolved = true
          resolved_count += 1
        rescue UnknownTypeError
          # Skip types that can't be resolved yet
          # They may be resolved in a later pass or by another context
        end
      end

      # Check if all imports for this class are resolved
      if imports.all?(&:resolved?)
        @resolved_classes << owner_class
      end
    end
  end

  resolved_count
end

#resolved?(owner_class) ⇒ 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 class’s imports are fully resolved.

Parameters:

  • owner_class (Class)

    The class to check

Returns:

  • (Boolean)

    true if all imports are resolved



169
170
171
172
173
174
175
176
# File 'lib/lutaml/model/import_registry.rb', line 169

def resolved?(owner_class)
  @mutex.synchronize do
    return true unless @pending_imports.key?(owner_class)
    return false unless @resolved_classes.include?(owner_class)

    @pending_imports[owner_class].all?(&:resolved?)
  end
end

#statsHash

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 statistics about the registry.

Returns:

  • (Hash)

    Statistics including pending and resolved counts



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/lutaml/model/import_registry.rb', line 210

def stats
  @mutex.synchronize do
    total = @pending_imports.values.sum(&:size)
    resolved = @pending_imports.values.sum do |imports|
      imports.count(&:resolved?)
    end
    pending_count = @pending_imports.keys.count do |k|
      pending_without_lock?(k)
    end

    {
      total_imports: total,
      resolved_imports: resolved,
      pending_imports: total - resolved,
      pending_classes: pending_count,
    }
  end
end