Class: Lutaml::Model::OneEntryCache

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

Overview

A single-entry cache that stores one key-value pair at a time. Used for MappingRule#namespaced_names where the same parent_namespace is queried repeatedly for all rules of a given element.

When the key changes, the old entry is replaced. This avoids hash allocation for the common case of a single repeated key.

Instance Method Summary collapse

Constructor Details

#initializeOneEntryCache

Returns a new instance of OneEntryCache.



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

def initialize
  @key = nil
  @value = nil
  @filled = false
end

Instance Method Details

#clearObject

Clear the cached entry.



46
47
48
49
50
# File 'lib/lutaml/model/one_entry_cache.rb', line 46

def clear
  @key = nil
  @value = nil
  @filled = false
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  !@filled
end

#fetch(key) ⇒ Object

Returns the cached value if key matches, nil otherwise. Uses @filled flag to distinguish “no cache” from “cached nil”.



20
21
22
23
24
25
# File 'lib/lutaml/model/one_entry_cache.rb', line 20

def fetch(key)
  return nil unless @filled
  return @value if @key == key

  nil
end

#fetch_or_compute(key) ⇒ Object

Fetch cached value, computing via block on cache miss.



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

def fetch_or_compute(key)
  cached = fetch(key)
  return cached unless cached.nil?

  value = yield
  store(key, value)
  value
end

#store(key, value) ⇒ Object

Store a value for the given key, replacing any previous entry.



28
29
30
31
32
33
# File 'lib/lutaml/model/one_entry_cache.rb', line 28

def store(key, value)
  @key = key
  @value = value
  @filled = true
  value
end