我应该命名 STI 模型吗?

编程入门 行业动态 更新时间:2024-10-17 23:27:21
本文介绍了我应该命名 STI 模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下型号:

module Core class Conditioner include Mongoid::Document field :operator, type: String, default: '' # can be !=, ==, <, >, <=, >=, = end end module Core class Count < Conditioner field :threshold, type: Integer, default: 0 # a simple threshold end end module Core class Time < Conditioner UNITS = %w(seconds minutes hours days weeks months years) # will be use like this: Time.value.send(Time.unit) Ex: 3.minutes field :value, type: Integer, default: 0 # 3 field :unit, type: String, default: '' # minutes validates :unit, presence: true, inclusion: { in: UNITS } end end

想知道我是否应该使用 Conditioner 命名 Count 和 Time 类?像这样:

Was wondering if I should namespace the Count and Time class with Conditioner ? Like this:

module Core class Conditioner::Time < Conditioner end end

因为我现在必须像这样调用 Time.now ::Time.now.

Since I have to call Time.now like this now ::Time.now.

编辑

关于答案,也许这应该是一个更好的主意:

Regarding answers, maybe this should be a better idea:

module Core module Conditioner class Base end end end module Core module Conditioner class Count < Conditioner::Base end end end module Core module Conditioner class Time < Conditioner::Base end end end

因为定义一个名为 Core::Time 的类可能过于通用,没有多大意义.

Since defining a class called Core::Time is maybe too generic and does not make many sense.

你怎么看?不确定此处的最佳做法.

What do you think? Not sure about the best practice here.

推荐答案

您不必为其命名空间,但如果您愿意也可以.

You don't have to namespace it, although you can if you want to.

您是否应该或不应该取决于您想要建模的内容(而不是您是否必须使用 ::Time 来解决 Time...).看起来你的 Core::Time 已经继承了 Core::Conditioner,所以让它成为它的超类的内部类没有多大意义.在你的情况下,最好不要命名它.

Whether you should or shouldn't depends on what do you want to model (and not whether you have to address Time with ::Time or not...). It looks like your Core::Time already subclasses Core::Conditioner, so it doesn't make much sense to make it an inner class of it's superclass. In your case it is better not to namespace it.

与 ruby​​ 具有相同的类名在这里不是问题,因为您已经使用 Core 为其命名了.

Having the same class name as ruby is not an issue here, since you have already namespaced it with Core.

更多推荐

我应该命名 STI 模型吗?

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

发布评论

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

>www.elefans.com

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