Module: LightService::Orchestrator::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#execute(code_block) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/light-service/orchestrator.rb', line 47

def execute(code_block)
  lambda do |ctx|
    return ctx if ctx.stop_processing?

    ctx = code_block.call(ctx)
    ctx
  end
end

#iterate(collection_key, steps) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/light-service/orchestrator.rb', line 56

def iterate(collection_key, steps)
  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
# File 'lib/light-service/orchestrator.rb', line 13

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

#reduce_if(condition_block, steps) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/light-service/orchestrator.rb', line 38

def reduce_if(condition_block, steps)
  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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/light-service/orchestrator.rb', line 25

def reduce_until(condition_block, steps)
  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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/light-service/orchestrator.rb', line 71

def with_callback(action, steps)
  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