Created
          February 9, 2009 19:26 
        
      - 
      
 - 
        
Save dennis/60939 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class PluginHandler | |
| def initialize | |
| @plugins = Array.new | |
| end | |
| def add(plugin) | |
| @plugins << plugin.send(:new) | |
| end | |
| def delegate_method_to_plugins(method, *args) | |
| @plugins.each do |plugin| | |
| plugin.send(method, args) if plugin.respond_to? method | |
| end | |
| end | |
| def method_missing(method, *args) | |
| if method.to_s.match(/^on_/) | |
| delegate_method_to_plugins(method, *args) | |
| else | |
| super | |
| end | |
| end | |
| end | |
| class APlugin | |
| def on_connect(text) | |
| puts "APlugin#on_connect(#{text})" | |
| end | |
| end | |
| ph = PluginHandler.new | |
| ph.add(APlugin) | |
| ph.on_nick | |
| ph.on_connect("Hello world") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment