Class: Evilution::Session::Store Private

Inherits:
Object
  • Object
show all
Defined in:
lib/evilution/session/store.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DEFAULT_DIR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

".evilution/results"

Instance Method Summary collapse

Constructor Details

#initialize(results_dir: DEFAULT_DIR) ⇒ Store

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Store.



14
15
16
# File 'lib/evilution/session/store.rb', line 14

def initialize(results_dir: DEFAULT_DIR)
  @results_dir = results_dir
end

Instance Method Details

#gc(older_than:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/evilution/session/store.rb', line 47

def gc(older_than:)
  return [] unless Dir.exist?(@results_dir)

  deleted = []
  Dir.glob(File.join(@results_dir, "*.json")).each do |file|
    timestamp = parse_filename_timestamp(File.basename(file))
    next unless timestamp
    next unless timestamp < older_than

    File.delete(file)
    deleted << file
  end
  deleted
end

#listObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
34
35
36
37
# File 'lib/evilution/session/store.rb', line 29

def list
  return [] unless Dir.exist?(@results_dir)

  Dir
    .glob(File.join(@results_dir, "*.json"))
    .sort_by { |f| File.basename(f) }
    .reverse
    .filter_map { |f| build_list_entry(f) }
end

#load(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



39
40
41
42
43
44
45
# File 'lib/evilution/session/store.rb', line 39

def load(path)
  raise Evilution::Error, "session file not found: #{path}" unless File.exist?(path)

  data = JSON.parse(File.read(path))
  Evilution::Session::Schema.validate!(data, source: path) if data.is_a?(Hash)
  data
end

#save(summary) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
24
25
26
27
# File 'lib/evilution/session/store.rb', line 18

def save(summary)
  FileUtils.mkdir_p(@results_dir)

  now = Time.now
  data = build_session_data(summary, now)
  filename = "#{format_timestamp(now)}-#{SecureRandom.hex(4)}.json"
  path = File.join(@results_dir, filename)
  atomic_write(path, JSON.pretty_generate(data))
  path
end