Class: Lutaml::Model::FinalizationCache
- Inherits:
-
Object
- Object
- Lutaml::Model::FinalizationCache
- 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
-
#clear ⇒ Object
Clear cached entries without changing finalized status.
-
#fetch(key) ⇒ Object
Fetch a cached value by key.
-
#fetch_or_store(key) ⇒ Object
Fetch cached value, computing via block on cache miss.
-
#finalize! ⇒ Object
Mark the cache as finalized and clear any stale entries.
- #finalized? ⇒ Boolean
-
#initialize ⇒ FinalizationCache
constructor
A new instance of FinalizationCache.
Constructor Details
#initialize ⇒ FinalizationCache
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
#clear ⇒ Object
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
18 19 20 |
# File 'lib/lutaml/model/finalization_cache.rb', line 18 def finalized? @finalized end |