Class: Perfer::Store

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Store

Returns a new instance of Store.



4
5
6
# File 'lib/perfer/store.rb', line 4

def initialize(file)
  @file = Path(file)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/perfer/store.rb', line 3

def file
  @file
end

Class Method Details

.all_results_pathsObject



12
13
14
# File 'lib/perfer/store.rb', line 12

def self.all_results_paths
  results_dir.glob('**/*.yml')
end

.for_session(session) ⇒ Object



31
32
33
# File 'lib/perfer/store.rb', line 31

def self.for_session(session)
  new path_for_bench_file(session.file)
end

.path_for_bench_file(bench_file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/perfer/store.rb', line 16

def self.path_for_bench_file(bench_file)
  path = results_dir
  path.mkpath unless path.exist?

  bench_file = Path(bench_file)
  return bench_file if bench_file.inside?(results_dir)

  # get the relative path to root, and relocate in @path
  names = bench_file.each_filename.to_a
  # prepend drive letter on Windows
  names.unshift bench_file.path[0..0].upcase if File.dirname('C:') == 'C:.'

  path.join(*names).add_ext('.yml')
end

.results_dirObject



8
9
10
# File 'lib/perfer/store.rb', line 8

def self.results_dir
  DIR/'results'
end

Instance Method Details

#append(result) ⇒ Object



52
53
54
55
# File 'lib/perfer/store.rb', line 52

def append(result)
  @file.dir.mkpath unless @file.dir.exist?
  @file.append YAML.dump(result)
end

#deleteObject



35
36
37
# File 'lib/perfer/store.rb', line 35

def delete
  @file.unlink
end

#loadObject



45
46
47
48
49
50
# File 'lib/perfer/store.rb', line 45

def load
  return unless @file.exist?
  yaml_load_documents.map! { |doc|
    Result === doc ? doc : Result.new(doc[:metadata], doc[:data])
  }
end

#rewriteObject



64
65
66
# File 'lib/perfer/store.rb', line 64

def rewrite
  save load
end

#save(results) ⇒ Object



57
58
59
60
61
62
# File 'lib/perfer/store.rb', line 57

def save(results)
  @file.dir.mkpath unless @file.dir.exist?
  # ensure results are still ordered by :run_time
  results.sort_by! { |r| r[:run_time] }
  @file.write YAML.dump_stream(*results.map(&:to_hash))
end

#yaml_load_documentsObject



39
40
41
42
43
# File 'lib/perfer/store.rb', line 39

def yaml_load_documents
  docs = @file.open { |f| YAML.load_stream(f) }
  docs = docs.documents unless Array === docs
  docs
end