module Pathway::Plugins::Base::DSLMethods

Public Class Methods

new(state, operation) click to toggle source
# File lib/pathway.rb, line 130
def initialize(state, operation)
  @result, @operation = wrap(state), operation
end

Public Instance Methods

around(wrapper, &steps) click to toggle source
# File lib/pathway.rb, line 162
def around(wrapper, &steps)
  @result.then do |state|
    seq = -> (dsl = self) { @result = dsl.run(&steps) }
    _callable(wrapper).call(seq, state)
  end
end
Also aliased as: sequence
guard(cond, &steps)
Alias for: if_true
if_false(cond, &steps) click to toggle source
# File lib/pathway.rb, line 176
def if_false(cond, &steps)
  cond = _callable(cond)
  if_true(-> state { !cond.call(state) }, &steps)
end
if_true(cond, &steps) click to toggle source
# File lib/pathway.rb, line 169
def if_true(cond, &steps)
  cond = _callable(cond)
  around(-> seq, state {
    seq.call if cond.call(state)
  }, &steps)
end
Also aliased as: guard
map(callable) click to toggle source

Execute step and replace the current state completely

# File lib/pathway.rb, line 157
def map(callable)
  bl = _callable(callable)
  @result = @result.then(bl)
end
run(&bl) click to toggle source
# File lib/pathway.rb, line 134
def run(&bl)
  instance_eval(&bl)
  @result
end
sequence(wrapper, &steps)
Alias for: around
set(callable, *args, to: @operation.result_key) click to toggle source

Execute step and modify the former state setting the key

# File lib/pathway.rb, line 147
def set(callable, *args, to: @operation.result_key)
  bl = _callable(callable)

  @result = @result.then do |state|
    wrap(bl.call(state, *args))
      .then { |value| state.update(to => value) }
  end
end
step(callable, *args) click to toggle source

Execute step and preserve the former state

# File lib/pathway.rb, line 140
def step(callable, *args)
  bl = _callable(callable)

  @result = @result.tee { |state| bl.call(state, *args) }
end

Private Instance Methods

_callable(callable) click to toggle source
# File lib/pathway.rb, line 190
def _callable(callable)
  case callable
  when Proc
    -> *args { @operation.instance_exec(*args, &callable) }
  when Symbol
    -> *args { @operation.send(callable, *args) }
  else
    callable
  end
end
wrap(obj) click to toggle source
# File lib/pathway.rb, line 186
def wrap(obj)
  Result.result(obj)
end