Class: Evilution::Session::Store
- Inherits:
-
Object
- Object
- Evilution::Session::Store
- Defined in:
- lib/evilution/session/store.rb
Constant Summary collapse
- DEFAULT_DIR =
".evilution/results"
Instance Method Summary collapse
- #gc(older_than:) ⇒ Object
-
#initialize(results_dir: DEFAULT_DIR) ⇒ Store
constructor
A new instance of Store.
- #list ⇒ Object
- #load(path) ⇒ Object
- #save(summary) ⇒ Object
Constructor Details
#initialize(results_dir: DEFAULT_DIR) ⇒ Store
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
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| = (File.basename(file)) next unless next unless < older_than File.delete(file) deleted << file end deleted end |
#list ⇒ Object
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
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
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 = "#{(now)}-#{SecureRandom.hex(4)}.json" path = File.join(@results_dir, filename) atomic_write(path, JSON.pretty_generate(data)) path end |