class MiGA::Result

The result from a task run. It can be project-wide or dataset-specific.

Attributes

data[R]

Hash with the result metadata

Public Class Methods

RESULT_DIRS() click to toggle source
# File lib/miga/result/base.rb, line 3
def RESULT_DIRS
  @@RESULT_DIRS ||=
    MiGA::Dataset.RESULT_DIRS.merge(MiGA::Project.RESULT_DIRS)
end
create(path, force = false) { || ... } click to toggle source
# File lib/miga/result.rb, line 32
def create(path, force = false, &blk)
  FileUtils.rm(path) if force && File.exist?(path)
  r_pre = self.load(path)
  return r_pre unless r_pre.nil?

  yield
  self.load(path)
end
exist?(path) click to toggle source

Check if the result described by the JSON in path already exists

# File lib/miga/result.rb, line 19
def exist?(path)
  File.exist? path
end
load(path) click to toggle source

Load the result described by the JSON in path. Returns MiGA::Result if it already exists, nil otherwise.

# File lib/miga/result.rb, line 26
def load(path)
  return nil unless MiGA::Result.exist? path

  MiGA::Result.new(path)
end
new(path) click to toggle source

Load or create the MiGA::Result described by the JSON file path

# File lib/miga/result.rb, line 50
def initialize(path)
  @path = File.absolute_path(path)
  MiGA::Result.exist?(@path) ? self.load : create
end

Public Instance Methods

[](k) click to toggle source

Entry with symbol k

# File lib/miga/result.rb, line 102
def [](k)
  data[k.to_sym]
end
[]=(k, v) click to toggle source

Adds value v to entry with symbol k

# File lib/miga/result.rb, line 108
def []=(k, v)
  data[k.to_sym] = v
end
add_file(k, file) click to toggle source

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.

# File lib/miga/result.rb, line 116
def add_file(k, file)
  k = k.to_sym
  @data[:files] ||= {}
  @data[:files][k] = file if File.exist? File.expand_path(file, dir)
  @data[:files][k] = "#{file}.gz" if
    File.exist? File.expand_path("#{file}.gz", dir)
end
add_files(files) click to toggle source

#add_file for each key-value pair in the files Hash

# File lib/miga/result.rb, line 126
def add_files(files)
  files.each { |k, v| add_file(k, v) }
end
clean!() click to toggle source

Register the result as cleaned

# File lib/miga/result.rb, line 63
def clean!
  self[:clean] = true
end
clean?() click to toggle source

Is the result clean? Returns Boolean

# File lib/miga/result.rb, line 57
def clean?
  !!self[:clean]
end
create() click to toggle source

Initialize and save empty result

# File lib/miga/result.rb, line 132
def create
  @data = { created: Time.now.to_s, stats: {}, files: {} }
  save
end
dir() click to toggle source

Directory containing the result

# File lib/miga/result.rb, line 85
def dir
  File.dirname(path)
end
each_file(&blk) click to toggle source

Iterate blk for each registered file. Depending on the number of arguments of blk (arity), it's called as:

  • blk

  • blk[file_sym, file_rel]

  • blk[file_sym, file_rel, file_abs]

Note that multiple files may have the same symbol (file_sym), since arrays of files are supported.

# File lib/miga/result.rb, line 181
def each_file(&blk)
  return to_enum(:each_file) unless block_given?

  @data[:files] ||= {}
  self[:files].each do |k, files|
    files = [files] unless files.kind_of? Array
    files.each do |file|
      case blk.arity
      when 1; blk.call(file)
      when 2; blk.call(k, file)
      when 3; blk.call(k, file, File.expand_path(file, dir))
      else; raise "Wrong number of arguments: #{blk.arity} for 1..3"
      end
    end
  end
end
file_path(k) click to toggle source

Absolute path to the file(s) defined by symbol k

# File lib/miga/result.rb, line 91
def file_path(k)
  k = k.to_sym
  f = self[:files].nil? ? nil : self[:files][k]
  return nil if f.nil?
  return File.expand_path(f, dir) unless f.is_a? Array

  f.map { |fi| File.expand_path(fi, dir) }
end
load() click to toggle source

Load (or reload) result data in the JSON file path

# File lib/miga/result.rb, line 152
def load
  @data = MiGA::Json.parse(path)
  @data[:files] ||= {}
end
path(which = :json) click to toggle source

Path to the standard files of the result. which must be one of:

  • :json (default) : JSON file describing the result.

  • :start : File with the date when the processing started.

  • :done : File with the date when the processing ended.

# File lib/miga/result.rb, line 72
def path(which = :json)
  case which.to_sym
  when :json
    @path
  when :start
    @path.sub(/\.json$/, '.start')
  when :done
    @path.sub(/\.json$/, '.done')
  end
end
remove!() click to toggle source

Remove result, including all associated files

# File lib/miga/result.rb, line 159
def remove!
  each_file { |file| FileUtils.rm_rf(File.join(dir, file)) }
  unlink
end
save() click to toggle source

Save the result persistently (in the JSON file path)

# File lib/miga/result.rb, line 139
def save
  @data[:updated] = Time.now.to_s
  s = path(:start)
  if File.exist? s
    @data[:started] = File.read(s).chomp
    File.unlink s
  end
  MiGA::Json.generate(data, path)
  self.load
end