Come restituire un valore su SAMA evento?
-
25-09-2019 - |
Domanda
Come faccio a fare un evento AASM restituisce un valore diverso da booleano? Sto usando SAMA 2.2.0
es. C'è un modello MusicPlayer che svolge in modo casuale un brano quando ha iniziato
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
Ora, se voglio tornare la canzone che è attualmente in riproduzione sulla partenza del giocatore, come faccio a fare attraverso il 'play_song' metodo?
Soluzione
Non si può farlo. Lo stato di ritorno è usato per indicare se la transizione è stata Sussessful o meno. Ma io sono curioso di sapere cosa caso d'uso che avete che avete bisogno di questo.
Ma si può avvolgere la transizione di stato e di utilizzare una variabile che è impostato dal metodo play_song
, in questo modo:
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow