delegate 用法

Delegate是一种应用composite来代替extend的机制,可以有效地降低代码的耦合性。

Rails 2.2增加了delegate方法,可以十分方便地实现delegate机制。来看看源码吧:

def delegate(*methods)  
  options = methods.pop  
  unless options.is_a?(Hash) && to = options[:to]  
    raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
  end
   
  if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/  
    raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
  end
   
  prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
  
  methods.each do |method|  
    module_eval(<<-EOS, "(__DELEGATION__)", 1)  
      def #{prefix}#{method}(*args, &block)  
        #{to}.__send__(#{method.inspect}, *args, &block)  
      end
    EOS
  end
end


delegate方法首先检查传入的参数,正确参数形式为:method1, :method2, ..., :methodN, :to => klass[, :prefix => prefix]

delegate要求参数的最后必须是一个Hash,:to表示需要代理的类,:prefix表示代理的方法是否要加前缀,如果:prefix => true,则代理的方法名为klass_method1, klass_method2, ..., klass_methodN,如果:prefix => prefix (prefix为string),则代理的方法名为prefix_method1, prefix_method2, ..., prefix_methodN。

最终通过module_eval动态生成每个方法定义。通过__send__方法调用:to类的方法。

来看看调用的例子:

简单的调用:

class Greeter < ActiveRecord::Base  
  def hello()   "hello" end
  def goodbye() "goodbye" end
end
   
class Foo < ActiveRecord::Base  
  delegate :hello, :goodbye, :to => :greeter
end
   
Foo.new.hello   # => "hello"  
Foo.new.goodbye # => "goodbye"  

增加:prefix => true:

class Foo < ActiveRecord::Base  
  delegate :hello, :goodbye, :to => :greeter, :prefix => true
end
   
Foo.new.greeter_hello   # => "hello"  
Foo.new.greeter_goodbye # => "goodbye"  

自定义前缀名:

class Foo < ActiveRecord::Base  
  delegate :hello, :goodbye, :to => :greeter, :prefix => :foo
end
  
Foo.new.foo_hello   # => "hello"  
Foo.new.foo_goodbye # => "goodbye" 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值