class LinearWorkFlow::WorkFlow

Attributes

index[RW]

Public Class Methods

at(state=nil)
Alias for: new
new(state=nil) click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 13
def initialize(state=nil)
  self.index = state ? states.index(state) : 0
  raise(InvalidStateError, "State must be in: #{states.inspect}") unless self.index
end
Also aliased as: at
states() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 4
def self.states
  [
    :first,
    :last
  ]
end

Public Instance Methods

actions() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 71
def actions
  {
    forward: :forward!,
    back: :back!
  }
end
back!() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 31
def back!
  raise(ChangeStateError, "Cannot go back from first state") if first?
  self.index -= 1
  state
end
back_state() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 53
def back_state
  states[index - 1] unless first?
end
can?(action) click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 61
def can?(action)
  !!restore_after do
    begin
      send(actions[action])
    rescue ChangeStateError
      false
    end
  end
end
first?() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 41
def first?
  index == 0
end
forward!() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 25
def forward!
  raise(ChangeStateError, "Cannot go forward from last state") if last?
  self.index += 1
  state
end
forward_state() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 49
def forward_state
  states[index + 1] unless last?
end
last?() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 37
def last?
  state == states.last
end
permissible_states() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 45
def permissible_states
  []
end
restore_after() { || ... } click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 82
def restore_after
  starting_index = index
  result = yield
  self.index = starting_index
  result
end
state() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 21
def state
  states[index]
end
states() click to toggle source
# File lib/linear_work_flow/work_flow.rb, line 78
def states
  self.class.states
end