Ruby-确保仅一个类对象 [英] Ruby - Ensure only one class object

查看:96
本文介绍了Ruby-确保仅一个类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型 Bot ,我想确保数据库中只有一个 Bot 对象。我还需要确保它是持久的,并且不会被篡改。

I have a Model Bot and I would like to ensure that there is only one Bot object in my database. I also need to make sure it is persisted and not tampered with.

我最初的想法是在迁移中执行此操作,该迁移将跟随:bots 表迁移。它将包含类似于以下内容的行:

My original thought was to do this in a migration, one that would follow the :bots table migration. It would include a line that is something like:

Bot.all.size == 0? Bot.create! :nil

也许这可以防止在将来的迁移中将AR对象弄乱?

Maybe this would prevent the AR object from being messed with in future migrations?

Bonus:能够即时并全局访问该类对象真是太棒了。我在考虑在我的 Bot 类中使用一个 singleton 模块,这样我就可以始终引用 Bot .instance 并有权访问该特定对象。

BONUS: Would be awesome to be able to have instant and global access to this class object. I was thinking using a singleton module in my Bot class that way I can always reference Bot.instance and have access to that specific object.

使用案例:

我的数据库中有4种类型的用户,该机器人将通过我们的应用内消息传递功能向他们传递特定于角色的消息。

I have 4 types of users in my DB and this bot will be the facilitator to delivery role-specific messages to them through our in-app messaging feature.

Class Bot 将具有 has_many BotMessage的关联/ bot_messages 。在 bot_messages 表上将是 user_role 的枚举字段。

The Class Bot will have a has_many association with BotMessage/bot_messages. On the bot_messages table will be an enum field for user_role.

消息将由公司管理员创建并存储在这些表中,因为我们希望通过查看用户与用户之间的对话线程来随时查看消息。 t

Messages will be created by company admins and stored in these tables because we want them to be viewable at any time by looking at the "conversation" thread between the User and the Bot.

仅拥有1个机器人就可以了。我不需要额外的 Bot 对象。此外,由于只有一个对象,因此能够以一种明确的方式定位到该对象而无需运行查询来查找它会很不错。

When it comes to only having 1 bot, it's just that. I have no need for an additional Bot object. Additionally, since there is only one object it would be nice to be able to have a way of explicitly targeting that object without having to run a query to find it.

对于例如,与 User 不同,在这里可能有1000条记录,为了找到特定的记录,您可以执行 @user = User.find_by_email(' foo@bar.com'),因为只需要查找一条记录,因此无需为机器人执行类似操作。这就是让我相信拥有 singleton 对象的可能在这里值得,因此,每当我需要为特定角色拉一条消息时,我都可以运行 Bot.instance.bot_messages.where(user_role:1)或类似的东西

For example, unlike User where there could be 1000 records and in order to find the specific one you would do something like @user = User.find_by_email('foo@bar.com'), doing something like that for the bot would be unnecessary since there is only one record to find. That is what lead me to believe having a singleton object may be worthwhile here, so whenever I need to pull up a message for a specific role, I could run Bot.instance.bot_messages.where(user_role: 1) or something similar

推荐答案

基于在您的用例中,我认为没有理由让 Bot 成为模型。

Based on your Use Case, I see no reason for Bot to be a model.

假设您扮演一个角色名为 cool_user 的用户,并且想要获取该角色的所有 bot_messages ,您可以执行以下操作:

Let's say you have a role called cool_user and you want to get all the bot_messages for that role, you might do something like:

class Bot

  class << self

    def bot_messages(user_role)
      BotMessage.send(user_role)
    end

  end

end

作为注释中非常周到但可能匿名的超级代码猴子笔记,您也可以这样做:

As a very thoughtful but potentially anonymous super code monkey notes in the comments, you could also do:

class Bot

  def self.bot_messages(user_role)
    BotMessage.send(user_role)
  end

end

有些人可能查找更具可读性。 IMO,这是个个人喜好问题。

Which some folks might find more readable. IMO, it is a bit of an issue of personal preference.

无论哪种情况,您都应该能够做到

In either case, you should be able to do

Bot.bot_messages(:cool_user)

文档


还将提供基于枚举字段允许值的范围。

Scopes based on the allowed values of the enum field will be provided as well.

所以,我相信 BotMessage ,并正确设置了枚举,应响应 cool_user 并返回该角色的所有 bot_messages

So, I believe BotMessage, with the properly set enum, should respond to cool_user and return all the bot_messages for that role.

您可能需要检查 docs 以使语法完全正确。

You may need to check the docs to get the syntax exactly right.

我相信这也应该满足您的奖金要求。

I believe this should also satisfy your BONUS requirement.

这篇关于Ruby-确保仅一个类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆