Class: OhSnap::Registry
- Inherits:
-
Object
- Object
- OhSnap::Registry
- Defined in:
- lib/oh_snap/registry.rb
Overview
Registry is responsible for managing snapshots during a test run.
Instance Method Summary collapse
-
#initialize(path:, serializer:, snapshots_dirname:, writable:) ⇒ Registry
constructor
A new instance of Registry.
- #save ⇒ Object
- #snapshot(name:) ⇒ Object
Constructor Details
#initialize(path:, serializer:, snapshots_dirname:, writable:) ⇒ Registry
Returns a new instance of Registry.
6 7 8 9 10 11 12 |
# File 'lib/oh_snap/registry.rb', line 6 def initialize(path:, serializer:, snapshots_dirname:, writable:) @path = path @serializer = serializer @snapshots_dirname = snapshots_dirname @writable = writable @snapshots = {} end |
Instance Method Details
#save ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/oh_snap/registry.rb', line 30 def save return unless writable? Dir.mkdir(snapshots_path) unless Dir.exist?(snapshots_path) @snapshots.each do |name, snapshot| dump(name, snapshot) end end |
#snapshot(name:) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/oh_snap/registry.rb', line 14 def snapshot(name:) return @snapshots[name] if @snapshots&.key?(name) unless File.exist?(snapshot_path(name)) return @snapshots[name] = @serializer.load('') unless writable? return @snapshots[name] = @serializer.load(nil) end File.open(snapshot_path(name), 'r') do |file| @snapshots[name] = @serializer.load(file.read) end @snapshots[name] end |