Sha256: 480254bc1e4ec3e3fbb52df1de809e9077c0c7a1003e0ed22a804f1a613dab84

Contents?: true

Size: 773 Bytes

Versions: 11

Compression:

Stored size: 773 Bytes

Contents

# frozen_string_literal: true

require "much-rails/mixin"

# MuchRails::AbstractClass overrides the `new` class method to prevent a class
# from being instantiated directly.
module MuchRails::AbstractClass
  include MuchRails::Mixin

  after_mixin_included do
    self.abstract_class = self

    define_singleton_method(:new) do |*args, &block|
      if abstract_class?
        raise(
          NotImplementedError,
          "#{self} is an abstract class and cannot be instantiated.",
        )
      end

      super(*args, &block)
    end
  end

  mixin_class_methods do
    def abstract_class
      @abstract_class
    end

    def abstract_class=(value)
      @abstract_class = value
    end

    def abstract_class?
      !!(abstract_class == self)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
much-rails-0.4.3 lib/much-rails/abstract_class.rb
much-rails-0.4.2 lib/much-rails/abstract_class.rb
much-rails-0.4.1 lib/much-rails/abstract_class.rb
much-rails-0.4.0 lib/much-rails/abstract_class.rb
much-rails-0.3.0 lib/much-rails/abstract_class.rb
much-rails-0.2.8 lib/much-rails/abstract_class.rb
much-rails-0.2.7 lib/much-rails/abstract_class.rb
much-rails-0.2.6 lib/much-rails/abstract_class.rb
much-rails-0.2.5 lib/much-rails/abstract_class.rb
much-rails-0.2.4 lib/much-rails/abstract_class.rb
much-rails-0.2.3 lib/much-rails/abstract_class.rb