我創建了一個叫做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