Created
September 12, 2013 19:56
-
-
Save semanticart/6542970 to your computer and use it in GitHub Desktop.
quick & dirty hack at implementing template method / abstracts / insanity
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
module TemplateMethod | |
module TemplateInitializer | |
def initialize(*args) | |
unimplemented = self.class::TEMPLATE_METHODS.select do |method_name| | |
!respond_to?(method_name) | |
end | |
raise "#{self.class} should implement #{unimplemented.join(', ')}" unless unimplemented.empty? | |
super | |
end | |
end | |
def self.included(parent) | |
parent.define_singleton_method :template_methods do |*methods_to_implement| | |
parent.const_set('TEMPLATE_METHODS', methods_to_implement) | |
def self.inherited(base) | |
base.send(:include, TemplateInitializer) | |
end | |
end | |
end | |
end | |
class Foo | |
include TemplateMethod | |
template_methods :cats, :dogs | |
end | |
class Baz < Foo | |
def cats | |
end | |
def dogs | |
end | |
end | |
class Bar < Foo | |
def cats | |
end | |
end | |
p Baz.new # ok | |
p Bar.new # uh oh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment