续集(DRY)中不同模型上的相同数据集/过滤逻辑(Same dataset

编程入门 行业动态 更新时间:2024-10-25 06:22:54
续集(DRY)中不同模型上的相同数据集/过滤逻辑(Same dataset-/filter-logic on different models in sequel (DRY))

我正在构建一个带有续集数据库后端的sinatra Web应用程序。 此应用程序的主要任务是收集来自不同机器人的状态消息,将它们存储在数据库中并提供各种方法来查看它们。 这些消息的一个共同点是,它们提供纬度/经度的WGS84位置。

现在我想提供各种过滤器来根据它们的位置查询消息,但是我想只编写一次这些过滤器,只测试一次,但在带有lat / lon条目的所有模型类中重复使用它们。

将其归结为一个非常简单的例子:

Sequel.migration do up do create_table(:auvmessages) do primary_key :id Float :lat Float :lon String :message end create_table(:asvmessages) do primary_key :id Float :lat Float :lon Integer :chargestate end end end class Auvessage < Sequel::Model dataset_module do def north_of(lat) self.where{ latitude > lat} end end end class Asvessage < Sequel::Model dataset_module do def north_of(lat) self.where{ latitude > lat} end end end

在两个模型类中都有north_of(lat)来过滤来自给定纬度以北的消息。 这个功能相当简单,您可以轻松地重复两到三次,但更复杂的情况呢?

我使用dataset_module之外的模块玩了一下,但似乎没有什么是正确的。

有没有一种首选的方法如何在不同的模型上重复使用过滤器? 我搜索了很多,但没有找到任何令人满意的答案。

编辑:

为了使我的问题更精确:我想将所有函数(如north_of(lat) (还有更多)移动到服务类中。 我现在想知道的是将该服务类集成到续集模型中的最佳方法:

“只是”包括它? 扩展dataset_module,如果是,如何? 编写数据集插件? ...

I am building a sinatra web app with a sequel database backend. The primary tasks of this app is collecting status messages from different robots, store them in a database and provide various methods to view them. A common denominator in these messages is, that they provide a WGS84 position in lat/lon.

Now I want to provide various filters for querying messages based on their positions, but I want to write these filters only once, test them only once but re-use them in all model-classes with a lat/lon entry.

To boil it down to a very simple example:

Sequel.migration do up do create_table(:auvmessages) do primary_key :id Float :lat Float :lon String :message end create_table(:asvmessages) do primary_key :id Float :lat Float :lon Integer :chargestate end end end class Auvessage < Sequel::Model dataset_module do def north_of(lat) self.where{ latitude > lat} end end end class Asvessage < Sequel::Model dataset_module do def north_of(lat) self.where{ latitude > lat} end end end

In both model classes have north_of(lat) to filter for messages which originate north of a given latitude. This function is fairly simple and you can easily repeat it two or three times, but what about more complex cases?

I have played around a bit with modules outside of dataset_module but nothing seem to be right.

Is there a preferred way how to re-use filters over different models? I have searched a lot, but didn't find any satisfying answer.

Edit:

To make my question a bit more precise: I want to move all functions like north_of(lat) (there are a lot more) into a service class. What I want to know now, is the best way to integrate that service class into a sequel-model:

"Just" include it? Extend dataset_module, and if so, how? Writing a dataset-plugin? ...

最满意答案

您可以将现有模块传递给dataset_module :

module NorthOf def north_of(lat) where{latitude > lat} end end Auvessage.dataset_module NorthOf Asvessage.dataset_module NorthOf

You can pass an existing module to dataset_module:

module NorthOf def north_of(lat) where{latitude > lat} end end Auvessage.dataset_module NorthOf Asvessage.dataset_module NorthOf

更多推荐

本文发布于:2023-08-03 12:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:续集   逻辑   模型   数据   dataset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!