I have created an interface called Collection to hold in a collection of any objects from my project's models. I want this collection vs an array as I want other fields in Collection.
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
I want to use this Collection to hold a list of Models::Company 's objects, apart from other lists which I will add to Portfolio Model in the future. I want this list of companies to be only a part of the Portfolio Model.
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
Any help is appreciated.
class?Model::CollectionMethodsorModel::CollectionValidationsor something like that?collectionsas it has both methods and fields. Is that ok?