Top Level Namespace

Defined Under Namespace

Modules: RailsBootstrapHelpers Classes: AbstractError

Instance Method Summary collapse

Instance Method Details

#abstract(*args) ⇒ Object

Class method that marks one or several methods as abstract, or a whole class. If an abstract method is called an AbstractError will be thrown.

Note: when making a whole class abstract the call to "abstract" must come after any constructors.

Parameters

Examples

class Foo
abstract
end

class Bar < Foo
end

Foo.new
# => AbstractError: Cannot instantiate abstract class Foo.

Bar.new
# => #<Bar:0x100123a30>

class Base
abstract :foo
end

class Sub < Base
end

class Foo < Base
def foo
  3
end
end

Sub.new.foo
# => AbstractError: Unimplemented abstract method foo.

Foo.new.foo
# => 3


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails-bootstrap-helpers/core_ext/abstract.rb', line 48

def abstract(*args)
  if args.length == 0
    class_eval do
      alias __abstract_initialize__ initialize

      def initialize (*params, &block)
        raise AbstractError.new("Cannot instantiate abstract class #{self.class.name}.")
      end

      def self.inherited (subclass)
        subclass.send(:define_method, :initialize) do |*args|
          __abstract_initialize__ *args
        end
      end
    end
  else
    class_eval do
      args.each do |name|
        define_method name do |*params, &block|
          raise AbstractError.new("Unimplemented abstract method #{name}.")
        end
      end
    end
  end
end