class Utopia::Session::LazyHash
A simple hash table which fetches it's values only when required.
Attributes
values[R]
Public Class Methods
new(&block)
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 27 def initialize(&block) @changed = false @values = nil @loader = block end
Public Instance Methods
[](key)
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 36 def [] key load![key] end
[]=(key, value)
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 40 def []= key, value values = load! if values[key] != value values[key] = value @changed = true end return value end
changed?()
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 63 def changed? @changed end
delete(key)
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 55 def delete(key) load! @changed = true if @values.include? key @values.delete(key) end
include?(key)
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 51 def include?(key) load!.include?(key) end
load!()
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 67 def load! @values ||= @loader.call end
loaded?()
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 71 def loaded? !@values.nil? end
needs_update?(timeout = nil)
click to toggle source
# File lib/utopia/session/lazy_hash.rb, line 75 def needs_update?(timeout = nil) # If data has changed, we need update: return true if @changed # We want to be careful here and not call load! which isn't cheap operation. if timeout and @values and updated_at = @values[:updated_at] # If the last update was too long ago, we need update: return true if updated_at < (Time.now - timeout) end return false end