hqt

Custom Include/Extend

hqt
Feb 5th, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.26 KB | None | 0 0
  1. # created by Huynh Quang Thao
  2. # on 02/04/2017
  3. require 'test/unit'
  4.  
  5. # ------------------------------------------------------------
  6. # implementation
  7. class Class
  8.   # def custom_extend(module_name)
  9.   #   module_name.methods(false).each do |method|
  10.   #     define_singleton_method(method) do |*args, &block|
  11.   #       module_name.send(method, *args, &block)
  12.   #     end
  13.   #   end
  14.   # end
  15.  
  16.   def custom_include(module_)
  17.     # for public and protected methods:
  18.     module_.instance_methods(include_super=false).each do |meth_name|
  19.       meth = module_.instance_method(meth_name)
  20.       define_method(meth_name) do |*args, &block|
  21.         meth.bind(self).call(*args, &block)
  22.       end
  23.     end
  24.  
  25.     # for private methods:
  26.     module_.private_instance_methods(include_super=false).each do |meth_name|
  27.       meth = module_.instance_method(meth_name)
  28.       define_method(meth_name) do |*args, &block|
  29.         meth.bind(self).call(*args, &block)
  30.       end
  31.       private :"#{meth_name}"
  32.     end
  33.   end
  34.  
  35.   def custom_extend(module_)
  36.     singleton_class.custom_include module_
  37.   end
  38. end
  39.  
  40. # -------------------------------------------------------------
  41. # Demonstration
  42. module SimpleModule
  43.   def hello_world
  44.     'Hello World'
  45.   end
  46.  
  47.   def hello_name
  48.     "Hello to #{@instance_name}"
  49.   end
  50.  
  51.   def hello_with_name(name)
  52.     res = block_given? ? yield : ''
  53.     "Hello to #{res}.#{name}"
  54.   end
  55.  
  56.   private
  57.   def private_hello
  58.     'Hello privately !!!'
  59.   end
  60. end
  61.  
  62. class SimpleClass
  63.   custom_include SimpleModule
  64.   custom_extend  SimpleModule
  65.  
  66.   @instance_name = 'Thao'
  67.  
  68.   def internal_hello
  69.     private_hello
  70.   end
  71. end
  72.  
  73. puts SimpleClass.hello_world
  74. puts SimpleClass.hello_name
  75.  
  76. puts SimpleClass.new.hello_world
  77. puts SimpleClass.new.hello_name
  78. puts SimpleClass.new.hello_with_name('thao')
  79.  
  80. # -----------------------------------------------------------------
  81. # Testing
  82. class TestUsing < Test::Unit::TestCase
  83.   def test_custom_include_simple
  84.     message = SimpleClass.new.hello_world
  85.     assert message == 'Hello World'
  86.   end
  87.  
  88.   def test_custom_include_param
  89.     message = SimpleClass.new.hello_with_name('Superman')
  90.     assert message == 'Hello to .Superman'
  91.   end
  92.  
  93.   def test_custom_include_block
  94.     message = SimpleClass.new.hello_with_name('Superman') do
  95.       'Mr'
  96.     end
  97.     assert message == 'Hello to Mr.Superman'
  98.   end
  99.  
  100.   def test_custom_include_private
  101.     assert_raise NoMethodError do
  102.       SimpleClass.new.private_hello
  103.     end
  104.  
  105.     message = SimpleClass.new.internal_hello
  106.     assert message == 'Hello privately !!!'
  107.   end
  108.  
  109.   def test_custom_extend_simple
  110.     message = SimpleClass.hello_world
  111.     assert message == 'Hello World'
  112.   end
  113.  
  114.   def test_custom_extend_shared_scope
  115.     message = SimpleClass.hello_name
  116.     assert message == 'Hello to Thao'
  117.   end
  118.  
  119.   def custom_extend_param
  120.     message = SimpleClass.hello_with_name('Supergirl')
  121.     assert message == 'Hello to .Supergirl'
  122.   end
  123.  
  124.   def test_custom_extend_block
  125.     message = SimpleClass.hello_with_name('Supergirl') do
  126.       'Miss'
  127.     end
  128.     assert message == 'Hello to Miss.Supergirl'
  129.   end
  130.  
  131.   def test_custom_extend_private
  132.     assert_raise NoMethodError do
  133.       SimpleClass.private_hello
  134.     end
  135.   end
  136. end
  137.  
  138. TestUsing.new("test extend/include")
Advertisement
Add Comment
Please, Sign In to add comment