Class: BioDSL::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/BioDSL/command.rb

Overview

Command class for initiating and calling commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, lmb, options) ⇒ Command

Constructor for Command objects.

Parameters:

  • name (Symbol)

    Name of command.

  • lmb (Proc)

    Lambda for command callback execution.

  • options (Hash)

    Options hash.



37
38
39
40
41
42
43
# File 'lib/BioDSL/command.rb', line 37

def initialize(name, lmb, options)
  @name       = name
  @lmb        = lmb
  @run_status = 'running'
  @options    = options
  @status     = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/BioDSL/command.rb', line 29

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/BioDSL/command.rb', line 29

def options
  @options
end

#run_statusObject

Returns the value of attribute run_status.



30
31
32
# File 'lib/BioDSL/command.rb', line 30

def run_status
  @run_status
end

#statusObject (readonly)

Returns the value of attribute status.



29
30
31
# File 'lib/BioDSL/command.rb', line 29

def status
  @status
end

Instance Method Details

#calc_deltaBioDSL::Status

Locate all status key pairs <foo>_in and <foo>_out and add a new status key <foo>_delta with the numerical difference.

Returns:

  • (BioDSL::Status)

    returns self.



92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

#calc_time_elapsedBioDSL::Status

Add a key with time_elapsed to the status.

Returns:

  • (BioDSL::Status)

    returns self.



81
82
83
84
85
86
# 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

#call(*args) ⇒ Object

Callback method for executing a Command lambda.

Parameters:

  • args (Array)

    List of arguments used in the callback.



48
49
50
51
52
53
54
55
# 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

#to_sString

Return string representation of a Command object.

Returns:

  • (String)

    With formated command.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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