Skip to content

Instantly share code, notes, and snippets.

@aarontc
Created May 28, 2014 18:58
Show Gist options
  • Save aarontc/799c3bf174509b682fed to your computer and use it in GitHub Desktop.
Save aarontc/799c3bf174509b682fed to your computer and use it in GitHub Desktop.
Example for AASM feature request
require 'aasm'
require 'timers'
class TimedStateMachineExample
include AASM
TRANSITION_SECONDS = 5
def initialize(timers)
@timers = timers || Timers.new
end
aasm do
state :first, initial: true, before_enter: :begin_timed_transition
state :second
state :third
event :one do
transitions from: :third, to: :first
end
event :two do
transitions from: :first, to: :second
end
event :three do
transitions from: :second, to: :third
end
end
private
def begin_timed_transition
@transition_timer = @timers.after(TRANSITION_SECONDS) { finish_timed_transition }
end
def finish_timed_transition
two
end
end
def main
timers = Timers.new
uut = TimedStateMachineExample.new(timers)
timers.wait
puts 'If the state machine was initialized on object instantiation, the next line will say "second":'
puts uut.aasm.current_state
end
main if $0 === __FILE__
require 'aasm'
require 'timers'
class TimedStateMachineExample
include AASM
TRANSITION_SECONDS = 5
def initialize(timers)
@timers = timers || Timers.new
aasm.current_state
end
aasm do
state :first, initial: true, before_enter: :begin_timed_transition
state :second
state :third
event :one do
transitions from: :third, to: :first
end
event :two do
transitions from: :first, to: :second
end
event :three do
transitions from: :second, to: :third
end
end
private
def begin_timed_transition
@transition_timer = @timers.after(TRANSITION_SECONDS) { finish_timed_transition }
end
def finish_timed_transition
two
end
end
def main
timers = Timers.new
uut = TimedStateMachineExample.new(timers)
timers.wait
puts 'If the state machine was initialized on object instantiation, the next line will say "second":'
puts uut.aasm.current_state
end
main if $0 === __FILE__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment