Module: Abstractify::Abstract

Defined in:
lib/abstractify/abstract.rb

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



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)
  # An array of method names as symbols.
  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