Created
August 6, 2012 10:15
-
-
Save paneq/3273206 to your computer and use it in GitHub Desktop.
Prevent including module if target already has some of its instance methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A | |
extend SelfishModule | |
def abc; end | |
end | |
module B | |
extend SelfishModule | |
def bcd; end | |
end | |
module Aa | |
extend SelfishModule | |
def abc; end | |
end | |
o = Object.new | |
o.extend(A) | |
o.extend(B) | |
o.extend(Aa) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A | |
extend SelfishModule | |
def abc; end | |
end | |
module B | |
extend SelfishModule | |
def bcd; end | |
end | |
module Aa | |
extend SelfishModule | |
def abc; end | |
end | |
class X | |
include A | |
include B | |
include Aa | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RuntimeError: Methods already implemented: [:abc] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SelfishModule | |
def append_features(target) | |
target_methods = target.instance_methods + target.private_instance_methods | |
my_methods = instance_methods + private_instance_methods | |
common_methods = target_methods & my_methods | |
raise "Methods already implemented: #{common_methods}" unless common_methods.empty? | |
super | |
end | |
def extend_object(target) | |
target_methods = target.methods(true) + target.private_methods(true) | |
my_methods = instance_methods + private_instance_methods | |
common_methods = target_methods & my_methods | |
raise "Methods already implemented: #{common_methods}" unless common_methods.empty? | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment