내 프로젝트 모델의 객체 컬렉션을 유지하기 위해 Collection
이라는 인터페이스를 만들었습니다. 나는이 컬렉션과 배열을 다른 필드가 원하는대로 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
도움을 주시면 감사하겠습니다.
모듈에서 "초록"이라는 용어를 사용하면 실제로 사람들을 혼란스럽게합니다. 루비는 추상적 인 기본 클래스를 표현하기위한 표준 방법이 없기 때문에 루비는이 용어를 전혀 사용하지 않지만 구체적인 의미를 가지며 클래스와 관련이 있습니다. 또한 문제가 무엇인지 분명하지 않습니다. – tadman
@tadman : 죄송합니다. 클래스로 변경해야합니까? – chipmunk
아니요, 믹스 인 모듈이라고해도 괜찮습니다. 그러나 "초록"이라고 부르는 것은 잘못된 것입니다. 왜 그것을'Model :: CollectionMethods' 또는'Model :: CollectionValidations' 또는 이와 비슷한 이름으로 부르지 않습니까? – tadman