Created
April 15, 2015 19:00
-
-
Save tdg5/99a7ec0ddda0e1729a04 to your computer and use it in GitHub Desktop.
PartiallyImplementedInterface mixin
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 PartiallyImplementedInterface | |
def self.not_implemented_error(klass, method) | |
raise NotImplementedError, not_implemented_message(klass, method), caller(2) | |
end | |
def self.not_implemented_message(klass, method_name) | |
"#{method_name} must be implemented by subclasses of #{klass.name}!" | |
end | |
private_class_method :not_implemented_message | |
def not_implemented(*methods) | |
methods.each do |iface_method| | |
define_method(iface_method) do | |
PartiallyImplementedInterface.not_implemented_error(self.class, iface_method) | |
end | |
end | |
end | |
end | |
class Meow | |
extend PartiallyImplementedInterface | |
not_implemented :nesting | |
def lots_of_nesting; lots; end | |
def lots; of; end | |
def of; nesting; end | |
end | |
Meow.new.lots_of_nesting | |
# example.rb:27:in `of': nesting must be implemented by subclasses of Meow! (NotImplementedError) | |
# from example.rb:26:in `lots' | |
# from example.rb:25:in `lots_of_nesting' | |
# from example.rb:30:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment