Module: LightService::Orchestrator::ClassMethods

Defined in:
lib/light-service/orchestrator.rb

Instance Method Summary collapse

Instance Method Details

#execute(code_block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/light-service/orchestrator.rb', line 53

def execute(code_block)
  issue_deprecation_warning_for(__method__)

  lambda do |ctx|
    return ctx if ctx.stop_processing?

    ctx = code_block.call(ctx)
    ctx
  end
end

#iterate(collection_key, steps) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/light-service/orchestrator.rb', line 64

def iterate(collection_key, steps)
  issue_deprecation_warning_for(__method__)

  lambda do |ctx|
    return ctx if ctx.stop_processing?

    collection = ctx[collection_key]
    item_key = collection_key.to_s.singularize.to_sym
    collection.each do |item|
      ctx[item_key] = item
      ctx = scoped_reduction(ctx, steps)
    end

    ctx
  end
end

#reduce(steps, context = @context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/light-service/orchestrator.rb', line 13

def reduce(steps, context = @context)
  issue_deprecation_warning_for(__method__)

  steps.each_with_object(context) do |step, ctx|
    if step.respond_to?(:call)
      step.call(ctx)
    elsif step.respond_to?(:execute)
      step.execute(ctx)
    else
      raise 'Pass either an action or organizer'
    end
  end
end

#reduce_if(condition_block, steps) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/light-service/orchestrator.rb', line 42

def reduce_if(condition_block, steps)
  issue_deprecation_warning_for(__method__)

  lambda do |ctx|
    return ctx if ctx.stop_processing?

    ctx = scoped_reduction(ctx, steps) if condition_block.call(ctx)
    ctx
  end
end

#reduce_until(condition_block, steps) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/light-service/orchestrator.rb', line 27

def reduce_until(condition_block, steps)
  issue_deprecation_warning_for(__method__)

  lambda do |ctx|
    return ctx if ctx.stop_processing?

    loop do
      ctx = scoped_reduction(ctx, steps)
      break if condition_block.call(ctx) || ctx.failure?
    end

    ctx
  end
end

#with(data = {}) ⇒ Object



8
9
10
11
# File 'lib/light-service/orchestrator.rb', line 8

def with(data = {})
  @context = LightService::Context.make(data)
  self
end

#with_callback(action, steps) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/light-service/orchestrator.rb', line 81

def with_callback(action, steps)
  issue_deprecation_warning_for(__method__)

  lambda do |ctx|
    return ctx if ctx.stop_processing?

    # This will only allow 2 level deep nesting of callbacks
    previous_callback = ctx[:callback]

    ctx[:callback] = lambda do |context|
      reduce(steps, context)
    end

    ctx = action.execute(ctx)
    ctx[:callback] = previous_callback

    ctx
  end
end