module Libis::Workflow::Base::Workflow

This is the base module for Workflows.

This module lacks the implementation for the data attributes. It functions as an interface that describes the common functionality regardless of the storage implementation. These attributes require some implementation:

These values should be set by calling the configure method which takes a Hash as argument with :name, :description, :input and :tasks keys.

A minimal in-memory implementation could be:

class Workflow

include ::Libis::Workflow::Base::Workflow

attr_accessor :name, :description, :config

def initialize
  @name = ''
  @description = ''
  @config = Hash.new
end

end

Public Class Methods

included(base) click to toggle source
# File lib/libis/workflow/base/workflow.rb, line 74
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

configure(cfg) click to toggle source
# File lib/libis/workflow/base/workflow.rb, line 78
def configure(cfg)
  cfg.key_symbols_to_strings!(recursive: true)
  self.name = cfg.delete('name') || self.class.name
  self.description = cfg.delete('description') || ''
  self.config['input'] = {}
  self.config['tasks'] = []
  self.config.merge! cfg

  self.class.require_all

  self.config
end
input() click to toggle source
# File lib/libis/workflow/base/workflow.rb, line 91
def input
  self.config.key_strings_to_symbols(recursive: true)[:input].inject({}) do |hash, input_def|
    name = input_def.first
    default = input_def.last[:default]
    parameter = ::Libis::Tools::Parameter.new name, default
    input_def.last.each { |k, v| parameter[k] = v }
    hash[name] = parameter
    hash
  end
rescue => _e
  {}
end
instantize_task(parent, cfg) click to toggle source
# File lib/libis/workflow/base/workflow.rb, line 137
def instantize_task(parent, cfg)
  task_class = Libis::Workflow::TaskGroup
  task_class = cfg['class'].constantize if cfg['class']
  # noinspection RubyArgCount
  task_instance = task_class.new(parent, cfg)
  cfg['tasks'] && cfg['tasks'].map do |task_cfg|
    task_instance << instantize_task(task_instance, task_cfg)
  end
  task_instance
end
prepare_input(options) click to toggle source

@param [Hash] options

# File lib/libis/workflow/base/workflow.rb, line 105
def prepare_input(options)
  options = options.key_strings_to_symbols
  result = {}
  self.input.each do |key, parameter|
    value = nil
    if options.has_key?(key)
      value = parameter.parse(options[key])
    elsif !parameter[:default].nil?
      value = parameter[:default]
    else
      next
    end
    propagate_to = []
    propagate_to = parameter[:propagate_to] if parameter[:propagate_to].is_a? Array
    propagate_to = parameter[:propagate_to].split(/[\s,;]+/) if parameter[:propagate_to].is_a? String
    result[key] = value if propagate_to.empty?
    propagate_to.each do |target|
      task_name, param_name = target.split('#')
      param_name ||= key.to_s
      result[task_name] ||= {}
      result[task_name][param_name] = value
    end
  end
  result
end
tasks(parent = nil) click to toggle source
# File lib/libis/workflow/base/workflow.rb, line 131
def tasks(parent = nil)
  self.config['tasks'].map do |cfg|
    instantize_task(parent || nil, cfg)
  end
end