
require 'facets/core/module/namespace'

class Module

  # Include a module via a specified namespace.
  #
  #   module T
  #     def t ; "HERE" ; end
  #   end
  #
  #   class X
  #     include_as :test => T
  #     def t ; test.t ; end
  #   end
  #
  #   X.new.t  #=> "HERE"
  #

  def include_as( h )
    h.each { |name, mod| namespace(name, mod) }
  end

end



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

  require 'test/unit'

  class TestIncludeAs < Test::Unit::TestCase

    module MockModule
      def x; "X"; end
      def y; @y; end
    end

    class MockObject
      include_as :mod => MockModule

      def initialize
        @y = "Y"
      end
    end

    def test_01
      m = MockObject.new
      assert_equal( "X", m.mod.x )
    end

    def test_02
      m = MockObject.new
      assert_equal( "Y", m.mod.y )
    end

  end

=end



# devlopment testing

=begin

if __FILE__ == $0

  # test

  module T
    def t ; p self ; end
    def r ; p self ; end
    def q ; super ; end
  end

  class N
    def q ; puts "ho" ; end
  end

  class X < N
    include_as :test => T

    def n ; p test ; end
    def m ; test.t ; end
    def o ; test.r ; end
  end

  x = X.new
  x.n
  x.m
  x.o
  x.q

end

=end
