Sha256: 9da822fae2e5325263e3a2abf56a4b13c1e025dbadc97a34f14921ea637de32d

Contents?: true

Size: 1.98 KB

Versions: 25

Compression:

Stored size: 1.98 KB

Contents

class Module
  # Provides a delegate class method to easily expose contained objects' methods
  # as your own. Pass one or more methods (specified as symbols or strings)
  # and the name of the target object as the final <tt>:to</tt> option (also a symbol
  # or string).  At least one method and the <tt>:to</tt> option are required.
  #
  # Delegation is particularly useful with Active Record associations:
  #
  #   class Greeter < ActiveRecord::Base
  #     def hello()   "hello"   end
  #     def goodbye() "goodbye" end
  #   end
  #
  #   class Foo < ActiveRecord::Base
  #     belongs_to :greeter
  #     delegate :hello, :to => :greeter
  #   end
  #
  #   Foo.new.hello   # => "hello"
  #   Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>
  #
  # Multiple delegates to the same target are allowed:
  #
  #   class Foo < ActiveRecord::Base
  #     belongs_to :greeter
  #     delegate :hello, :goodbye, :to => :greeter
  #   end
  #
  #   Foo.new.goodbye # => "goodbye"
  #
  # Methods can be delegated to instance variables, class variables, or constants
  # by providing them as a symbols:
  #
  #   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]
      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

    methods.each do |method|
      module_eval(<<-EOS, "(__DELEGATION__)", 1)
        def #{method}(*args, &block)
          #{to}.__send__(#{method.inspect}, *args, &block)
        end
      EOS
    end
  end
end

Version data entries

25 entries across 25 versions & 7 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
masover-castronaut-0.4.4.4 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
masover-castronaut-0.4.4.5 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
masover-castronaut-0.5.0.1 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.4.1 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.4.2 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.4.3 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.4.4 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.4.5 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.4.6 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.5.0 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.5.1 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.5.2 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.5.3 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
relevance-castronaut-0.5.4 vendor/activesupport/lib/active_support/core_ext/module/delegation.rb
radiant-0.7.2 vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb
vibes-bj-1.2.2 spec/rails_root/vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb
vibes-bj-1.2.1 spec/rails_root/vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb
activesupport-2.1.1 lib/active_support/core_ext/module/delegation.rb
activesupport-2.1.0 lib/active_support/core_ext/module/delegation.rb