Sha256: 94b9c2ca5580da2e5e8695bbd01359b1f4e77b9b9ecab43ab9bda56116c6f8b4

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

#require 'facet/module/revisal'

require 'facet/module/rename'
require 'facet/module/redef'
require 'facet/module/remove'
require 'facet/module/nodef'
require 'facet/module/wrap'

# Using integrate is just like using include except the
# module included is a reconstruction of the one give
# via commands given in a 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

8 entries across 8 versions & 1 rubygems

Version Path
facets-1.4.0 lib/facets/core/module/integrate.rb
facets-1.4.1 lib/facets/core/module/integrate.rb
facets-1.4.2 lib/facets/core/module/integrate.rb
facets-1.4.3 lib/facets/core/module/integrate.rb
facets-1.4.5 lib/facets/core/module/integrate.rb
facets-1.4.4 lib/facets/core/module/integrate.rb
facets-1.7.0 lib/facets/core/module/integrate.rb
facets-1.7.30 lib/facets/core/module/integrate.rb