Ruby接口,对象集合

Ruby接口,对象集合,ruby,activerecord,interface,sinatra,Ruby,Activerecord,Interface,Sinatra,我创建了一个名为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

我创建了一个名为
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
我想使用这个
集合
来保存Models::Company的对象列表,以及我将来将添加到Portfolio模型中的其他列表。我希望这个公司列表只是投资组合模型的一部分

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

非常感谢您的帮助。

我看到您来自Java世界,我想您希望将Java的泛型引入Ruby。但是,首先,为什么Java有泛型?让我们上一堂历史课

在早期Java(1.5之前)中,没有泛型类型,因此程序员必须编写如下代码:

List list = new ArrayList();
// add some strings to the list
list.add("foo");
list.add("bar");

// we have to iterate over each element as an Object
for (Object obj : list) {
  // and then cast it to String
  String str = (String) obj;
  // in order to call String methods on it.
  String uppercased = str.toUpperCase();

  // ...
}
这肯定不是干的。为了减轻强制转换的痛苦,Java1.5引入了泛型

List<String> list = new ArrayList<String>();
// add some strings to the list
list.add("foo");
list.add("bar");

// now we can iterate over the elements as strings
for (String str : list) {
  // no more casting, yay!
  String uppercased = str.toUpperCase();

  // ...
}
所以rubyists通常不定义容器类,他们只使用数组。如果应该应用类型限制,则在将对象添加到数组中时,它们只应用类型限制

list = []
list << something if something.is_a? Portfolio
list=[]

list我看到您来自Java世界,我想您希望将Java的泛型引入Ruby。但是,首先,为什么Java有泛型?让我们上一堂历史课

在早期Java(1.5之前)中,没有泛型类型,因此程序员必须编写如下代码:

List list = new ArrayList();
// add some strings to the list
list.add("foo");
list.add("bar");

// we have to iterate over each element as an Object
for (Object obj : list) {
  // and then cast it to String
  String str = (String) obj;
  // in order to call String methods on it.
  String uppercased = str.toUpperCase();

  // ...
}
这肯定不是干的。为了减轻强制转换的痛苦,Java1.5引入了泛型

List<String> list = new ArrayList<String>();
// add some strings to the list
list.add("foo");
list.add("bar");

// now we can iterate over the elements as strings
for (String str : list) {
  // no more casting, yay!
  String uppercased = str.toUpperCase();

  // ...
}
所以rubyists通常不定义容器类,他们只使用数组。如果应该应用类型限制,则在将对象添加到数组中时,它们只应用类型限制

list = []
list << something if something.is_a? Portfolio
list=[]

在模块中使用术语“抽象”的列表确实会让人感到困惑。这有一个特定的含义,它与类有关,尽管Ruby根本不使用这个术语,因为没有标准的方法来表示抽象基类。此外,还不清楚问题出在哪里。@tadman:对不起,我应该把它改成
?不,我的意思是它是一个混合模块,但称它为“抽象”是误导。为什么不把它叫做
Model::CollectionMethods
Model::CollectionValidations
之类的东西呢?啊,明白了。完成。我称它为
collections
,因为它既有方法也有字段。这样可以吗?在模块中使用术语“抽象”确实会让人困惑。这有一个特定的含义,它与类有关,尽管Ruby根本不使用这个术语,因为没有标准的方法来表示抽象基类。此外,还不清楚问题出在哪里。@tadman:对不起,我应该把它改成
?不,我的意思是它是一个混合模块,但称它为“抽象”是误导。为什么不把它叫做
Model::CollectionMethods
Model::CollectionValidations
之类的东西呢?啊,明白了。完成。我称它为
collections
,因为它既有方法也有字段。可以吗?谢谢你,这确实有助于澄清很多概念!谢谢,这确实有助于澄清许多概念!