aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/module/delegation.rb
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2007-12-05 21:45:35 +0000
committerMarcel Molina <marcel@vernix.org>2007-12-05 21:45:35 +0000
commitcdc4e93dfa46032b136d67bdb71b1e1f3d99cd1a (patch)
tree443ed22060d8a22abb3947319ccb99e77dbc9b0a /activesupport/lib/active_support/core_ext/module/delegation.rb
parentb9e0b7279506fe6f847aac2606a5c3d8eb7a6992 (diff)
downloadrails-cdc4e93dfa46032b136d67bdb71b1e1f3d99cd1a.tar.gz
rails-cdc4e93dfa46032b136d67bdb71b1e1f3d99cd1a.tar.bz2
rails-cdc4e93dfa46032b136d67bdb71b1e1f3d99cd1a.zip
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
Diffstat (limited to 'activesupport/lib/active_support/core_ext/module/delegation.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb20
1 files changed, 20 insertions, 0 deletions
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]