3

aasmイベントがブール値以外の値を返すようにするにはどうすればよいですか?私はaasm2.2.0を使用しています

例:開始時にランダムに曲を再生するMusicPlayerモデルがあります

aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly and return the song object
end

プレーヤーの起動時に現在再生されている曲を返したい場合、「play_song」メソッドを使用してそれを行うにはどうすればよいですか?

4

1 に答える 1

0

あなたはそれをすることはできません。リターンステータスは、遷移が正常であったかどうかを示すために使用されます。しかし、私はあなたがそれを必要としているあなたのユースケースが何であるか興味があります。

ただし、状態遷移をラップして、次のplay_songようにメソッドによって設定される変数を使用することができます。

aasm_state :started, :after_enter => :play_song
aasm_state :stopped
aasm_event :start do
  :transitions :from => :stopped, :to => :started
end

def play_song
  # select and play a song randomly
  @song = ...
end

def shuffle
  if start
    @song
  end
end
于 2012-10-20T08:36:32.163 に答える