我创建了一个叫做Collection
从我的项目模型的所有对象的集合中持有的接口。我想要这个集合vs一个数组,因为我想要Collection
中的其他字段。Ruby接口的集合对象
module Collection
def self.included(klass)
klass.attr_writer :list, type: Array
klass.attr_writer :class_type, type: Class
# Other fields go in here, along with their validations
klass.validate :validate_list
def validate_list
self.list.each { |o|
if(!o.instance_of? self.class_type)
klass.errors.add :list, 'Objects in list must be of the same type'
return
end
}
end
end
end
我想用这个Collection
持有模式::公司的对象,除了从其他列表中,我将在未来加入到投资组合模型的列表。我希望这个公司名单只是投资组合模型的一部分。
class Portfolio
include Model::Collection
@schema = {
'type' => 'object',
'properties' => {
'id' => { 'type' => 'string' },
'title' => { 'type' => 'string' },
'description' => { 'type' => 'string' },
'companies_list' => {'type' => '?'}, # 1. Should this be array or Collections?
}
}
@modelName = 'portfolios'
@collectionName = 'portfolios'
store_in collection: 'portfolios'
field :title, type: String
field :description, type: String
field :companies_list, type: Array # 2. Should this be array or array of Collections?
embeds_many :companies
end
任何帮助表示赞赏。
在模块中使用术语“摘要”真的会让人困惑。这有一个特定的含义,它与类有关,尽管Ruby没有真正使用这个术语,因为没有用于表示抽象基类的标准方法。此外,这里的问题还不清楚。 – tadman
@tadman:对不起,我应该把它改成'class'? – chipmunk
不,我的意思是说这是一个mixin模块,但称它为“抽象”是错误的。为什么不把它称为'Model :: CollectionMethods'或'Model :: CollectionValidations'或类似的东西? – tadman