继承被误解 [英] Inheritance misunderstood

查看:60
本文介绍了继承被误解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中建立继承.

I'm trying to build inheritance in my app .

事实上,这很简单,我有几个模型,其中一些需要存档",这只是意味着我要将数据从数据库公共模式转移到存档模式(同一数据库).

In fact it's simple i have several models , and some of them need to be 'archive' , it simply means that i gonna move data from my database public schemat to an archive schemat ( same database ).

每个模型都有一个通过示例保存"自身"OnArchive"的方法:

Each models have a methods 'save'itself'OnArchive by example :

 def saveContactArchive(contact)
record = ArchContacts.new
record.id=contact.id
record.information_id=contact.information_id
record.is_internal = contact.is_internal if (contact.is_internal != nil)
record.type_contact_id = contact.type_contact_id if (contact.type_contact_id != nil)
record.user_id = contact.user_id if (contact.user_id != nil)
record.info_readed = contact.info_readed if (contact.info_readed != nil)
record.not_received = contact.not_received if (contact.not_received != nil)
record.society_name = contact.society_name if (contact.society_name != nil)
record.person_name = contact.person_name if (contact.person_name != nil)
record.email = contact.email if (contact.email != nil)
record.receipt = contact.receipt if (contact.receipt != nil)
record.receipt_confirmed = contact.receipt_confirmed if (contact.receipt_confirmed != nil)
record.created_at = contact.created_at if (contact.created_at != nil)
record.updated_at = contact.updated_at if (contact.updated_at != nil)



id = contact.id
if (!existOnArchive(id))
  return record.save
else
  return true
end


  end

有些模型具有通过示例保存'arrayOf'OnArchive的方法:

And SOME of models have a methodes save'arrayOf'OnArchive by example :

 def saveContactsArchive(contacts)
resultContact = false
for c in contacts

  if(c.id != nil)
    resultContact = saveContactArchive(c)
  else
    resultContact = true
  end
  if(!resultContact)
    ArchiveLogs.debug("Sauvegarde d'un contact sur l archive echoue, contact concerne  "+c.inspect)
  end
end
return resultContact
   end

我正在尝试为所有这些Models创建一个父类,称为Archive. 此类将定义2种方法

I'm trying to create a parent class for all those Models , called Archive. This class woud define 2 methods

 class Archive
  def saveOnArchive(element)
    "Save on archive"
  end
  def saveArrayOnArchive(elements)
    "Save an array on archive"
  end
 end

saveArrayOnArchive对于所有模型都遵循相同的逻辑,例如saveContactsArchive;在数组上循环,对于每个调用saveOnArchive的元素,如果出错则写日志.

saveArrayOnArchive follow the same logic for all models , like for saveContactsArchive ; loop on array , for each element call saveOnArchive, write logs if error .

我的问题;

1)更好地创建模块化saveArrayOnArchive以及如何从父类调用子方法saveOnArchive吗?

1)is that better to create a modular saveArrayOnArchive and how to call children method saveOnArchive from parent class?

2)我的模特会是什么样子?他们是否要重新定义每种方法?如果他们不向父方法添加任何东西,就调用super?

2)What my models gonna look like ? have they to redifine every methods ? calling super if they don't add anything to parent method?

3)甚至有可能,因为我的模型已经从activerecord中删除了 类ArchContacts< ActiveRecord :: Base - - 编辑 - - 为什么要让我的模型成为ActiveRecord :: Base的子代(我只是跟随另一个没有大脑的开发者模型....) ----编辑----

3)is that even possible since my models are already childreen from activerecord class ArchContacts < ActiveRecord::Base ----EDIT---- Why shoud my models be children of ActiveRecord::Base (i just followed an other dev model without brain ....) ----EDIT----

3合1)如果有人了解我...如何获得帮助

3 in 1 ) How to achieve this if someone understood me ... any help apricieted

推荐答案

在您的情况下,它们似乎都具有相同的属性,因此您可以充分利用方法

In your case they all seem to have the same attributes, so you could make good use of the method #becomes which creates a new instance of a different class with the same attributes of the current class, for example

contact = Contact.first
archived_contact = contact.becomes ArchContact
archived_contact.save

这将创建存档的联系人,并避免您复制每个属性的所有麻烦.

This will create the archived contact and save you all the pain of copying each attribute.

首先让我解释一下方法调用的工作原理,每个实例都有一棵树,方法调用会冒泡直到找到所需的方法,因此,如果您调用saveOnArchive,实例将查看它的类并检查是否有一个具有该名称的方法,如果不是,则检查包含的模块,如果不是,则检查该类的SuperClass,然后检查该SuperClass的模块,一直到树,然后落入method_missing方法,该方法打印错误消息,指出找不到该方法.

Let me first explain how method calling works, each instance has a tree that a method call bubbles up till it finds the method you are looking for, so if you call saveOnArchive the instance will look at it's class and check if there's a method with that name, if not then it checks the included modules, if not it checks the SuperClass of the class, then the SuperClass's modules, all the way up to the tree and then falls into the method_missing method which prints the error saying that the method is not found.

我认为您不必创建对象并将其插入两个方法中,您可以include具有这些方法的模块,例如,这里是存档模块.

I don't think it's necessary for you to create an object and inheret it for just 2 methods, you could include a module that has these methods, for example here's the archive module.

module Archive
  ef method_1
  end
  def method_2
  end
end

然后将其包含在Contact类中

Then include it in the Contact class

class Contact < ActiveRecord::Base
  include Archive
end

现在,您的Contact类具有method_1method_2,因为它们存在于查找树中,但是由于您具有不同的类并且每个档案都位于不同的net表中,因此您可以添加一些动态方法来处理此问题,这是一个小例子

Now your Contact class has method_1 and method_2 because they exist in the look up tree, but since you have different classes and each archive in differnet tables, you could add a little dynamic method to handle this, here's a small example

module Archive
  def self.included(klass)
    klass.instance_eval do
      define_method "save_#{klass.name}_archive" do
        self.becomes "#{klass.name}Archive".constantize
      end
    end
  end
end

该模块使用包含它的类并获取其名称来定义动态方法,在这种情况下,contact模型将具有一个save_contact_archive方法,该方法返回填充有其属性的ContactArchive实例,您可以然后保存该对象.

The module uses the class that included it and gets its name to define dynamic methods, in this case the contact model will have a save_contact_archive method that returns an instance of ContactArchive filled with it's attributes, you can then save that object.

但是,如果一个名为User的类加入了Archive模块,则这些方法将被称为save_user_archive并将其保存到一个名为UserArchive的对象中.

But if a class called User inculded the Archive module, the methods will be called save_user_archive and it saves to an object called UserArchive.

这是一个简单的用法

contact = Contact.first
archived_contact = Contact.save_contact_archive
archived_contact.save

希望我能简化一下,如果您需要进一步的帮助,请询问.

I hope I made this simple, if you need further help please ask.

这篇关于继承被误解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆