Sha256: 0f6131acca4b8f5d806416ff3edb91003216f37e4be84cda556925bd26132aec

Contents?: true

Size: 1.51 KB

Versions: 251

Compression:

Stored size: 1.51 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} is already declared as abstract"
    end
    if T::Private::Final.final_module?(mod)
      raise "#{mod} was already declared as final and cannot be 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

251 entries across 251 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.5181 lib/types/private/abstract/declare.rb
sorbet-runtime-0.5.5179 lib/types/private/abstract/declare.rb
sorbet-runtime-0.5.5176 lib/types/private/abstract/declare.rb
sorbet-runtime-0.5.5175 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5173 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5168 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5163 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5158 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5146 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5144 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5140 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5136 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5135 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5133 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5129 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5109 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5099 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5095 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5085 lib/types/private/abstract/declare.rb
sorbet-runtime-0.4.5083 lib/types/private/abstract/declare.rb