Class: RWF::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/rwf/flow.rb

Constant Summary collapse

ALLOWED_OPTIONS =
i[type cure ptr on_success on_error].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(io_params = {}, params = nil) ⇒ Object



32
33
34
# File 'lib/rwf/flow.rb', line 32

def call(io_params = {}, params = nil)
  new.(io_params, params)
end

.cure(*args) ⇒ Object



24
25
26
# File 'lib/rwf/flow.rb', line 24

def cure(*args)
  error(*args, cure: true)
end

.error(*args) ⇒ Object



28
29
30
# File 'lib/rwf/flow.rb', line 28

def error(*args)
  task(*args, type: :error)
end

.task(*args) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rwf/flow.rb', line 12

def task(*args)
  task, *options = *args
  task_options = {}
  options.each { |option| task_options.merge!(option) }
  task_options[:ptr] ||= task if task.is_a?(Symbol)

  extra_options = task_options.keys - ALLOWED_OPTIONS
  raise ConfigError, "Unknown task option(s): #{extra_options.join(',')}." unless extra_options.empty?

  tasks << [task, task_options]
end

.tasksObject



8
9
10
# File 'lib/rwf/flow.rb', line 8

def tasks
  @tasks ||= []
end

Instance Method Details

#call(io_params = {}, params = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rwf/flow.rb', line 41

def call(io_params = {}, params = nil)
  result = Result.new(io_params)
  next_type = nil
  next_ptr = nil
  next_index = 0

  while next_index < tasks.size
    task, options = tasks[next_index]

    if options[:type] == next_type || next_ptr
      next_type, next_ptr = execute_task(result, task, options, io_params, params)
      break if next_ptr == :end
    end

    next_index = decide_index(next_index, next_ptr)
  end

  result.initial? ? result.success! : result
end

#tasksObject



37
38
39
# File 'lib/rwf/flow.rb', line 37

def tasks
  self.class.tasks
end