Skip to content

Instantly share code, notes, and snippets.

@semanticart
Created September 12, 2013 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semanticart/6542970 to your computer and use it in GitHub Desktop.
Save semanticart/6542970 to your computer and use it in GitHub Desktop.
quick & dirty hack at implementing template method / abstracts / insanity
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