Class: FunCi::Persistence::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_ci/persistence/state_machine.rb

Defined Under Namespace

Classes: InvalidTransition

Constant Summary collapse

STATES =
%i[scheduled running completed failed timed_out cancelled].freeze
TRANSITIONS =
{
  scheduled: %i[running cancelled],
  running: %i[completed failed timed_out cancelled],
  completed: [],
  failed: [],
  timed_out: [],
  cancelled: []
}.freeze
TERMINAL_STATES =
%i[completed failed timed_out cancelled].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_state) ⇒ StateMachine

Returns a new instance of StateMachine.



23
24
25
26
# File 'lib/fun_ci/persistence/state_machine.rb', line 23

def initialize(initial_state)
  validate_state!(initial_state)
  @current_state = initial_state
end

Instance Attribute Details

#current_stateObject (readonly)

Returns the value of attribute current_state.



21
22
23
# File 'lib/fun_ci/persistence/state_machine.rb', line 21

def current_state
  @current_state
end

Instance Method Details

#terminal?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/fun_ci/persistence/state_machine.rb', line 37

def terminal?
  TERMINAL_STATES.include?(@current_state)
end

#transition_to!(new_state) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/fun_ci/persistence/state_machine.rb', line 28

def transition_to!(new_state)
  validate_state!(new_state)
  unless TRANSITIONS.fetch(@current_state).include?(new_state)
    raise InvalidTransition,
      "Cannot transition from #{@current_state} to #{new_state}"
  end
  @current_state = new_state
end