1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
# = Component
#
# module Foo
# extend Component
#
# def self_x; "X::x"; end
#
# def x; "x"; end
# end
#
# class X
# include Foo
# end
#
# X.x #=> "X::x"
# X.new.x #=> "x"
#
module Component
def class_module
i = self
@class_module ||= Module.new do
include i
end
# @class_module.module_eval do
# instance_methods.each do |name|
# if md = /^self_/.match(name.to_s)
# alias_method md.post_match, name
# undef_method(name)
# else
# undef_method(name)
# end
# end
# end
@class_module
end
def instance_module
i = self
@instance_module ||= Module.new do
include i
end
# @instance_module.module_eval do
# instance_methods.each do |name|
# if md = /^self_/.match(name.to_s)
# undef_method(name)
# end
# end
# end
@instance_module
end
def method_added(name)
if md = /^self_/.match(name.to_s)
class_module.send(:alias_method, md.post_match, name)
class_module.send(:undef_method, name)
instance_module.send(:undef_method, name)
else
class_module.send(:undef_method, name)
end
end
def included(base)
return if base.name.empty?
base.extend(class_module)
end
def append_features(base)
return super if base.name.empty?
instance_module.send(:append_features, base)
end
#def included(base)
# base.extend class_module
# instance_methods(false).each do |name|
# if md = /^self_/.match(name.to_s)
# base.send(:undef_method, name)
# end
# end
#end
end
# __TEST__
=begin test
require 'test/unit'
class TestComponent < Test::Unit::TestCase
module Foo
extend Component
def x; "x"; end
def self_x; "X::x"; end
#def x; "x"; end
end
class X
include Foo
end
def test_class_method
assert_equal("X::x", X.x)
end
def test_instance_method
assert_equal("x", X.new.x)
end
end
=end
|