Sha256: a30a3958dc568599143b99f728d9a5289baa9ca7a9ff4787dd9373ab7b48c60a

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

#require 'facets/core/module/revisal'

require 'facets/core/module/rename'
require 'facets/core/module/redef'
require 'facets/core/module/remove'
require 'facets/core/module/nodef'
require 'facets/core/module/wrap'

# Using integrate is just like using include except the
# module included is a reconstruction of the one given
# altered by the commands given in the block.
#
# Convenient commands available are: #rename, #redef,
# #remove, #nodef and #wrap. But any module method
# can be used.
#
#   module W
#     def q ; "q" ; end
#     def y ; "y" ; end
#   end
#
#   class X
#     integrate W do
#       nodef :y
#     end
#   end
#
#   x = X.new
#   x.q  #=> "q"
#   x.y  #=> missing method error
#
# This is like #revisal, but #revisal only
# returns the reconstructred module. It does not
# include it.

class Module

  def integrate( mod, &block )
    #include mod.revisal( &blk )
    m = Module.new { include mod }
    m.class_eval &block
    include m
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

=begin testing

  require 'test/unit'

  class TCModule < Test::Unit::TestCase
    module M
      def x ; 1 ; end
    end
    class C
      integrate M do
        rename :y, :x
      end
    end

    def test_integrate
      c = C.new
      assert_raises( NoMethodError ) { c.x }
      assert_equal( 1, c.y )
    end
  end

=end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-1.8.49 lib/facets/core/module/integrate.rb
facets-1.8.51 lib/facets/core/module/integrate.rb
facets-1.8.54 lib/facets/core/module/integrate.rb