Class: MiGA::Result
Overview
The result from a task run. It can be project-wide or dataset-specific.
Constant Summary
Constants included from MiGA
CITATION, VERSION, VERSION_DATE, VERSION_NAME
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Hash with the result metadata.
-
#path ⇒ Object
readonly
Path to the JSON file describing the result.
-
#results ⇒ Object
readonly
Array of MiGA::Result objects nested within the result (if any).
Class Method Summary collapse
-
.exist?(path) ⇒ Boolean
Check if the result described by the JSON in
path
already exists. -
.load(path) ⇒ Object
Load the result described by the JSON in
path
.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Entry with symbol
k
. -
#add_file(k, file) ⇒ Object
Register
file
(path relative to #dir) with the symbolk
. -
#add_files(files) ⇒ Object
#add_file for each key-value pair in the
files
Hash. -
#add_result(result) ⇒ Object
Add the MiGA::Result
result
as part of the current result. -
#create ⇒ Object
Initialize and #save empty result.
-
#dir ⇒ Object
Directory containing the result.
-
#each_file(&blk) ⇒ Object
Iterate
blk
for each registered file. -
#file_path(k) ⇒ Object
Absolute path to the file(s) defined by symbol
k
. -
#initialize(path) ⇒ Result
constructor
Load or create the MiGA::Result described by the JSON file
path
. -
#load ⇒ Object
Load (or reload) result data in the JSON file #path.
-
#remove! ⇒ Object
Remove result, including all associated files.
-
#save ⇒ Object
Save the result persistently (in the JSON file #path).
Methods inherited from MiGA
CITATION, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, initialized?, #result_files_exist?, root_path, tabulate
Constructor Details
Instance Attribute Details
#data ⇒ Object (readonly)
Hash with the result metadata.
32 33 34 |
# File 'lib/miga/result.rb', line 32 def data @data end |
#path ⇒ Object (readonly)
Path to the JSON file describing the result.
28 29 30 |
# File 'lib/miga/result.rb', line 28 def path @path end |
#results ⇒ Object (readonly)
Array of MiGA::Result objects nested within the result (if any).
36 37 38 |
# File 'lib/miga/result.rb', line 36 def results @results end |
Class Method Details
.exist?(path) ⇒ Boolean
Check if the result described by the JSON in path
already exists.
12 13 14 |
# File 'lib/miga/result.rb', line 12 def self.exist?(path) !!(File.size? path) end |
Instance Method Details
#[](k) ⇒ Object
Entry with symbol k
.
63 |
# File 'lib/miga/result.rb', line 63 def [](k) data[k.to_sym] ; end |
#add_file(k, file) ⇒ Object
Register file
(path relative to #dir) with the symbol k
. If the file doesn’t exist but the .gz extension does, the gzipped file is registered instead. If neither exists, nothing is registered.
69 70 71 72 73 74 75 |
# File 'lib/miga/result.rb', line 69 def add_file(k, file) k = k.to_sym @data[:files] ||= {} @data[:files][k] = file if File.exist? File.(file, dir) @data[:files][k] = file + ".gz" if File.exist? File.(file + ".gz", dir) end |
#add_files(files) ⇒ Object
#add_file for each key-value pair in the files
Hash.
79 80 81 |
# File 'lib/miga/result.rb', line 79 def add_files(files) files.each { |k, v| add_file(k, v) } end |
#add_result(result) ⇒ Object
Add the MiGA::Result result
as part of the current result.
148 149 150 151 |
# File 'lib/miga/result.rb', line 148 def add_result(result) @data[:results] << result.path save end |
#create ⇒ Object
Initialize and #save empty result.
85 86 87 88 |
# File 'lib/miga/result.rb', line 85 def create @data = {:created=>Time.now.to_s, :results=>[], :stats=>{}, :files=>{}} save end |
#dir ⇒ Object
Directory containing the result.
47 48 49 |
# File 'lib/miga/result.rb', line 47 def dir File.dirname(path) end |
#each_file(&blk) ⇒ Object
Iterate blk
for each registered file. If blk
calls for one argument, the relative path to the file is passed. If it calls for two arguments, the symbol describing the file is passed first and the path second. Note that multiple files may have the same symbol, since arrays of files are supported.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/miga/result.rb', line 130 def each_file(&blk) @data[:files] ||= {} self[:files].each do |k,files| files = [files] unless files.kind_of? Array files.each do |file| if blk.arity==1 blk.call(file) elsif blk.arity==2 blk.call(k, file) else raise "Wrong number of arguments: #{blk.arity} for one or two" end end end end |
#file_path(k) ⇒ Object
Absolute path to the file(s) defined by symbol k
.
53 54 55 56 57 58 59 |
# File 'lib/miga/result.rb', line 53 def file_path(k) k = k.to_sym f = self[:files].nil? ? nil : self[:files][k] return nil if f.nil? return File.(f, dir) unless f.is_a? Array f.map{ |fi| File.(fi, dir) } end |
#load ⇒ Object
Load (or reload) result data in the JSON file #path.
103 104 105 106 107 108 |
# File 'lib/miga/result.rb', line 103 def load json = File.read(path) @data = JSON.parse(json, {:symbolize_names=>true}) @data[:files] ||= {} @results = self[:results].map{ |rs| MiGA::Result.new rs } end |
#remove! ⇒ Object
Remove result, including all associated files.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/miga/result.rb', line 112 def remove! each_file do |file| f = File.(file, dir) FileUtils.rm_rf(f) if File.exist? f end %w(.start .done).each do |ext| f = path.sub(/\.json$/, ext) File.unlink f if File.exist? f end File.unlink path end |
#save ⇒ Object
Save the result persistently (in the JSON file #path).
92 93 94 95 96 97 98 99 |
# File 'lib/miga/result.rb', line 92 def save @data[:updated] = Time.now.to_s json = JSON.pretty_generate data ofh = File.open(path, "w") ofh.puts json ofh.close self.load end |