Class: RspecSprint::SnapshotStore
- Inherits:
-
Object
- Object
- RspecSprint::SnapshotStore
- Defined in:
- lib/rspec_sprint/snapshot_store.rb
Overview
Persists normalized snapshots to .rspec-sprint/snapshots/ for future comparison (the seed for –compare-last / Test Performance Ledger). Failure-safe: disk errors warn and skip; doctor never crashes because of us.
Constant Summary collapse
- DEFAULT_DIR =
".rspec-sprint/snapshots"- BASELINE_PATH =
".rspec-sprint/baseline.json"- MAX_SNAPSHOTS =
3
Class Method Summary collapse
- .load_baseline(path: BASELINE_PATH) ⇒ Object
- .load_last(dir: DEFAULT_DIR) ⇒ Object
- .save(snapshot, dir: DEFAULT_DIR) ⇒ Object
- .save_baseline(snapshot, path: BASELINE_PATH) ⇒ Object
Instance Method Summary collapse
-
#initialize(dir: DEFAULT_DIR) ⇒ SnapshotStore
constructor
A new instance of SnapshotStore.
- #list ⇒ Object
-
#load_baseline(path: BASELINE_PATH) ⇒ Object
Returns the saved baseline as a symbol-keyed Hash, or nil if none.
-
#load_last ⇒ Object
Returns the most recent snapshot as a symbol-keyed Hash, or nil if none.
- #save(snapshot) ⇒ Object
-
#save_baseline(snapshot, path: BASELINE_PATH) ⇒ Object
Saves snapshot as the named baseline (single file, never pruned).
Constructor Details
#initialize(dir: DEFAULT_DIR) ⇒ SnapshotStore
Returns a new instance of SnapshotStore.
32 33 34 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 32 def initialize(dir: DEFAULT_DIR) @dir = dir end |
Class Method Details
.load_baseline(path: BASELINE_PATH) ⇒ Object
28 29 30 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 28 def self.load_baseline(path: BASELINE_PATH) new.load_baseline(path: path) end |
.load_last(dir: DEFAULT_DIR) ⇒ Object
20 21 22 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 20 def self.load_last(dir: DEFAULT_DIR) new(dir: dir).load_last end |
.save(snapshot, dir: DEFAULT_DIR) ⇒ Object
16 17 18 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 16 def self.save(snapshot, dir: DEFAULT_DIR) new(dir: dir).save(snapshot) end |
.save_baseline(snapshot, path: BASELINE_PATH) ⇒ Object
24 25 26 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 24 def self.save_baseline(snapshot, path: BASELINE_PATH) new.save_baseline(snapshot, path: path) end |
Instance Method Details
#list ⇒ Object
45 46 47 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 45 def list Dir.glob(File.join(@dir, "*.json")).sort end |
#load_baseline(path: BASELINE_PATH) ⇒ Object
Returns the saved baseline as a symbol-keyed Hash, or nil if none.
68 69 70 71 72 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 68 def load_baseline(path: BASELINE_PATH) JSON.parse(File.read(path), symbolize_names: true) rescue JSON::ParserError, Errno::ENOENT nil end |
#load_last ⇒ Object
Returns the most recent snapshot as a symbol-keyed Hash, or nil if none.
50 51 52 53 54 55 56 57 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 50 def load_last files = list return nil if files.empty? JSON.parse(File.read(files.last), symbolize_names: true) rescue JSON::ParserError, Errno::ENOENT nil end |
#save(snapshot) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 36 def save(snapshot) FileUtils.mkdir_p(@dir) path = File.join(@dir, "#{}.json") File.write(path, serialize(snapshot)) prune rescue Errno::EACCES, Errno::ENOSPC => e warn "rspec-sprint: snapshot not saved (#{e.class.name.split("::").last}: #{e.})" end |
#save_baseline(snapshot, path: BASELINE_PATH) ⇒ Object
Saves snapshot as the named baseline (single file, never pruned).
60 61 62 63 64 65 |
# File 'lib/rspec_sprint/snapshot_store.rb', line 60 def save_baseline(snapshot, path: BASELINE_PATH) FileUtils.mkdir_p(File.dirname(path)) File.write(path, serialize(snapshot)) rescue Errno::EACCES, Errno::ENOSPC => e warn "rspec-sprint: baseline not saved (#{e.class.name.split("::").last}: #{e.})" end |