您为 Rails 推荐哪种状态机插件?

发布于 2024-11-17 12:36:10 字数 1536 浏览 3 评论 0原文

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

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

发布评论

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

评论(6

音盲 2024-11-24 12:36:10

state_machine 似乎是人们说要使用的那个,至少我和他们交谈过。它与环境无关,因此您不必在应用程序的某个部分使用一个状态机,而在应用程序的另一部分使用完全不同的状态机。

2015 年 2 月更新

遗憾的是,状态机已不再维护,并且存在许多问题,这使其成为不太受欢迎的选择。然而,该项目的这个分支得到了积极维护,并且看起来很稳定。

state_machine seems to be the one people say to use, at least who I've spoken to. It is environment agnostic, so you don't have to use one state machine in one part of your app, and a completely different one in another part of your app.

Update February 2015

State Machine is unfortunately no longer maintained and there are many issues, which makes it a less favourable choice. However, this fork of the project is actively maintained and appears to be stable.

山田美奈子 2024-11-24 12:36:10

我最终使用了 Stateflow 并且喜欢它。 https://github.com/ryanza/stateflow

它运行在rails 3.0上,方法类似状态机代码存在于多个 Rails 3.0 测试版中,但从最终版本中删除。我还没有继续了解当前关于在 Rails 中使用状态机的想法是什么 - 但我想如果状态机在未来的版本中重新集成,它会有点像这样。如果我想放弃 gem 并使用核心功能,希望这意味着最少的代码更改

I ended up using stateflow and like it. https://github.com/ryanza/stateflow

It runs with rails 3.0, and it is similar in approach to the state machine code that was in several of the rails 3.0 betas but dropped from the final release. I've not followed along to see what the current thinking about having a state machine inside rails is - but I guess that if a state machine gets reintegrated in a future release it will be a bit like this. Hopefully that will mean minimal code changes if I ever want to drop the gem and use core functionality

梦年海沫深 2024-11-24 12:36:10

我会谨慎对待转变。作者/提取者说他没有时间维护它,并且通常在他的新项目中偏爱 state_machine

另一方面,它确实有效(尽管检查未解决的问题以查看它是否适用于您的情况)。我自己在一个项目中使用转换,但用于一个简单的状态机。

我之前在 Rails 2 项目中使用过 actions 作为状态机,而且效果非常好(即使是非常复杂的状态机)

I would approach Transitions with care. The author/extractor says he doesn't have time to maintain it, and usually favors state_machine on his new projects.

On the other hand, it does work (although check the open issues to see if it will work in your case). I'm using Transitions myself on a project, but for a trivial state machine.

I've used acts as state machine before, on a Rails 2 project, and it did really well (even with very complex state machines)

久光 2024-11-24 12:36:10

SimpleStateMachine 是一个简单的 DSL,用于使用状态转换防护来装饰现有方法。

class LampSwitch
   extend SimpleStateMachine

   def initialize
     self.state = 'off'
   end

   event :push_switch, :off => :on
end

lamp = LampSwitch.new
lamp.state          # => 'off'
lamp.off?           # => true
lamp.push_switch    #
lamp.state          # => 'on'
lamp.on?            # => true

它与 ActiveModel 验证一起使用,并允许使用参数调用事件:

class User < ActiveRecord::Base
  ...
  def activate_account(activation_code)
    if activation_code_invalid?(activation_code)
      errors.add(:activation_code, 'Invalid')
    end
  end
  event :activate_account, :invited => :activated
end

user = User.new
user.activate_account!('INVALID') # => raises ActiveRecord::RecordInvalid
user.activated?                     # => false
user.activate_account!('VALID')
user.activated?                     # => true

它可以挽救异常:

def download_data
  raise Service::ConnectionError
end
event :download_data, Service::ConnectionError => :download_failed

user.download_data               # catches Service::ConnectionError
user.state                       # => "download_failed"
user.state_machine.raised_error  # the raised error

SimpleStateMachine is a simple DSL to decorate existing methods with state transition guards.

class LampSwitch
   extend SimpleStateMachine

   def initialize
     self.state = 'off'
   end

   event :push_switch, :off => :on
end

lamp = LampSwitch.new
lamp.state          # => 'off'
lamp.off?           # => true
lamp.push_switch    #
lamp.state          # => 'on'
lamp.on?            # => true

It works with ActiveModel validations and allows events to be called with arguments:

class User < ActiveRecord::Base
  ...
  def activate_account(activation_code)
    if activation_code_invalid?(activation_code)
      errors.add(:activation_code, 'Invalid')
    end
  end
  event :activate_account, :invited => :activated
end

user = User.new
user.activate_account!('INVALID') # => raises ActiveRecord::RecordInvalid
user.activated?                     # => false
user.activate_account!('VALID')
user.activated?                     # => true

It can rescue exception:

def download_data
  raise Service::ConnectionError
end
event :download_data, Service::ConnectionError => :download_failed

user.download_data               # catches Service::ConnectionError
user.state                       # => "download_failed"
user.state_machine.raised_error  # the raised error
阿楠 2024-11-24 12:36:10

即使是最简单的状态机 gem 也有比我需要的更多的功能,所以我决定推出自己的解决方案。我尝试了 Transitions 和 Stateflow,但两者都存在一些小问题。

Even the simplest state machine gems had way more features than I needed, so I just decided to roll my own solution. I tried Transitions and Stateflow, but had minor issues with both.

格子衫的從容 2024-11-24 12:36:10

我会推荐 Workflow 因为我觉得它是所有可用状态机中最简单的。

I would recommend Workflow as i feel its the easiest of all the state machines available .

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