33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/abstractify/abstract.rb', line 33
def self.included(mod)
abstract_methods = []
class_methods = Module.new
class_methods.define_method :abstract do |*names|
abstract_methods.concat names.map(&:to_sym)
end
class_methods.define_method :new do |*args, **kwargs|
raise AbstractClassError.new("Class #{self} is abstract") unless self < mod
abstract_methods.each do |name|
raise AbstractMethodMustExistError.new("Method #{name} must be defined") unless method_defined? name
method = instance_method name
raise AbstractMethodMustOverrideError.new("Method #{name} must be overridden") unless method.owner < mod
end
super(*args, **kwargs)
end
mod.extend(class_methods)
end
|