Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mryoshio/f3c00471d0a7344bb5032358e2734625 to your computer and use it in GitHub Desktop.
Save mryoshio/f3c00471d0a7344bb5032358e2734625 to your computer and use it in GitHub Desktop.
# sample task model
#
# > rails c
# irb(main):004:0> t.finish
# "-------> notify_done with 0"
# "-------> notify_done with 1"
# "-------> notify_done with 2"
# "-------> notify_done end"
# => true
class Task < ActiveRecord::Base
state_machine initial: :todo do
state :todo
state :doing
state :done
event :start do
transition from: :todo, to: :doing
end
event :finish do
transition from: :doing, to: :done
end
event :redo do
transition from: :done, to: :todo
end
after_transition to: :todo, do: :notify_todo
after_transition to: :doing, do: :notify_doing
after_transition to: :done do |task, transition|
task.notify_done
end
end
def notify_todo
p "-------> #{__method__}"
end
def notify_doing
p "-------> #{__method__}"
end
def notify_done(cnt: 0)
p "-------> #{__method__} with #{cnt}"
raise
rescue => e
sleep Math.exp(cnt)
cnt += 1
retry if cnt < 3
ensure
p "-------> #{__method__} end"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment