Class: Navo::SuiteState

Inherits:
Object
  • Object
show all
Defined in:
lib/navo/suite_state.rb

Overview

Stores persisted state about a test suite.

This allows information to carry forward between different invocations of the tool, e.g. remembering a previously-started Docker container.

Constant Summary collapse

FILE_NAME =
'state.yaml'

Instance Method Summary collapse

Constructor Details

#initialize(suite:) ⇒ SuiteState

Returns a new instance of SuiteState.



12
13
14
# File 'lib/navo/suite_state.rb', line 12

def initialize(suite:)
  @suite = suite
end

Instance Method Details

#[](key) ⇒ Array, ...

Access the state as if it were a hash.

Parameters:

  • key (String, Symbol)

Returns:

  • (Array, Hash, Number, String)


20
21
22
# File 'lib/navo/suite_state.rb', line 20

def [](key)
  @hash[key.to_s]
end

#[]=(key, value) ⇒ Object

Set the state as if it were a hash.

Parameters:

  • key (String, Symbol)
  • value (Array, Hash, Number, String)


28
29
30
# File 'lib/navo/suite_state.rb', line 28

def []=(key, value)
  @hash[key.to_s] = value
end

#destroyObject

Destroy persisted state.



48
49
50
51
# File 'lib/navo/suite_state.rb', line 48

def destroy
  @hash = {}
  FileUtils.rm_f(file_path)
end

#loadObject

Loads persisted state.



33
34
35
36
37
38
39
40
# File 'lib/navo/suite_state.rb', line 33

def load
  @hash =
    if File.exist?(file_path) && yaml = YAML.load_file(file_path)
      yaml.to_hash
    else
      {} # Handle empty files
    end
end

#saveObject

Persists state to disk.



43
44
45
# File 'lib/navo/suite_state.rb', line 43

def save
  File.open(file_path, 'w') { |f| f.write(@hash.to_yaml) }
end