Module: LucidOperation::PromiseRun

Defined in:
lib/isomorfeus_operation/lucid_operation/promise_run.rb

Instance Method Summary collapse

Instance Method Details

#promise_runObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/isomorfeus_operation/lucid_operation/promise_run.rb', line 3

def promise_run
  promise = Promise.new
  original_promise = promise
  operation = self

  # steps
  self.class.gherkin[:steps].each do |gherkin_step|
    matched = false
    self.class.steps.each do |step|
      # step[0] -> regular_expression
      # step[1] -> block
      match_data = gherkin_step.match(step[0])
      if match_data
        matched = true
        promise = promise.then do |result|
          operation.step_result = result
          operation.instance_exec(*match_data, &step[1])
        end
      end
    end
    Isomorfeus.raise_error(message: "No match found for step #{gherkin_step}!") unless matched
  end

  # fail track
  self.class.gherkin[:failure].each do |gherkin_step|
    matched = false
    self.class.failure_steps.each do |step|
      # step[0] -> regular_expression
      # step[1] -> block
      match_data = gherkin_step.match(step[0])
      if match_data
        matched = true
        promise = promise.fail do |result|
          operation.step_result = result
          operation.instance_exec(*match_data, &step[1])
        end
      end
    end
    Isomorfeus.raise_error(message: "No match found for failure step #{gherkin_step}!") unless matched
  end

  # ensure
  self.class.gherkin[:ensure].each do |gherkin_step|
    matched = false
    self.class.ensure_steps.each do |step|
      # step[0] -> regular_expression
      # step[1] -> block
      match_data = gherkin_step.match(step[0])
      if match_data
        matched = true

        promise = promise.ensure do |result|
          operation.step_result = result
          operation.instance_exec(*match_data, &step[1])
        end
      end
    end
    Isomorfeus.raise_error(message: "No match found for ensure step #{gherkin_step}!") unless matched
  end

  original_promise.resolve
  promise
end