Class: BehaveFun::Task

Inherits:
Object
  • Object
show all
Includes:
TaskSerializer
Defined in:
lib/behave_fun/task.rb

Direct Known Subclasses

Decorator, Tree

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TaskSerializer

#as_json, #dump_status, #name, #restore_status, #serializable_status_fields

Constructor Details

#initialize(params = {}) ⇒ Task

Returns a new instance of Task.



8
9
10
11
12
# File 'lib/behave_fun/task.rb', line 8

def initialize(params = {})
  self.params = params
  @children = []
  @status = :fresh
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/behave_fun/task.rb', line 6

def children
  @children
end

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/behave_fun/task.rb', line 6

def context
  @context
end

#controlObject

Returns the value of attribute control.



5
6
7
# File 'lib/behave_fun/task.rb', line 5

def control
  @control
end

#guardObject

Returns the value of attribute guard.



5
6
7
# File 'lib/behave_fun/task.rb', line 5

def guard
  @guard
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/behave_fun/task.rb', line 5

def params
  @params
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/behave_fun/task.rb', line 6

def status
  @status
end

Class Method Details

.task_nameObject



14
15
16
# File 'lib/behave_fun/task.rb', line 14

def self.task_name
  name.demodulize.underscore
end

Instance Method Details

#add_child(task) ⇒ Object



92
93
94
95
# File 'lib/behave_fun/task.rb', line 92

def add_child(task)
  @children << task
  task.control = self
end

#cancelObject



38
39
40
41
# File 'lib/behave_fun/task.rb', line 38

def cancel
  @status = :cancelled
  children.each { |child| child.cancel }
end

#cancelled?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/behave_fun/task.rb', line 59

def cancelled?
  @status == :cancelled
end

#child_failObject



88
# File 'lib/behave_fun/task.rb', line 88

def child_fail; end

#child_runningObject



90
# File 'lib/behave_fun/task.rb', line 90

def child_running; end

#child_successObject



86
# File 'lib/behave_fun/task.rb', line 86

def child_success; end

#dupObject



119
120
121
122
123
# File 'lib/behave_fun/task.rb', line 119

def dup
  cloned = self.class.new(params)
  children.each { cloned.add_child(_1.dup) }
  cloned
end

#ended?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/behave_fun/task.rb', line 63

def ended?
  succeeded? || failed? || cancelled?
end

#executeObject



84
# File 'lib/behave_fun/task.rb', line 84

def execute; end

#failObject



33
34
35
36
# File 'lib/behave_fun/task.rb', line 33

def fail
  @status = :failed
  control.child_fail if control
end

#failed?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/behave_fun/task.rb', line 55

def failed?
  @status == :failed
end

#fresh?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/behave_fun/task.rb', line 43

def fresh?
  @status == :fresh
end

#guard_passed?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/behave_fun/task.rb', line 67

def guard_passed?
  return true unless guard

  guard.context = context
  guard.reset
  guard.run

  case guard.status
  when :succeeded then true
  when :failed    then false
  else
    raise Error, 'Guard should finish in one step'
  end
end

#resetObject



113
114
115
116
117
# File 'lib/behave_fun/task.rb', line 113

def reset
  cancel
  @status = :fresh
  children.each { |child| child.reset }
end

#runObject

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/behave_fun/task.rb', line 97

def run
  raise Error, 'Cannot run ended task' if ended?
  if fresh?
    if guard_passed?
      start
      running
      execute
    else
      fail
    end
  else
    running
    execute
  end
end

#runningObject



23
24
25
26
# File 'lib/behave_fun/task.rb', line 23

def running
  @status = :running
  control.child_running if control
end

#running?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/behave_fun/task.rb', line 47

def running?
  @status == :running
end

#startObject



82
# File 'lib/behave_fun/task.rb', line 82

def start; end

#succeeded?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/behave_fun/task.rb', line 51

def succeeded?
  @status == :succeeded
end

#successObject



28
29
30
31
# File 'lib/behave_fun/task.rb', line 28

def success
  @status = :succeeded
  control.child_success if control
end