class LIBIS::Workflow::Workflow

Attributes

config[R]
tasks[R]
workitem[R]

Public Class Methods

new(config) click to toggle source

@param [Hash] config Workflow configuration

# File lib/libis/workflow/workflow.rb, line 22
def initialize(config)

  Config.require_all(File.join(File.dirname(__FILE__), 'tasks'))
  Config.require_all(Config.taskdir)
  Config.require_all(Config.itemdir)

  @config = {input: [], tasks: [], start_object: '::LIBIS::Workflow::WorkItem'}.merge config

  @name = config[:name] || self.class.name

  unless @config[:tasks].last[:class] && @config[:tasks].last[:class].split('::').last == 'Analyzer'
    @config[:tasks] << { class: '::LIBIS::Workflow::Tasks::Analyzer' }
  end

  @tasks = []
  @config[:tasks].each do |m|
    task_class = Task
    task_class    = m[:class].constantize if m[:class]
    task_instance = task_class.new nil, m.symbolize_keys!
    @tasks << {
        class:    task_class,
        instance: task_instance
    }
  end

  @inputs = @config[:input]

end

Public Instance Methods

inputs_required() click to toggle source
# File lib/libis/workflow/workflow.rb, line 73
def inputs_required
  (@inputs || {}).reject { |_, input| input.has_key?(:default) }
end
run(opts = {}) click to toggle source

@param [Hash] opts

# File lib/libis/workflow/workflow.rb, line 52
def run(opts = {})

  @workitem = @config[:start_object].constantize.new
  raise RuntimeError.new "Could not create instance of start object '#{@config[:start_object]}'" unless workitem

  workitem.workflow = self
  workitem.save

  process_options opts

  check_item_type WorkItem

  @tasks.each do |m|
    next if workitem.failed? and not m[:instance].options[:allways_run]
    m[:instance].run(workitem)
  end

  workitem.set_status :DONE unless workitem.failed?

end

Private Instance Methods

prepare_input(opts, inputs) click to toggle source

@param [Hash] opts @param [Array] inputs

# File lib/libis/workflow/workflow.rb, line 89
def prepare_input(opts, inputs)
  options = opts.symbolize_keys
  interactive = options.delete :interactive
  (inputs || {}).each do |key, input|
    # provided in opts
    unless options.has_key? key
      if input.has_key? :default
        # not provided in opts, but default exists
        options[key] = input[:default]
      else
        raise StandardError.new "input option '#{input[:name]}' has no value." unless interactive
        # ask user
        puts input[:description] if input[:description]
        print "#{input[:name] || key.to_s} : "
        value = STDIN.gets.strip
        options[key] = value
      end
    end
    case input[:type]
      when 'Time'
        options[key] = s_to_time options[key]
      when 'Boolean'
        options[key] = %w'true yes t y 1'.include? options[key].downcase if options[key].is_a?(String)
      else
        options[key].gsub!('%s',Time.now.strftime('%Y%m%d%H%M%S')) if options[key].is_a? String
    end
  end
  options
end
process_options(opts) click to toggle source

@param [Hash] opts

# File lib/libis/workflow/workflow.rb, line 80
def process_options(opts)
  options = opts.dup
  @action = options.delete(:action) || 'start'
  options = prepare_input options, @inputs
  options.each { |k,v| workitem.options[k.to_sym] = v }
end
s_to_time(str) click to toggle source

@param [String] str @return [Time]

# File lib/libis/workflow/workflow.rb, line 121
def s_to_time(str)
  d = str.split %r'[/ :.-]'
  Time.new *d
end