插件扩展

来源:互联网 发布:php登陆页面实例 编辑:程序博客网 时间:2024/05/29 18:17

如何实现一个简单的插件?下面实现一个在model中能输出hello world的插件。

注:()中的斜体是本人的心得体会,可忽略。

 

第一步,在工程目录下新建一个插件,运行

Shell代码  收藏代码
  1. rails generate plugin HelloWorld  


这个命令会生成以下目录文件:

Shell代码  收藏代码
  1. create  vendor/plugins/hello_world  
  2. create  vendor/plugins/hello_world/MIT-LICENSE  
  3. create  vendor/plugins/hello_world/README  
  4. create  vendor/plugins/hello_world/Rakefile  
  5. create  vendor/plugins/hello_world/init.rb  
  6. create  vendor/plugins/hello_world/install.rb  
  7. create  vendor/plugins/hello_world/uninstall.rb  
  8. create  vendor/plugins/hello_world/lib  
  9. create  vendor/plugins/hello_world/lib/hello_world.rb  
  10. invoke  test_unit  
  11. inside    vendor/plugins/hello_world  
  12. create      test  
  13. create      test/hello_world_test.rb  
  14. create      test/test_helper.rb  

这里面比较重要的文件就是init.rb和lib/hello_world.rb,在插件被加载时,init.rb会先被加载,完成初始化,lib下放实现代码库。

 

第二步,编辑lib下的hello_world.rb文件,定义一个实现输出Hello World的方法say

hello_world.rb:

Ruby代码  收藏代码
  1. module HelloWorld  
  2.    def say  
  3.      p 'Hello World!'  
  4.    end  
  5. end  

 

第三步,插件完成了,下面在model中使用这个插件。在init.rb文件里加入

init.rb:

Ruby代码  收藏代码
  1. ActiveRecord::Base.send(:include, HelloWorld)  

这样就为所有的model都混入了HelloWorld,say方法也就成了model里的实例方法了。


这样就在ActiveRcord:Base里混入了HelloWorld模块,而model又是继承于ActiveRecord::Base,所以model就能直接调用HelloWorld中的静态方法了。
send所发送的消息,在程序运行时是可变的,而且,send还能打开module或类中的private方法,使用上更加灵活。


在model中使用,post.rb:

Ruby代码  收藏代码
  1. class Post < ActiveRecord::Base  
  2. end  
 

在控制台中看一下结果: 

Shell代码  收藏代码
  1. 1.8.7 :001 >  Post.new.say  
  2. "Hello World!"  
  3.  => nil   

没有问题!一个简单的插件就完成了,但这样HelloWorld里的方法对所有的model都是打开的,如果不想这样,可以这样这样写

hello_world.rb:

Ruby代码  收藏代码
  1. module HelloWorld  
  2.    def self.included(base)  
  3.      base.extend(ClassMethods)  
  4.    end  
  5.      
  6.    module ClassMethods      
  7.       def hellolize   
  8.         include HelloWorld::InstanceMethods  
  9.       end                    
  10.    end  
  11.    
  12.    module InstanceMethods  
  13.      def say  
  14.        p 'Hello World!'  
  15.      end  
  16.    end  
  17. end  

 

当HelloWorld模块被include时,方法included将被调用,混含的类的名字被传入,再调用了extend方法,ClassMethods模块中的方法就成了混入类中的类方法了,就可以直接调用了。当hellolize方法被调用时,InstanceMethods就被混入类中了,成了混入类中的实例方法了。这样做的好处是,我想在哪个model里便用say方法,就在哪个model里调用hellolize方法,这样做保证了方法的安全性


init.rb:

Ruby代码  收藏代码
  1. ActiveRecord::Base.send(:include, HelloWorld)  


在model中使用,post.rb:

Ruby代码  收藏代码
  1. class Post < ActiveRecord::Base  
  2.   hellolize  
  3. end  

 

在控制台中看一下结果: 

Shell代码  收藏代码
  1. 1.8.7 :001 > Post.new.say  
  2. "Hello World!"  
  3.  => nil   

 

以上只是一个实现过程,可以根据实际情况把重复功能代码写成插件使用。

当然,一个完整的插件有完整的验证和测试,此例子纯粹供练手,仅供参考。