class InterstateMachine::StateMachine

Attributes

context[R]
state[RW]
states[R]

Public Class Methods

new(object) click to toggle source
# File lib/interstate_machine/state_machine.rb, line 6
def initialize(object)
  @states = object.class.machine_states
  @state = defined_state(object)
end

Public Instance Methods

evaluate_transition_by!(rule) click to toggle source
# File lib/interstate_machine/state_machine.rb, line 11
def evaluate_transition_by!(rule)
  @context = rule
  raise failed_transition unless context.from.include? context.object.state.to_sym
end
next() click to toggle source
# File lib/interstate_machine/state_machine.rb, line 20
def next
  context.object.state = context.transition_to.first
  context.object.save
end
next_state() click to toggle source
# File lib/interstate_machine/state_machine.rb, line 16
def next_state
  context.transition_to.first
end

Private Instance Methods

defined_state(object) click to toggle source
# File lib/interstate_machine/state_machine.rb, line 32
def defined_state(object)
  # TODO  quite ugly, can be improved
  if object.state.nil?
    object.state = object.class.initialized_state.to_sym
    object.save if object.respond_to?(:persisted?) && object.persisted?
    object.state
  else
    object.state.to_sym
  end
end
failed_transition() click to toggle source
# File lib/interstate_machine/state_machine.rb, line 27
def failed_transition
  "can not transit to #{context.transition_to} from #{state} via
    #{context.event}"
end