Skip to content

Instantly share code, notes, and snippets.

@apotonick
Last active August 29, 2015 13:56
Show Gist options
  • Save apotonick/9344743 to your computer and use it in GitHub Desktop.
Save apotonick/9344743 to your computer and use it in GitHub Desktop.
Inheritable Module Class Methods
# 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
# 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