1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00
aasm/spec/models/callbacks/basic.rb

63 lines
2.1 KiB
Ruby
Raw Normal View History

2014-11-25 16:49:21 -05:00
module Callbacks
class Basic
include AASM
def initialize(options={})
@fail_event_guard = options[:fail_event_guard]
@fail_transition_guard = options[:fail_transition_guard]
@log = options[:log]
end
aasm do
state :open, :initial => true,
:before_enter => :before_enter_open,
:enter => :enter_open,
:after_enter => :after_enter_open,
:before_exit => :before_exit_open,
:exit => :exit_open,
:after_exit => :after_exit_open
state :closed,
:before_enter => :before_enter_closed,
:enter => :enter_closed,
:after_enter => :after_enter_closed,
:before_exit => :before_exit_closed,
:exit => :exit_closed,
:after_exit => :after_exit_closed
2014-12-05 11:20:32 -05:00
event :close, :before => :before_event, :after => :after_event, :guard => :event_guard do
transitions :to => :closed, :from => [:open], :guard => :transition_guard, :after => :after_transition
2014-11-25 16:49:21 -05:00
end
2014-12-05 11:20:32 -05:00
event :open, :before => :before_event, :after => :after_event do
2014-11-25 16:49:21 -05:00
transitions :to => :open, :from => :closed
end
end
def log(text)
puts text if @log
end
def before_enter_open; log('before_enter_open'); end
def enter_open; log('enter_open'); end
def before_exit_open; log('before_exit_open'); end
def after_enter_open; log('after_enter_open'); end
def exit_open; log('exit_open'); end
def after_exit_open; log('after_exit_open'); end
def before_enter_closed; log('before_enter_closed'); end
def enter_closed; log('enter_closed'); end
def before_exit_closed; log('before_exit_closed'); end
def exit_closed; log('exit_closed'); end
def after_enter_closed; log('after_enter_closed'); end
def after_exit_closed; log('after_exit_closed'); end
def event_guard; log('event_guard'); !@fail_event_guard; end
def transition_guard; log('transition_guard'); !@fail_transition_guard; end
2014-12-05 11:20:32 -05:00
def after_transition; log('after_transition'); end
2014-11-25 16:49:21 -05:00
2014-12-05 11:20:32 -05:00
def before_event; log('before_event'); end
def after_event; log('after_event'); end
2014-11-25 16:49:21 -05:00
end
end