Module: Pathway::Plugins::Base::DSLMethods

Defined in:
lib/pathway.rb

Instance Method Summary collapse

Instance Method Details

#guard(cond, &steps) ⇒ Object



162
163
164
165
166
167
# File 'lib/pathway.rb', line 162

def guard(cond, &steps)
  cond = _callable(cond)
  sequence(-> seq, state {
    seq.call if cond.call(state)
  }, &steps)
end

#initialize(operation, input) ⇒ Object



122
123
124
125
# File 'lib/pathway.rb', line 122

def initialize(operation, input)
  @result = wrap(State.new(operation, input: input))
  @operation = operation
end

#map(callable) ⇒ Object

Execute step and replace the current state completely



150
151
152
153
# File 'lib/pathway.rb', line 150

def map(callable)
  bl = _callable(callable)
  @result = @result.then(bl)
end

#run(&bl) ⇒ Object



127
128
129
130
# File 'lib/pathway.rb', line 127

def run(&bl)
  instance_eval(&bl)
  @result
end

#sequence(steps_wrapper, &steps) ⇒ Object



155
156
157
158
159
160
# File 'lib/pathway.rb', line 155

def sequence(steps_wrapper, &steps)
  @result.then do |state|
    seq = -> { @result = dup.run(&steps) }
    _callable(steps_wrapper).call(seq, state)
  end
end

#set(callable, *args, to: @operation.result_key) ⇒ Object

Execute step and modify the former state setting the key



140
141
142
143
144
145
146
147
# File 'lib/pathway.rb', line 140

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) ⇒ Object

Execute step and preserve the former state



133
134
135
136
137
# File 'lib/pathway.rb', line 133

def step(callable, *args)
  bl = _callable(callable)

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