module Libis::Workflow::Status
Constants
- STATUS
- STATUS_TEXT
Public Instance Methods
Check status of the object.
@param [Symbol] state status to look for @param [String] task name of task whose status to check @return [Boolean] true if the object status matches
# File lib/libis/workflow/status.rb, line 74 def check_status(state, task = nil) self.status(task) == state end
Compare status with current status of the object.
@param [Symbol] state @return [Integer] 1, 0 or -1 depending on which status is higher in rank
# File lib/libis/workflow/status.rb, line 82 def compare_status(state, task = nil) STATUS[self.status(task)] <=> STATUS[state] end
Changes the status of the object. The status changed is logged in the status_log with the current timestamp.
@param [String] task namepath of the task @param [Symbol] status status to set
# File lib/libis/workflow/status.rb, line 31 def set_status(task, status) task = task.namepath if task.is_a?(Libis::Workflow::Task) log_entry = self.status_entry(task) if log_entry.nil? || STATUS[status_symbol(log_entry['status'])] > STATUS[status_symbol(status)] log_entry = self.add_status_log('task' => task, 'status' => status, 'created' => DateTime.now) end log_entry['status'] = status log_entry['updated'] = DateTime.now self.save_log_entry(log_entry) end
Get last known status symbol for a given task
@param [String] task task name to check item status for @return [Symbol] the status code
# File lib/libis/workflow/status.rb, line 46 def status(task = nil) entry = self.status_entry(task) status_symbol(entry['status']) rescue :NOT_STARTED end
Gets the last known status label of the object.
@param [String] task name of task to get the status for @return [String] status label ( = task name + status )
# File lib/libis/workflow/status.rb, line 64 def status_label(task = nil) entry = self.status_entry(task) "#{entry['task'] rescue nil}#{entry['status'].capitalize rescue nil}" end
Update the progress of the working task @param [String] task namepath of the task @param [Integer] progress progress indicator (as <progress> of <max> or as % if <max> not set). Default: 0 @param [Integer] max max count.
# File lib/libis/workflow/status.rb, line 90 def status_progress(task, progress = nil, max = nil) log_entry = self.status_entry(task) if log_entry.nil? || STATUS[status_symbol(log_entry['status'])] >= STATUS[:DONE] log_entry = self.add_status_log('task' => task, 'status' => :STARTED, 'created' => DateTime.now) end log_entry['progress'] = progress ? progress : (log_entry['progress'] || 0) + 1 log_entry['max'] = max if max log_entry['updated'] = DateTime.now self.save_log_entry(log_entry) end
Get last known status text for a given task
@param [String] task task name to check item status for @return [Symbol] the status code
# File lib/libis/workflow/status.rb, line 55 def status_text(task = nil) entry = self.status_entry(task) status_string(entry['status']) rescue STATUS_TEXT.first end
Protected Instance Methods
Get last known status entry for a given task
@param [String] task task name to check item status for @return [Hash] the status entry
# File lib/libis/workflow/status.rb, line 107 def status_entry(task = nil) task = task.namepath if task.is_a?(Libis::Workflow::Task) return self.status_log.last if task.blank? self.status_log.select { |entry| entry['task'] == task }.last end
# File lib/libis/workflow/status.rb, line 125 def status_string(x) return STATUS_TEXT[x] if x.is_a?(Integer) return STATUS_TEXT[STATUS[x]] if STATUS.has_key?(x) x = x.to_s.upcase.to_sym STATUS.has_key?(x) ? STATUS_TEXT[STATUS[x]] : 'unknown status' end
Convert String, Symbol or Integer to correct symbol for the status. If the input value is nil, the fist status entry is returned.
@param [String|Symbol|Integer] x string, symbol or integer for status code. @return [Symbol] the corresponding STATUS
symbol
# File lib/libis/workflow/status.rb, line 118 def status_symbol(x) return STATUS.key(x) if x.is_a?(Integer) return x if STATUS.has_key?(x) x = x.to_s.upcase.to_sym STATUS.has_key?(x) ? x : nil end