Class: Perfer::Store
- Inherits:
-
Object
- Object
- Perfer::Store
- Defined in:
- lib/perfer/store.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Class Method Summary collapse
- .all_results_paths ⇒ Object
- .for_session(session) ⇒ Object
- .path_for_bench_file(bench_file) ⇒ Object
- .results_dir ⇒ Object
Instance Method Summary collapse
- #append(result) ⇒ Object
- #delete ⇒ Object
-
#initialize(file) ⇒ Store
constructor
A new instance of Store.
- #load ⇒ Object
- #rewrite ⇒ Object
- #save(results) ⇒ Object
- #yaml_load_documents ⇒ Object
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
#file ⇒ Object (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_paths ⇒ Object
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_dir ⇒ Object
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 |
#delete ⇒ Object
35 36 37 |
# File 'lib/perfer/store.rb', line 35 def delete @file.unlink end |
#load ⇒ Object
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 |
#rewrite ⇒ Object
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_documents ⇒ Object
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 |