Sha256: 587f6c9dd22102b7710aef662233afee0e3f9f87fcd769f1fb6abb6a62b6c179

Contents?: true

Size: 1.35 KB

Versions: 52

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
# typed: false

module T::Private::Final
  module NoInherit
    def inherited(arg)
      super(arg)
      raise "#{self.name} was declared as final and cannot be inherited"
    end
  end

  module NoIncludeExtend
    def included(arg)
      super(arg)
      raise "#{self.name} was declared as final and cannot be included"
    end

    def extended(arg)
      super(arg)
      raise "#{self.name} was declared as final and cannot be extended"
    end
  end

  def self.declare(mod)
    if !mod.is_a?(Module)
      raise "#{mod.inspect} is not a class or module and cannot be declared as final with `final!`"
    end
    if final_module?(mod)
      raise "#{mod.name} was already declared as final and cannot be re-declared as final"
    end
    if T::AbstractUtils.abstract_module?(mod)
      raise "#{mod.name} was already declared as abstract and cannot be declared as final"
    end
    mod.extend(mod.is_a?(Class) ? NoInherit : NoIncludeExtend)
    mark_as_final_module(mod)
    mark_as_final_module(mod.singleton_class)
    T::Private::Methods.add_module_with_final(mod)
    T::Private::Methods.install_hooks(mod)
  end

  def self.final_module?(mod)
    mod.instance_variable_defined?(:@sorbet_final_module)
  end

  private_class_method def self.mark_as_final_module(mod)
    mod.instance_variable_set(:@sorbet_final_module, true)
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
sorbet-runtime-0.4.4524 lib/types/private/final.rb
sorbet-runtime-0.4.4523 lib/types/private/final.rb
sorbet-runtime-0.4.4521 lib/types/private/final.rb
sorbet-runtime-0.4.4522 lib/types/private/final.rb
sorbet-runtime-0.4.4519 lib/types/private/final.rb
sorbet-runtime-0.4.4520 lib/types/private/final.rb
sorbet-runtime-0.4.4518 lib/types/private/final.rb
sorbet-runtime-0.4.4517 lib/types/private/final.rb
sorbet-runtime-0.4.4516 lib/types/private/final.rb
sorbet-runtime-0.4.4515 lib/types/private/final.rb
sorbet-runtime-0.4.4514 lib/types/private/final.rb
sorbet-runtime-0.4.4513 lib/types/private/final.rb
sorbet-runtime-0.4.4512 lib/types/private/final.rb
sorbet-runtime-0.4.4511 lib/types/private/final.rb
sorbet-runtime-0.4.4510 lib/types/private/final.rb
sorbet-runtime-0.4.4507 lib/types/private/final.rb
sorbet-runtime-0.4.4506 lib/types/private/final.rb
sorbet-runtime-0.4.4505 lib/types/private/final.rb
sorbet-runtime-0.4.4504 lib/types/private/final.rb
sorbet-runtime-0.4.4503 lib/types/private/final.rb