Class: Utopia::Session::LazyHash
- Inherits:
-
Object
- Object
- Utopia::Session::LazyHash
- Defined in:
- lib/utopia/session/lazy_hash.rb
Overview
A simple hash table which fetches it's values only when required.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch a value by key, loading the hash if necessary.
-
#[]=(key, value) ⇒ Object
Store a value by key.
-
#changed? ⇒ Boolean
Check whether any value has changed.
-
#delete(key) ⇒ Object
Delete a value by key.
-
#include?(key) ⇒ Boolean
Check whether the hash contains a key.
-
#initialize(&block) ⇒ LazyHash
constructor
Initialize a lazily loaded hash.
-
#loaded? ⇒ Boolean
Check whether the underlying values have been loaded.
-
#needs_update?(timeout = nil) ⇒ Boolean
Check whether the values should be persisted.
-
#now ⇒ Object
The current time for session expiry and persistence.
-
#persist(timeout = nil) ⇒ Object
Persist the session values if they have changed or require updating.
Constructor Details
#initialize(&block) ⇒ LazyHash
Initialize a lazily loaded hash.
12 13 14 15 16 17 |
# File 'lib/utopia/session/lazy_hash.rb', line 12 def initialize(&block) @changed = false @values = nil @loader = block end |
Instance Method Details
#[](key) ⇒ Object
Fetch a value by key, loading the hash if necessary.
22 23 24 |
# File 'lib/utopia/session/lazy_hash.rb', line 22 def [] key load![key] end |
#[]=(key, value) ⇒ Object
Store a value by key.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/utopia/session/lazy_hash.rb', line 30 def []= key, value values = load! if values[key] != value values[key] = value @changed = true end return value end |
#changed? ⇒ Boolean
Check whether any value has changed.
61 62 63 |
# File 'lib/utopia/session/lazy_hash.rb', line 61 def changed? @changed end |
#delete(key) ⇒ Object
Delete a value by key.
51 52 53 54 55 56 57 |
# File 'lib/utopia/session/lazy_hash.rb', line 51 def delete(key) load! @changed = true if @values.include? key @values.delete(key) end |
#include?(key) ⇒ Boolean
Check whether the hash contains a key.
44 45 46 |
# File 'lib/utopia/session/lazy_hash.rb', line 44 def include?(key) load!.include?(key) end |
#loaded? ⇒ Boolean
Check whether the underlying values have been loaded.
89 90 91 |
# File 'lib/utopia/session/lazy_hash.rb', line 89 def loaded? !@values.nil? end |
#needs_update?(timeout = nil) ⇒ Boolean
Check whether the values should be persisted.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/utopia/session/lazy_hash.rb', line 96 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 < (now - timeout) end return false end |
#now ⇒ Object
The current time for session expiry and persistence.
67 68 69 |
# File 'lib/utopia/session/lazy_hash.rb', line 67 def now Time.now.utc end |
#persist(timeout = nil) ⇒ Object
Persist the session values if they have changed or require updating.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/utopia/session/lazy_hash.rb', line 75 def persist(timeout = nil) return unless needs_update?(timeout) values = load! updated_at = values[:updated_at] = now result = yield(values, updated_at) @changed = false return result end |