class Dotpretty::StateMachine::StateMachine

Attributes

current_state_name[RW]
observer[RW]
states[RW]

Public Class Methods

new(initial_state:, observer:, states:) click to toggle source
# File lib/dotpretty/state_machine/state_machine.rb, line 7
def initialize(initial_state:, observer:, states:)
  self.current_state_name = initial_state
  self.observer = observer
  self.states = states
end

Public Instance Methods

trigger(event, *args) click to toggle source
# File lib/dotpretty/state_machine/state_machine.rb, line 15
def trigger(event, *args)
  current_state.trigger(event) do |transition, exit_action|
    perform(exit_action, *args) if current_state_name != transition[:next_state_name]
    perform(transition[:action], *args)
    if current_state_name != transition[:next_state_name]
      self.current_state_name = transition[:next_state_name]
      perform(current_state.entry_action, *args)
    end
  end
end

Private Instance Methods

current_state() click to toggle source
# File lib/dotpretty/state_machine/state_machine.rb, line 32
def current_state
  states[current_state_name] || StateDetails.new({
    entry_action: nil,
    exit_action: nil,
    transitions: {}
  })
end
perform(action, *args) click to toggle source
# File lib/dotpretty/state_machine/state_machine.rb, line 28
def perform(action, *args)
  observer.send(action, *args) if action
end