Module: MiGA::Common::WithResult
- Included in:
- Dataset::Result, Project::Result
- Defined in:
- lib/miga/common/with_result.rb
Overview
Helper module including specific functions to handle objects that have results. The class including this module must implement methods .RESULT_DIRS
, #ignore_task?
, #metadata
, #project
, and #inactivate!
.
Instance Method Summary collapse
-
#add_result(task, save = true, opts = {}) ⇒ Object
Look for the result with symbol key
task
and register it in the object. -
#each_result ⇒ Object
For each result execute the 2-ary block: key symbol and MiGA::Result.
-
#get_result(task) ⇒ Object
Get a result as MiGA::Result for the object with key
task
. -
#next_task(tasks = nil, save = false) ⇒ Object
Get the next task from
tasks
, saving intermediate results ifsave
. -
#recalculate_tasks(reason = nil) ⇒ Object
Mark all results for recalculation.
-
#result(task) ⇒ Object
Get the result MiGA::Result in this object identified by the symbol
task
. -
#result_dirs ⇒ Object
Result directories as a Hash.
-
#results ⇒ Object
Get all the results (Array of MiGA::Result) in this object.
Instance Method Details
#add_result(task, save = true, opts = {}) ⇒ Object
Look for the result with symbol key task
and register it in the object. If save
is false, it doesn’t register the result, but it still returns a result if it already exists.
The opts
hash controls result creation (if necessary). Supported values include:
-
is_clean
: A Boolean indicating if the input files are clean -
force
: A Boolean indicating if the result must be re-indexed, ignored unless save = true
Returns MiGA::Result or nil
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/miga/common/with_result.rb', line 25 def add_result(task, save = true, opts = {}) task = task.to_sym return nil if result_dirs[task].nil? base = File.join(project.path, 'data', result_dirs[task], result_base) json = "#{base}.json" return MiGA::Result.load(json) unless save MiGA::Result.create(json, opts[:force]) do r = send("add_result_#{task}", base, opts) if File.exist?("#{base}.done") unless r.nil? r.save pull_hook(:on_result_ready, r.key) end end end |
#each_result ⇒ Object
For each result execute the 2-ary block: key symbol and MiGA::Result
61 62 63 |
# File 'lib/miga/common/with_result.rb', line 61 def each_result results.each { |res| yield(res.key, res) } end |
#get_result(task) ⇒ Object
Get a result as MiGA::Result for the object with key task
. This is equivalent to add_result(task, false).
68 69 70 |
# File 'lib/miga/common/with_result.rb', line 68 def get_result(task) add_result(task, false) end |
#next_task(tasks = nil, save = false) ⇒ Object
Get the next task from tasks
, saving intermediate results if save
. If tasks
is nil
(default), it uses the entire list of tasks. Returns a Symbol.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/miga/common/with_result.rb', line 76 def next_task(tasks = nil, save = false) tasks ||= result_dirs.keys tasks.find do |t| if ignore_task?(t) # Do not run if this step is to be ignored false else res = add_result(t, save) if res.nil? # Run if the step has not been calculated, # unless too many attempts were already made cur_try = ["_try_#{t}"] || 0 if cur_try > project.option(:max_try) inactivate! "Too many errors in step #{t}" false else true end else # Run if the step is ready but has to be recalculated res.recalculate? ? true : false end end end end |
#recalculate_tasks(reason = nil) ⇒ Object
Mark all results for recalculation
104 105 106 |
# File 'lib/miga/common/with_result.rb', line 104 def recalculate_tasks(reason = nil) each_result { |_k, res| res.recalculate!(reason).save } end |
#result(task) ⇒ Object
Get the result MiGA::Result in this object identified by the symbol task
44 45 46 47 48 49 50 51 |
# File 'lib/miga/common/with_result.rb', line 44 def result(task) task = task.to_sym return nil if result_dirs[task].nil? MiGA::Result.load( "#{project.path}/data/#{result_dirs[task]}/#{result_base}.json" ) end |
#result_dirs ⇒ Object
Result directories as a Hash
9 10 11 |
# File 'lib/miga/common/with_result.rb', line 9 def result_dirs self.class.RESULT_DIRS end |
#results ⇒ Object
Get all the results (Array of MiGA::Result) in this object
55 56 57 |
# File 'lib/miga/common/with_result.rb', line 55 def results result_dirs.keys.map { |k| result k }.compact end |