如何返回 aasm 事件的值?

发布于 2024-10-07 09:55:20 字数 398 浏览 2 评论 0原文

如何使 aasm 事件返回布尔值以外的值?我正在使用 aasm

2.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”方法来实现?

How do I make an aasm event return a value other than boolean? I'm using aasm 2.2.0

E.g. There is a MusicPlayer model which randomly plays a song when started

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

Now if I want to return the song that is currently being played on starting the player, how do I do it through the 'play_song' method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2024-10-14 09:55:20

你不能那样做。返回状态用于指示转换是否成功。但我很好奇你有什么用例需要它。

但是您可以包装状态转换并使用由 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

You can't do that. The return status is used to indicate if the transition was sussessful or not. But I'm curious what use case you have that you need that.

But you can wrap the state transition and use a variable that's set by the play_song method, like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文