Class: Swiftcore::Tasks::Task

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/swiftcore/tasks/task.rb

Overview

This is a sortable, orderable container for a #call()able object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order = 0, &task) ⇒ Task

Returns a new instance of Task.



12
13
14
15
16
17
# File 'lib/swiftcore/tasks/task.rb', line 12

def initialize(order = 0, &task)
  @order = order
  self.task = task
  @status = :pending
  @result = nil
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



9
10
11
# File 'lib/swiftcore/tasks/task.rb', line 9

def order
  @order
end

#resultObject

Returns the value of attribute result.



9
10
11
# File 'lib/swiftcore/tasks/task.rb', line 9

def result
  @result
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/swiftcore/tasks/task.rb', line 9

def status
  @status
end

#taskObject

Returns the value of attribute task.



10
11
12
# File 'lib/swiftcore/tasks/task.rb', line 10

def task
  @task
end

#taskordObject (readonly)

Returns the value of attribute taskord.



10
11
12
# File 'lib/swiftcore/tasks/task.rb', line 10

def taskord
  @taskord
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  if less_than(other)
    -1
  elsif greater_than(other)
    1
  else
    0
  end
end

#call(*args) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/swiftcore/tasks/task.rb', line 44

def call(*args)
  @status = :running
  @result = @task&.call(*args)
  @status = :finished
  @result
rescue StandardError => e
  @status = e
end

#greater_than(other) ⇒ Object



29
30
31
32
# File 'lib/swiftcore/tasks/task.rb', line 29

def greater_than(other)
  (@order > other.order) ||
    (@order == other.order && @taskord > other.taskord)
end

#less_than(other) ⇒ Object



24
25
26
27
# File 'lib/swiftcore/tasks/task.rb', line 24

def less_than(other)
  (@order < other.order) ||
    (@order == other.order && @taskord < other.taskord)
end