Class Tap::Task
In: lib/tap/task.rb
Parent: App::Api

Tasks are nodes that map to the command line. Tasks provide support for configuration, documentation, and provide helpers to build workflows.

Configuration

Tasks are configurable. By default each task will be configured as specified in the class definition. Configurations may be accessed through config, or through accessors.

  class ConfiguredTask < Tap::Task
    config :one, 'one'
    config :two, 'two'
  end

  t = ConfiguredTask.new
  t.config                     # => {:one => 'one', :two => 'two'}
  t.one                        # => 'one'
  t.one = 'ONE'
  t.config                     # => {:one => 'ONE', :two => 'two'}

Overrides and even unspecified configurations may be provided during initialization. Unspecified configurations do not have accessors.

  t = ConfiguredTask.new(:one => 'ONE', :three => 'three')
  t.config                     # => {:one => 'ONE', :two => 'two', :three => 'three'}
  t.respond_to?(:three)        # => false

Configurations can be validated/transformed using an optional block. Many common blocks are pre-packaged and may be accessed through the class method ‘c’:

  class ValidatingTask < Tap::Task
    # string config validated to be a string
    config :string, 'str', &c.check(String)

    # integer config; string inputs are converted using YAML
    config :integer, 1, &c.yaml(Integer)
  end

  t = ValidatingTask.new
  t.string = 1                 # !> ValidationError
  t.integer = 1.1              # !> ValidationError

  t.integer = "1"
  t.integer == 1               # => true

See the Configurable documentation for more information.

Subclassing

Tasks may be subclassed normally, but be sure to call super as necessary, in particular when overriding the following methods:

  class Subclass < Tap::Task
    class << self
      def inherited(child)
        super
      end
    end

    def initialize(*args)
      super
    end

    def initialize_copy(orig)
      super
    end
  end

Methods

associations   call   enq   exe   log   new   on_complete   parser   process  

Attributes

joins  [R]  An array of joins for self

Public Class methods

[Source]

     # File lib/tap/task.rb, line 99
 99:     def initialize(config={}, app=Tap::App.current)
100:       @app = app
101:       @joins = []
102:       initialize_config(config)
103:     end

[Source]

    # File lib/tap/task.rb, line 77
77:       def parser(app)
78:         opts = super
79:         
80:         # add option to specify a config file

81:         opts.on('--config FILE', 'Specifies a config file') do |config_file|
82:           configs = Configurable::Utils.load_file(config_file, true)
83:           opts.config.merge!(configs)
84:         end
85:         
86:         opts
87:       end

Public Instance methods

Returns the associations array: [nil, joins]

[Source]

     # File lib/tap/task.rb, line 161
161:     def associations
162:       [nil, joins]
163:     end

Call splats the input to process and exists to provide subclasses a way to wrap process behavior.

[Source]

     # File lib/tap/task.rb, line 107
107:     def call(input)
108:       process(*input)
109:     end

Enques self with an array of inputs (directly use app.enq to enque with a non-array input, or override in a subclass).

[Source]

     # File lib/tap/task.rb, line 139
139:     def enq(*args)
140:       app.enq(self, args)
141:     end

Executes self with an array of inputs (directly use app.exe to execute with a non-array input, or override in a subclass).

[Source]

     # File lib/tap/task.rb, line 145
145:     def exe(*args)
146:       app.exe(self, args)
147:     end

Logs the inputs to the application logger (via app.log)

[Source]

     # File lib/tap/task.rb, line 150
150:     def log(action, msg=nil, level=Logger::INFO)
151:       app.log(action, msg, level) { yield }
152:     end

Sets the block as a join for self.

[Source]

     # File lib/tap/task.rb, line 155
155:     def on_complete(&block) # :yields: result

156:       joins << block if block
157:       self
158:     end

The method for processing inputs into outputs. Override this method in subclasses to provide class-specific process logic. The arguments given to enq/exe should correspond to the arguments required by process. The process return is the result passed to joins.

  class TaskWithTwoInputs < Tap::Task
    def process(a, b)
      [b,a]
    end
  end

  results = []
  app = Tap::App.new

  task = TaskWithTwoInputs.new({}, app)
  task.enq(1,2).enq(3,4)
  task.on_complete {|result| results << result }

  app.run
  results                 # => [[2,1], [4,3]]

By default, process simply returns the inputs.

[Source]

     # File lib/tap/task.rb, line 133
133:     def process(*inputs)
134:       inputs
135:     end

[Validate]