From cdc4e93dfa46032b136d67bdb71b1e1f3d99cd1a Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Wed, 5 Dec 2007 21:45:35 +0000 Subject: Document that the delegate method can delegate to things other than just methods. Closes #7184 [dcmanges, jeremymcanally] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8311 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/module/delegation.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index 34e4bf9397..f6647eaed3 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -5,6 +5,7 @@ class Module # or string). At least one method and the :to option are required. # # Delegation is particularly useful with Active Record associations: + # # class Greeter < ActiveRecord::Base # def hello() "hello" end # def goodbye() "goodbye" end @@ -25,6 +26,25 @@ class Module # end # # Foo.new.goodbye # => "goodbye" + # + # Methods can be delegated to instance variables, class variables, or constants + # by providing the variable as a symbol: + # class Foo + # CONSTANT_ARRAY = [0,1,2,3] + # @@class_array = [4,5,6,7] + # + # def initialize + # @instance_array = [8,9,10,11] + # end + # delegate :sum, :to => :CONSTANT_ARRAY + # delegate :min, :to => :@@class_array + # delegate :max, :to => :@instance_array + # end + # + # Foo.new.sum # => 6 + # Foo.new.min # => 4 + # Foo.new.max # => 11 + # def delegate(*methods) options = methods.pop unless options.is_a?(Hash) && to = options[:to] -- cgit v1.2.3