Module: ProtoProcessor::Task

Defined in:
lib/proto_processor/task.rb

Defined Under Namespace

Modules: Validations Classes: InvalidTaskError, MissingParametersError

Constant Summary collapse

FAILURE =
'FAILURE'
SUCCESS =
'SUCCESS'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/proto_processor/task.rb', line 26

def self.included(base)
  base.class_eval do
    attr_reader :input, :options, :report, :error
    extend Validations
  end
  #base.extend Validations
end

Instance Method Details

#after_processObject



99
100
101
# File 'lib/proto_processor/task.rb', line 99

def after_process
  true
end

#before_processObject



95
96
97
# File 'lib/proto_processor/task.rb', line 95

def before_process
  true
end

#initialize(args) ⇒ Object

new([input, options, report])

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
# File 'lib/proto_processor/task.rb', line 35

def initialize(args)
  raise ArgumentError, "You must provide an Enumerable object as argument" unless args.respond_to?(:each)
  raise ArgumentError, "You must provide an array with input, options and report" if args.size < 3
  raise ArgumentError, "A task report must be or behave like a Hash" unless args.last.respond_to?(:[]=)
  @input, @options, @report = args[0], args[1].dup, args[2].dup
  @success = false
  @error = nil
end

#processObject

Abstract

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/proto_processor/task.rb', line 91

def process
  raise NotImplementedError, "You need to implement #process in you tasks"
end

#runObject

class HaltedChainError < StandardError

  def message
    "Task not run because previous task failed"
  end
end


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/proto_processor/task.rb', line 50

def run
  begin
    log_halt_task and return false if report[:status] == FAILURE
    run_validations!
    before_process
    process
    report!(:status, SUCCESS)
    @success = true
    after_process
  rescue StandardError => e
    # horrible horrible hack to allow Rspec exceptions to bubble up.
    # we can't rescue them because RSpec is only included when running specs
    raise if e.class.name =~ /Spec/
    report!(:status, FAILURE)
    report!(:error, {:name => e.class.name, :message => e.message})
    @error = e
    ProtoProcessor.logger.error "#{self.class.name}: #{e.class.name} => #{e.message}"
    ProtoProcessor.logger.debug e.backtrace.join("\n")
  end
  [@input, @options, @report]
end

#successful?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/proto_processor/task.rb', line 72

def successful?
  @success
end

#update_input!(new_input) ⇒ Object

Update input so it’s passed to the next task



105
106
107
# File 'lib/proto_processor/task.rb', line 105

def update_input!(new_input)
  @input = new_input
end

#valid?Boolean

Validate in subclasses

Example: def validate

options[:some].nil?

end

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/proto_processor/task.rb', line 82

def valid?
  run_validations!
  true
rescue InvalidTaskError => e
  false
end