Class: Insta::SnapshotFile
- Inherits:
-
Object
- Object
- Insta::SnapshotFile
- Defined in:
- lib/insta/snapshot_file.rb,
sig/insta/snapshot_file.rbs
Instance Attribute Summary collapse
-
#base_path ⇒ Pathname
readonly
: Pathname.
-
#extension ⇒ String
readonly
: String.
Instance Method Summary collapse
-
#initialize(base_path, test_class, test_name, extension = nil) ⇒ SnapshotFile
constructor
: (String, String, String, ?String?) -> void.
-
#named_path(name) ⇒ Pathname
: (String) -> Pathname.
-
#path_for(options = {}) ⇒ Pathname
: (?Hash[Symbol, untyped]) -> Pathname.
-
#pending_path(path) ⇒ Pathname
: (Pathname) -> Pathname.
-
#read(path) ⇒ Snapshot?
: (Pathname) -> Snapshot?.
-
#write(path, content, metadata = {}) ⇒ void
: (Pathname, String, Snapshot::snapshot_metadata) -> void.
Constructor Details
#initialize(base_path, test_class, test_name, extension = nil) ⇒ SnapshotFile
: (String, String, String, ?String?) -> void
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/insta/snapshot_file.rb', line 12 def initialize(base_path, test_class, test_name, extension = nil) directory_resolver = Insta.configuration.snapshot_directory directory = if directory_resolver directory_resolver.call(test_class: test_class) else SnapshotName.underscore(test_class) end @base_path = Pathname.new(base_path) / directory @test_name = test_name @extension = extension || Insta.configuration.snapshot_extension @counter = 0 end |
Instance Attribute Details
#base_path ⇒ Pathname (readonly)
: Pathname
8 9 10 |
# File 'lib/insta/snapshot_file.rb', line 8 def base_path @base_path end |
#extension ⇒ String (readonly)
: String
9 10 11 |
# File 'lib/insta/snapshot_file.rb', line 9 def extension @extension end |
Instance Method Details
#named_path(name) ⇒ Pathname
: (String) -> Pathname
40 41 42 |
# File 'lib/insta/snapshot_file.rb', line 40 def named_path(name) @base_path / "#{SnapshotName.sanitize(name)}#{@extension}" end |
#path_for(options = {}) ⇒ Pathname
: (?Hash[Symbol, untyped]) -> Pathname
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/insta/snapshot_file.rb', line 26 def path_for( = {}) @counter += 1 filename_resolver = Insta.configuration.snapshot_filename derived = if filename_resolver filename_resolver.call(test_name: @test_name, counter: @counter, options: ) else SnapshotName.derive(@test_name, counter: @counter, options: ) end @base_path / "#{derived}#{@extension}" end |
#pending_path(path) ⇒ Pathname
: (Pathname) -> Pathname
45 46 47 |
# File 'lib/insta/snapshot_file.rb', line 45 def pending_path(path) Pathname.new("#{path}.new") end |
#read(path) ⇒ Snapshot?
: (Pathname) -> Snapshot?
58 59 60 61 62 |
# File 'lib/insta/snapshot_file.rb', line 58 def read(path) return nil unless path.exist? Snapshot.parse(path.read) end |
#write(path, content, metadata = {}) ⇒ void
This method returns an undefined value.
: (Pathname, String, Snapshot::snapshot_metadata) -> void
50 51 52 53 54 55 |
# File 'lib/insta/snapshot_file.rb', line 50 def write(path, content, = {}) FileUtils.mkdir_p(path.dirname) snapshot = Snapshot.new(content, ) path.write(snapshot.serialize) end |