#Ruby の aasm gem で callback の LifeCycle を動作確認する

# NOTE

- かなり細かくcallbackの層が分かれており、柔軟に指定できそう。
- 現在のイベントは aasm.current_event で得られる。
- aasm.from_state / aasm.to_state でステータスのBEFORE AFTER が得られる。
- callback の aasm.events では、そのタイミングで次に利用可能なイベントが配列で得られるっぽい。after_all_transitions と after_all_events のタイミングで状態が切り替わっているのが分かる。
- initial に callback 指定をしても new でインスタンス作成した時点で callback が走るわけではないらしい。
 

# Lifecycle

![image](https://user-images.githubusercontent.com/13635059/68553220-9ddc7b80-0462-11ea-93bd-04ede9c2fd3b.png)

[aasm/aasm: AASM - State machines for Ruby classes (plain Ruby, ActiveRecord, Mongoid)](https://github.com/aasm/aasm#lifecycle)

# Code

```rb
require 'aasm'

class Job
  include AASM

  aasm do
    state :sleeping, initial: true, before_enter: :log_status_change_before_enter
    state :running, :cleaning

    event :run do
      transitions from: :sleeping, to: :running
    end

    event :clean do
      transitions from: :running, to: :cleaning
    end

    event :sleep do
      transitions from: [:running, :cleaning], to: :sleeping
    end

    event :down do
      transitions from: [:running, :cleaning], to: :sleeping
    end

    before_all_events     :log_status_change_before_all_events
    after_all_transitions :log_status_change_after_all_transitions
    after_all_events      :log_status_change_after_all_events
  end

  def log_status_change_before_enter
    puts '[before_enter]'
    puts "changing from #{aasm.from_state} to #{aasm.to_state} (current_event: #{aasm.current_event})"
    puts
  end

  def log_status_change_before_all_events
    puts '[before_all_events]'
    log_change_status
  end

  def log_status_change_after_all_transitions
    puts '[after_all_transitions]'
    log_change_status
  end

  def log_status_change_after_all_events
    puts '[after_all_events]'
    log_change_status
  end

  def log_change_status
    puts "changing from #{aasm.from_state} to #{aasm.to_state} (current_event: #{aasm.current_event}) (events: #{aasm.events.map(&:name)})"
    puts
  end

end

# no callback happen
job = Job.new

job.run
job.clean
job.sleep

# [before_all_events]
# changing from  to  (current_event: run) (events: [:run])

# [after_all_transitions]
# changing from sleeping to running (current_event: run) (events: [:run])

# [after_all_events]
# changing from sleeping to running (current_event: run) (events: [:clean, :sleep, :down])

# [before_all_events]
# changing from sleeping to running (current_event: clean) (events: [:clean, :sleep, :down])

# [after_all_transitions]
# changing from running to cleaning (current_event: clean) (events: [:clean, :sleep, :down])

# [after_all_events]
# changing from running to cleaning (current_event: clean) (events: [:sleep, :down])

# [before_all_events]
# changing from running to cleaning (current_event: sleep) (events: [:sleep, :down])

# [after_all_transitions]
# changing from cleaning to sleeping (current_event: sleep) (events: [:sleep, :down])

# [after_all_events]
# changing from cleaning to sleeping (current_event: sleep) (events: [:run])


```

# ref

[AASM - classの状態遷移をスマートに実装するためのgem (Ruby, Active Record, Sequel, Mongoid) - Qiita](https://qiita.com/satour/items/fe838dc21dc95df95c62)


# Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2709








<!-- Update From Qiita API -->

# チャットメンバー募集


何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->