我正在构建一个带有续集数据库后端的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 endIn 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 NorthOfYou 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更多推荐
发布评论