class ParamsReady::Helpers::Memo
Public Class Methods
new(slots = 1)
click to toggle source
# File lib/params_ready/helpers/memo.rb, line 9 def initialize(slots = 1) raise ParamsReadyError, "Expected positive value for number of slots, got: '#{slots}'" unless slots > 0 @slots = slots @cache = nil end
Public Instance Methods
cache_value(value, key)
click to toggle source
# File lib/params_ready/helpers/memo.rb, line 23 def cache_value(value, key) stale = @cache return if stale&.key? key frozen = Extensions::Hash.try_deep_freeze(value) fresh = if stale.nil? || @slots == 1 { key => frozen } else kept = stale.to_a.last(@slots - 1) [*kept, [key, frozen]].to_h end @cache = fresh.freeze end
cached_value(key)
click to toggle source
# File lib/params_ready/helpers/memo.rb, line 15 def cached_value(key) cache = @cache return Extensions::Undefined if cache.nil? return Extensions::Undefined unless cache.key? key cache[key] end