Last active
August 29, 2015 13:56
-
-
Save apotonick/9344743 to your computer and use it in GitHub Desktop.
Inheritable Module Class Methods
This file contains 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
# Here's the original problem: | |
module Feature | |
module ClassMethods | |
def feature | |
end | |
end | |
def self.included(includer) | |
includer.extend ClassMethods | |
end | |
end | |
# i want the ::included method to be "inherited", so that this works: | |
module Client | |
include Feature | |
end | |
module Extension | |
include Client | |
end | |
module Plugin | |
include Extension | |
end | |
module Framework | |
include Plugin | |
end | |
Client.feature | |
Extension.feature | |
Plugin.feature | |
Framework.feature |
This file contains 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
# here's a dead simple solution. | |
module Feature | |
module ClassMethods | |
def feature | |
end | |
end | |
InheritedIncludedCodeBlock = lambda do |includer| | |
includer.extend ClassMethods | |
end | |
module RecursiveIncluded | |
def included(includer) | |
#super # TODO: test me. | |
puts "RecursiveIncluded in #{includer}" | |
includer.module_eval do | |
InheritedIncludedCodeBlock.call(includer) | |
extend RecursiveIncluded | |
end | |
end | |
end | |
extend RecursiveIncluded | |
end | |
# 1. i use a recursive ::included to inherit the behaviour to including modules | |
# 2. the only problem is referencing the InheritedIncludedCodeBlock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment