Class: InterstateMachine::StateMachine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ StateMachine

Returns a new instance of StateMachine.



6
7
8
9
# File 'lib/interstate_machine/state_machine.rb', line 6

def initialize(object)
  @states = object.class.machine_states
  @state = defined_state(object)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/interstate_machine/state_machine.rb', line 3

def context
  @context
end

#stateObject

Returns the value of attribute state.



4
5
6
# File 'lib/interstate_machine/state_machine.rb', line 4

def state
  @state
end

#statesObject (readonly)

Returns the value of attribute states.



3
4
5
# File 'lib/interstate_machine/state_machine.rb', line 3

def states
  @states
end

Instance Method Details

#evaluate_transition_by!(rule) ⇒ Object



11
12
13
14
# 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

#nextObject



20
21
22
23
# File 'lib/interstate_machine/state_machine.rb', line 20

def next
  context.object.state = context.transition_to.first
  context.object.save
end

#next_stateObject



16
17
18
# File 'lib/interstate_machine/state_machine.rb', line 16

def next_state
  context.transition_to.first
end