Class: Lutaml::Model::FinalizationCache

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

Overview

A hash-backed cache that only stores values after finalization. Used by Xml::Mapping for caching elements/attributes/mappings after the mapping definition is complete.

Before finalization, fetch_or_store computes but does not cache. After finalization, results are cached and frozen per key. Calling finalize! clears all cached entries.

Instance Method Summary collapse

Constructor Details

#initializeFinalizationCache

Returns a new instance of FinalizationCache.



13
14
15
16
# File 'lib/lutaml/model/finalization_cache.rb', line 13

def initialize
  @store = {}
  @finalized = false
end

Instance Method Details

#clearObject

Clear cached entries without changing finalized status.



46
47
48
# File 'lib/lutaml/model/finalization_cache.rb', line 46

def clear
  @store.clear
end

#fetch(key) ⇒ Object

Fetch a cached value by key. Returns nil if not found.



30
31
32
# File 'lib/lutaml/model/finalization_cache.rb', line 30

def fetch(key)
  @store[key]
end

#fetch_or_store(key) ⇒ Object

Fetch cached value, computing via block on cache miss. Only caches and freezes the result after finalization.



36
37
38
39
40
41
42
43
# File 'lib/lutaml/model/finalization_cache.rb', line 36

def fetch_or_store(key)
  cached = @store[key]
  return cached if cached

  value = yield
  @store[key] = value.freeze if @finalized
  value
end

#finalize!Object

Mark the cache as finalized and clear any stale entries. Called when the mapping definition is complete.



24
25
26
27
# File 'lib/lutaml/model/finalization_cache.rb', line 24

def finalize!
  @store.clear
  @finalized = true
end

#finalized?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/lutaml/model/finalization_cache.rb', line 18

def finalized?
  @finalized
end