我应该命名 STI 模型吗? [英] Should I namespace STI models?
问题描述
我有以下型号:
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 模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!