Module: Abstract

Included in:
AbstractNode, Mocks::Mock, RandomGenerators::RandomGenerator
Defined in:
lib/abstract.rb

Overview

When this module is included into a class, this class can’t be instantiate any more until the Concrete module is included too. This module cannot be included if the Concrete module has been included before in one of the class’ superclass.

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/abstract.rb', line 19

def self.included(klass)
  super
  if klass.include?(Concrete)
    raise(TypeError, "#{klass} - cannot make abstract a concrete class")
  end
  klass.module_eval do
    class << self
      unless private_method_defined?(:concrete_new)
        alias_method :concrete_new, :new
        visibility = instance_method_visibility('new')
        def new(*args, &block)
          raise(TypeError, "cannot instantiate an abstract class #{name}")
        end
        send(visibility, :new)
        private :concrete_new
      end
    end
  end
end