Class: BioDSL::Pipeline

Inherits:
Object
  • Object
show all
Includes:
EmailHelper, HistoryHelper, LogHelper, OptionsHelper, StatusHelper
Defined in:
lib/BioDSL/pipeline.rb

Overview

Pipeline class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StatusHelper

#status_init, #status_progress

Methods included from HistoryHelper

#save_history

Methods included from LogHelper

#log_error, #log_ok

Methods included from EmailHelper

#compose_mail, #send_email

Constructor Details

#initializePipeline

Pipeline class constructor.



54
55
56
57
58
59
# File 'lib/BioDSL/pipeline.rb', line 54

def initialize
  @commands = []      # Array of Commands in the Pipeline.
  @options  = {}      # Options hash.
  @enums    = [[]]    # Array of Enumerators.
  @complete = false   # Flag denoting if run was completed.
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ self (private)

Add a command to the pipeline. This is done by first requiring the relevant Class/Module and then calling the relevant command.

Examples:

Here we add the command ‘dump` to the pipeline.

Pipeline.new.dump
  # => self

Parameters:

  • method (Symbol)

    Method name.

  • args (Array)

    Method arguments.

  • block (Proc)

    Method block.

Returns:

  • (self)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/BioDSL/pipeline.rb', line 171

def method_missing(method, *args, &block)
  require_file(method)

  const = method.to_s.split('_').map(&:capitalize).join('')

  if BioDSL.const_defined? const
    options = args.first || {}
    options_load_rc(options, method)

    klass = BioDSL.const_get(const)
    klass.send(:include, OptionsHelper)
    klass.send(:include, StatusHelper)
    lmb = klass.send(:new, options).lmb

    @commands << Command.new(method, lmb, options)
  else
    super
  end

  self
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



51
52
53
# File 'lib/BioDSL/pipeline.rb', line 51

def commands
  @commands
end

#completeObject

Returns the value of attribute complete.



51
52
53
# File 'lib/BioDSL/pipeline.rb', line 51

def complete
  @complete
end

Instance Method Details

#+(other) ⇒ Object

Method that adds two Pipelines and return a new Pipeline.



79
80
81
82
83
84
85
86
87
# File 'lib/BioDSL/pipeline.rb', line 79

def +(other)
  unless other.is_a?(BioDSL::Pipeline)
    fail PipelineError, "Not a pipeline: #{other.inspect}"
  end

  p = self.class.new
  p << self
  p << other
end

#<<(other) ⇒ self

Method for merging one pipeline onto another.

Parameters:

  • other (Pipeline)

    Pipeline to merge.

Returns:

  • (self)

    .



71
72
73
74
75
76
# File 'lib/BioDSL/pipeline.rb', line 71

def <<(other)
  other.commands.map { |command| commands << command }
  other.status.map   { |status|  self.status << status }

  self
end

#popObject

Removes last command from a Pipeline and returns a new Pipeline with this command.



91
92
93
94
95
# File 'lib/BioDSL/pipeline.rb', line 91

def pop
  p = BioDSL::Pipeline.new
  p.commands = [@commands.pop]
  p
end

#run(options = {}) ⇒ self

Run all the commands in the Pipeline.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :verbose (Boolean) — default: false

    Enable verbose output.

Returns:

  • (self)

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/BioDSL/pipeline.rb', line 105

def run(options = {})
  prime_variables(options)

  fail BioDSL::PipelineError, 'Empty pipeline' if @commands.empty?

  @options = options

  check_options
  command_runner
  print_status
  send_email(self)
  save_report
  log_ok

  self
rescue => exception
  exit_gracefully(exception)
ensure
  save_history
end

#sizeInteger

Returns The size or number of commands in a pipeline.

Returns:

  • (Integer)

    The size or number of commands in a pipeline.



62
63
64
# File 'lib/BioDSL/pipeline.rb', line 62

def size
  @commands.size
end

#statusArray

Return a list of all status hashes from the commands.

Returns:

  • (Array)

    List of status hashes.



129
130
131
132
133
134
135
136
137
138
# File 'lib/BioDSL/pipeline.rb', line 129

def status
  @commands.each_with_object([]) do |e, a|
    if @complete
      e.calc_time_elapsed
      e.calc_delta
    end

    a << e.status
  end
end

#to_sObject

Format a Pipeline to a pretty string which is returned.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/BioDSL/pipeline.rb', line 141

def to_s
  command_strings = %w(BD new)

  @commands.each { |command| command_strings << command.to_s }

  if @complete
    if @options.empty?
      command_strings << 'run'
    else
      command_strings << "run(#{options_string})"
    end
  end

  command_strings.join('.')
end