Class: Lutaml::Model::AdapterScope

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/adapter_scope.rb

Overview

Thread-local scoped adapter override stack.

Provides block-scoped adapter overrides for testing and library stacking. Each thread has its own stack — no mutex needed.

Examples:

Testing with a specific adapter

Lutaml::Model::Config.with_adapter(xml: :ox) do
  MyClass.from_xml(xml)  # Uses Ox
end
# Outside the block, reverts to configured default

Library stacking

# Library A guarantees Ox internally
def self.parse(data)
  Config.with_adapter(xml: :nokogiri) { MyModel.from_xml(data) }
end

Constant Summary collapse

STACK_KEY =
:__lutaml_adapter_scope_stack
EMPTY =
{}.freeze

Class Method Summary collapse

Class Method Details

.currentHash{Symbol => Symbol}

Return the current scope’s overrides hash.

Returns:

  • (Hash{Symbol => Symbol})

    current overrides or empty hash



44
45
46
# File 'lib/lutaml/model/adapter_scope.rb', line 44

def self.current
  Thread.current[STACK_KEY]&.last || EMPTY
end

.override_for(format) ⇒ Symbol?

Return the override for a specific format from the current scope.

Parameters:

  • format (Symbol)

    the format name (:xml, :json, etc.)

Returns:

  • (Symbol, nil)

    the adapter type name or nil



52
53
54
# File 'lib/lutaml/model/adapter_scope.rb', line 52

def self.override_for(format)
  current[format]
end

.reset!void

This method returns an undefined value.

Clear all scope state (for testing reset).



59
60
61
# File 'lib/lutaml/model/adapter_scope.rb', line 59

def self.reset!
  Thread.current[STACK_KEY] = nil
end

.with(overrides) { ... } ⇒ Object

Push adapter overrides and yield. Restores previous scope on exit.

Parameters:

  • overrides (Hash{Symbol => Symbol})

    format => adapter type name e.g., { xml: :ox, json: :oj }

Yields:

  • block within which overrides are active

Returns:

  • (Object)

    the block’s return value



32
33
34
35
36
37
38
39
# File 'lib/lutaml/model/adapter_scope.rb', line 32

def self.with(overrides)
  stack = Thread.current[STACK_KEY] ||= []
  stack.push(overrides)
  yield
ensure
  stack.pop
  Thread.current[STACK_KEY] = nil if stack.empty?
end