class BioDSL::Command
Command
class for initiating and calling commands.
Attributes
Public Class Methods
Constructor for Command
objects.
@param name [Symbol] Name of command. @param lmb [Proc] Lambda for command callback execution. @param options [Hash] Options hash.
# File lib/BioDSL/command.rb, line 37 def initialize(name, lmb, options) @name = name @lmb = lmb @run_status = 'running' @options = options @status = {} end
Public Instance Methods
Locate all status key pairs <foo>_in and <foo>_out and add a new status key <foo>_delta with the numerical difference.
@return [BioDSL::Status] returns self.
# File lib/BioDSL/command.rb, line 92 def calc_delta @status.keys.select { |s| s[-3..-1] == '_in' }.each do |in_key| base = in_key[0...-3] out_key = "#{base}_out".to_sym next unless @status.key? out_key @status["#{base}_delta".to_sym] = delta(in_key, out_key) @status["#{base}_delta_percent".to_sym] = delta_percent(in_key, out_key) end self end
Add a key with time_elapsed to the status.
@return [BioDSL::Status] returns self.
# File lib/BioDSL/command.rb, line 81 def calc_time_elapsed delta = @status[:time_stop] - @status[:time_start] @status[:time_elapsed] = (Time.mktime(0) + delta).strftime('%H:%M:%S') self end
Callback method for executing a Command
lambda.
@param args [Array] List of arguments used in the callback.
# File lib/BioDSL/command.rb, line 48 def call(*args) @lmb.call(*args, @status) @run_status = 'done' @status[:time_stop] = Time.now calc_time_elapsed calc_delta end
Return string representation of a Command
object.
@return [String] With formated command.
# File lib/BioDSL/command.rb, line 60 def to_s options_list = [] @options.each do |key, value| options_list << case value.class.to_s when 'String' value = Regexp.quote(value) if key == :delimiter %(#{key}: "#{value}") when 'Symbol' "#{key}: :#{value}" else "#{key}: #{value}" end end @options.empty? ? @name : "#{@name}(#{options_list.join(', ')})" end
Private Instance Methods
Calculate the difference between status values given two status keys.
@param in_key [Symbol] Status hash key. @param out_key [Symbol] Status hash key.
@return [Fixnum] Difference.
# File lib/BioDSL/command.rb, line 114 def delta(in_key, out_key) @status[out_key] - @status[in_key] end
Calculate the percent difference between status values given two status keys.
@param in_key [Symbol] Status hash key. @param out_key [Symbol] Status hash key.
@return [Float] Percentage rounded to 2 decimals.
# File lib/BioDSL/command.rb, line 125 def delta_percent(in_key, out_key) d = @status[out_key] - @status[in_key] return 0.0 if d == 0 (100 * d.to_f / [@status[out_key], @status[in_key]].max).round(2) end