首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回调后在AASM中产生项目

回调后在AASM中产生项目
EN

Stack Overflow用户
提问于 2018-12-20 23:40:26
回答 1查看 29关注 0票数 0

你能在一个:after回调中产生项目吗?当我执行以下代码时,我得到了LocalJumpException

代码语言:javascript
复制
require 'aasm'
class TestClass
  include AASM
  aasm do
    state :created, initial: true
    state :running
    event :run do
      transitions from: :created,
      to: :running,
      after: proc { yield 1 }
    end
  end
end
TestClass.new.run! { |v| puts v }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-21 00:13:03

这是不可能的,因为aasm丢弃了传递给事件调用的代码块,但是嘿,它是ruby。

代码语言:javascript
复制
require 'aasm'
class TestClass
  include AASM
  aasm do
    state :created, initial: true
    state :running
    event :run do
      transitions from: :created,
      to: :running,
      after: -> { @λ_run.call(1) if @λ_run } # invoke the code block if was passed
    end
  end
end
TestClass.prepend(Module.new do
  def run!(*args, &λ)
    @λ_run = λ # store the code block passed to the method
    super
  end
end)
TestClass.new.run! { |v| puts v.inspect }

通过一些元编程,它应该很容易扩展到所有已定义的事件处理程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53871758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档