module MiGA::Cli::ObjectsHelper

@package MiGA @license Artistic-2.0

Public Instance Methods

add_metadata(obj, cli = self) click to toggle source
# File lib/miga/cli/objects_helper.rb, line 90
def add_metadata(obj, cli = self)
  raise "Unsupported object: #{obj.class}" unless obj.respond_to? :metadata

  cli[:metadata].split(',').each do |pair|
    (k, v) = pair.split('=')
    case v
    when 'true';  v = true
    when 'false'; v = false
    when 'nil';   v = nil
    end
    if k == '_step'
      obj.metadata["_try_#{v}"] ||= 0
      obj.metadata["_try_#{v}"]  += 1
    end
    obj.metadata[k] = v
  end unless cli[:metadata].nil?
  [:type, :name, :user, :description, :comments].each do |k|
    obj.metadata[k] = cli[k] unless cli[k].nil?
  end
  obj
end
load_and_filter_datasets(silent = false) click to toggle source

Load and filter a list of datasets as requested in the CLI If +silent=true+, it allows failures silently

# File lib/miga/cli/objects_helper.rb, line 41
def load_and_filter_datasets(silent = false)
  return @objects[:filtered_datasets] unless @objects[:filtered_datasets].nil?

  say 'Listing datasets'
  ds =
    if !self[:dataset].nil?
      [load_dataset(silent)].compact
    elsif !self[:ds_list].nil?
      File.readlines(self[:ds_list]).map do |i|
        load_dataset(silent, i.chomp)
      end.compact
    else
      load_project.datasets
    end
  k = 0
  n = ds.size
  ds.select! do |d|
    advance('Datasets:', k += 1, n, false)
    o = true
    o &&= (d.is_ref? == self[:ref]) unless self[:ref].nil?
    o &&= (d.is_active? == self[:active]) unless self[:active].nil?
    o &&= (self[:multi] ? d.is_multi? :
          d.is_nonmulti?) unless self[:multi].nil?
    o &&= (not d.metadata[:tax].nil?) &&
          d.metadata[:tax].in?(self[:taxonomy]) unless self[:taxonomy].nil?
    o
  end
  say ''
  ds = ds.values_at(self[:dataset_k] - 1) unless self[:dataset_k].nil?
  @objects[:filtered_datasets] = ds
end
load_dataset(silent = false, name = nil) click to toggle source

Load the dataset defined in the CLI If +silent=true+, it allows failures silently

# File lib/miga/cli/objects_helper.rb, line 21
def load_dataset(silent = false, name = nil)
  if name.nil?
    ensure_par(dataset: '-D')
    name = self[:dataset]
  end
  d = load_project.dataset(name)
  raise "Cannot load dataset: #{self[:dataset]}" if !silent && d.nil?

  return d
end
load_project(name = :project, flag = '-P') click to toggle source

Get the project defined in the CLI by parameter name and flag

# File lib/miga/cli/objects_helper.rb, line 7
def load_project(name = :project, flag = '-P')
  return @objects[name] unless @objects[name].nil?

  ensure_par(name => flag)
  say "Loading project: #{self[name]}"
  @objects[name] = MiGA::Project.load(self[name])
  raise "Cannot load project: #{self[name]}" if @objects[name].nil?

  @objects[name]
end
load_project_or_dataset() click to toggle source

Load an a project or (if defined) a dataset

# File lib/miga/cli/objects_helper.rb, line 34
def load_project_or_dataset
  self[:dataset].nil? ? load_project : load_dataset
end
load_result() click to toggle source
# File lib/miga/cli/objects_helper.rb, line 73
def load_result
  return @objects[:result] unless @objects[:result].nil?

  ensure_par(result: '-r')
  obj = load_project_or_dataset
  if obj.class.RESULT_DIRS[self[:result]].nil?
    klass = obj.class.to_s.gsub(/.*::/, '')
    raise "Unsupported result for #{klass}: #{self[:result]}"
  end
  r = obj.add_result(self[:result], false)
  if r.nil? && !self[:ignore_result_empty]
    raise "Cannot load result: #{self[:result]}"
  end

  @objects[:result] = r
end