Sha256: 99a5b57e0d9ac8f46fd7487e08f695bb14d9e05a48ee1d7cde194b189c0f7a4f

Contents?: true

Size: 1.38 KB

Versions: 11

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true
# typed: true

module T::Private::Abstract::Declare
  Abstract = T::Private::Abstract
  AbstractUtils = T::AbstractUtils

  def self.declare_abstract(mod, type:)
    if AbstractUtils.abstract_module?(mod)
      raise "#{mod.name} is already declared as abstract"
    end

    Abstract::Data.set(mod, :can_have_abstract_methods, true)
    Abstract::Data.set(mod.singleton_class, :can_have_abstract_methods, true)
    Abstract::Data.set(mod, :abstract_type, type)

    mod.extend(Abstract::Hooks)
    mod.extend(T::InterfaceWrapper::Helpers)

    if mod.is_a?(Class)
      if type == :interface
        # Since `interface!` is just `abstract!` with some extra validation, we could technically
        # allow this, but it's unclear there are good use cases, and it might be confusing.
        raise "Classes can't be interfaces. Use `abstract!` instead of `interface!`."
      end

      if mod.instance_method(:initialize).owner == mod
        raise "You must call `abstract!` *before* defining an initialize method"
      end

      # Don't need to silence warnings via without_ruby_warnings when calling
      # define_method because of the guard above

      mod.send(:define_method, :initialize) do |*args, &blk|
        if self.class == mod
          raise "#{mod} is declared as abstract; it cannot be instantiated"
        end
        super(*args, &blk)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sorbet-runtime-0.4.4446 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4445 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4443 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4442 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4440 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4439 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4438 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4437 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4436 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4435 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.4433 lib/types/private/abstract/declare.rb