Sha256: 5a61dde2442d845d28ee22245623ba1e844d6e7793563ac6b1687b34ed6678cd

Contents?: true

Size: 1011 Bytes

Versions: 10

Compression:

Stored size: 1011 Bytes

Contents

class Module

  #--
  # Don't think we should bother making this private.
  # That sort of defeats some of the AOP usefulness of this.
  #private
  #++

  # Creates a new method wrapping the previous of
  # the same name. Reference to the old method
  # is passed into the new definition block
  # as the first parameter.
  #
  #   wrap_method( sym ) { |old_meth, *args|
  #     old_meth.call
  #     ...
  #   }
  #
  # Keep in mind that this can not be used to wrap methods
  # that take a block.

  def wrap_method( sym, &blk )
    old = instance_method(sym)
    define_method(sym) { |*args| blk.call(old.bind(self), *args) }
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCModule < Test::Unit::TestCase

    def fixture_method; "A"; end

    wrap_method( :fixture_method ) { |old| old.call + "B" }

    def test_wrap_method
      assert_equal( "AB", fixture_method )
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-1.7.30 lib/facets/core/module/wrap_method.rb
facets-1.7.0 lib/facets/core/module/wrap_method.rb
facets-1.7.38 lib/facets/core/module/wrap_method.rb
facets-1.7.46 lib/facets/core/module/wrap_method.rb
facets-1.8.0 lib/facets/core/module/wrap_method.rb
facets-1.8.20 lib/facets/core/module/wrap_method.rb
facets-1.8.49 lib/facets/core/module/wrap_method.rb
facets-1.8.51 lib/facets/core/module/wrap_method.rb
facets-1.8.54 lib/facets/core/module/wrap_method.rb
facets-1.8.8 lib/facets/core/module/wrap_method.rb