等同于Django信号用于Rails?

时间:2021-03-22 00:12:47

In Rails, the closest I've seen to Django Signals are Observers. The problem with them is that they're restricted to triggering callbacks on hardcoded events related to a model's lifecycle.

在Rails中,我所见过的最接近Django信号的是观察者。它们的问题是,它们仅限于触发与模型生命周期相关的硬编码事件的回调。

Django signals can be created anywhere, triggered anywhere and handled anywhere. The model lifecycle callbacks are just regular signals that happen to come built-in and that are triggered by the ORM.

Django信号可以在任何地方创建、在任何地方触发和处理。模型生命周期回调只是内置的、由ORM触发的常规信号。

Does anyone know of a similarly general solution for Rails? It could be some generic Ruby library, not tied to Rails, which would be even better.

有人知道类似的Rails通用解决方案吗?它可以是一些通用的Ruby库,而不是绑定到Rails,这样会更好。


Edit: Observer is the closest thing, but it's not what I'm looking for. It's a one-to-many solution. Anyone can listen, but only the originating object can post. I'd like something where you declare a signal, and anyone can trigger it as well as handle it. Also, I don't like the fact that the Ruby Observer dictates that the handler have an #update method. I'd like to be able to pass any method reference with the appropriate signature.

编辑:观察者是最接近的东西,但它不是我想要的。这是一个一对多的解决方案。任何人都可以侦听,但只有原始对象可以发布。我想要一个你声明一个信号的东西,任何人都可以触发它,也可以处理它。另外,我不喜欢Ruby观察者告诉处理程序有一个#update方法。我希望能够通过适当的签名传递任何方法引用。

I could use the Ruby Observer to implement my own such broker, but I'm trying to learn if someone already did it.

我可以使用Ruby观察者来实现我自己的代理,但是我正在尝试了解是否有人已经这样做了。

5 个解决方案

#1


3  

I think a closer equivalent than Rails' Observer is the standard Ruby Observable module. It lets you add a list of observers to an object and the object can then send notifications to the observers when it changes.

我认为比Rails的观察者更接近的是标准的Ruby可观察模块。它允许向对象添加一个观察者列表,然后当对象发生更改时,对象可以向观察者发送通知。

#2


1  

What about the 'wisper' gem? https://github.com/krisleech/wisper

那“wisper”宝石呢?https://github.com/krisleech/wisper

Wisper is a Ruby library for decoupling and managing the dependencies of your Ruby objects using Pub/Sub.

Wisper是一个使用Pub/Sub来解耦和管理Ruby对象依赖关系的Ruby库。

It is commonly used as an alternative to ActiveRecord callbacks and Observers to reduce coupling between data and domain layers.

它通常被用作ActiveRecord回调和观察者的替代,以减少数据和域层之间的耦合。

#3


0  

Perhaps acts_as_state machine will help. Most of this functionality has recently been baked into Rails edge.

也许acts_as_state机器会有所帮助。这个功能的大部分最近都被集成到了Rails edge中。

#4


0  

I just implemented a gem with that. https://github.com/pkoch/django_signal/

我刚刚实现了一个宝石。https://github.com/pkoch/django_signal/

#5


0  

Ruby gem 'watchable' is the most appropriate choice https://github.com/jbarnette/watchable

Ruby gem“watchable”是最合适的选择:https://github.com/jbarnette/watchable

It has a syntax that is very familiar to Django's (and other frameworks, like Qt and many others).

它具有Django非常熟悉的语法(以及其他框架,如Qt和许多其他框架)。

#1


3  

I think a closer equivalent than Rails' Observer is the standard Ruby Observable module. It lets you add a list of observers to an object and the object can then send notifications to the observers when it changes.

我认为比Rails的观察者更接近的是标准的Ruby可观察模块。它允许向对象添加一个观察者列表,然后当对象发生更改时,对象可以向观察者发送通知。

#2


1  

What about the 'wisper' gem? https://github.com/krisleech/wisper

那“wisper”宝石呢?https://github.com/krisleech/wisper

Wisper is a Ruby library for decoupling and managing the dependencies of your Ruby objects using Pub/Sub.

Wisper是一个使用Pub/Sub来解耦和管理Ruby对象依赖关系的Ruby库。

It is commonly used as an alternative to ActiveRecord callbacks and Observers to reduce coupling between data and domain layers.

它通常被用作ActiveRecord回调和观察者的替代,以减少数据和域层之间的耦合。

#3


0  

Perhaps acts_as_state machine will help. Most of this functionality has recently been baked into Rails edge.

也许acts_as_state机器会有所帮助。这个功能的大部分最近都被集成到了Rails edge中。

#4


0  

I just implemented a gem with that. https://github.com/pkoch/django_signal/

我刚刚实现了一个宝石。https://github.com/pkoch/django_signal/

#5


0  

Ruby gem 'watchable' is the most appropriate choice https://github.com/jbarnette/watchable

Ruby gem“watchable”是最合适的选择:https://github.com/jbarnette/watchable

It has a syntax that is very familiar to Django's (and other frameworks, like Qt and many others).

它具有Django非常熟悉的语法(以及其他框架,如Qt和许多其他框架)。