Sha256: db1c29d9e32731eec52e89240d5e8621c5d3b337463ea89c49576ac661f357e3

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

# encoding: UTF-8

require 'anvil/task/class_methods'
require 'anvil/task/naming'
require 'anvil/task/options'

module Anvil
  # Common class for all tasks
  class Task
    extend ClassMethods
    extend Naming
    extend Options

    attr_reader :options

    def initialize(options = {})
      @options = options
    end

    # Runs a task and its callbacks if the assures are OK
    #
    # @return [Object, nil] anything the task might return
    def run
      if run_assures
        run_before_callbacks
        task_return_value = run_task
        run_after_callbacks

        task_return_value
      end
    end

    def logger
      Anvil.logger
    end

    protected

    def run_after_callbacks
      self.class.afters.each do |after|
        after[0].new(after[1]).run
      end
    end

    def run_assures
      self.class.assures.map { |a| a.new.assured? }.all?
    end

    def run_before_callbacks
      self.class.befores.each do |before|
        before[0].new(before[1]).run
      end
    end

    def run_task
      task
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anvil-core-0.3.2 lib/anvil/task.rb
anvil-core-0.3.1 lib/anvil/task.rb
anvil-core-0.3.0 lib/anvil/task.rb
anvil-core-0.2.0 lib/anvil/task.rb
anvil-core-0.1.0 lib/anvil/task.rb