Class: Lookback::Backends::Snapshot
- Inherits:
-
Object
- Object
- Lookback::Backends::Snapshot
- Defined in:
- lib/lookback/backends.rb
Overview
In-memory snapshot backend. Stores an ordered list of versions per (record_class, record_id) key. Suitable for tests and single-process use; persistent stores land in later releases.
Defined Under Namespace
Classes: Version
Instance Method Summary collapse
- #clear ⇒ Object
- #history(key) ⇒ Object
-
#initialize ⇒ Snapshot
constructor
A new instance of Snapshot.
- #latest(key) ⇒ Object
- #record(key, attributes, event: :update, now: Time.now) ⇒ Object
Constructor Details
#initialize ⇒ Snapshot
Returns a new instance of Snapshot.
13 14 15 |
# File 'lib/lookback/backends.rb', line 13 def initialize @versions = Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#clear ⇒ Object
37 38 39 |
# File 'lib/lookback/backends.rb', line 37 def clear @versions.clear end |
#history(key) ⇒ Object
29 30 31 |
# File 'lib/lookback/backends.rb', line 29 def history(key) @versions[key].dup end |
#latest(key) ⇒ Object
33 34 35 |
# File 'lib/lookback/backends.rb', line 33 def latest(key) @versions[key].last end |
#record(key, attributes, event: :update, now: Time.now) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lookback/backends.rb', line 17 def record(key, attributes, event: :update, now: Time.now) list = @versions[key] version = Version.new( number: list.length + 1, recorded_at: now, attributes: deep_dup(attributes), event: event, ) list << version version end |