Sha256: a7e0fce9c04fc0745c482d4d9a377e14174679f2d71fe49c83ea1cf4ce1bd861

Contents?: true

Size: 1.45 KB

Versions: 379

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true
# typed: false

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

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

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

  def self.declare(mod)
    if !mod.is_a?(Module)
      raise "#{mod} is not a class or module and cannot be declared as final with `final!`"
    end
    if final_module?(mod)
      raise "#{mod} was already declared as final and cannot be re-declared as final"
    end
    if T::AbstractUtils.abstract_module?(mod)
      raise "#{mod} was already declared as abstract and cannot be declared as final"
    end
    if T::Private::Sealed.sealed_module?(mod)
      raise "#{mod} was already declared as sealed 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

379 entries across 379 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.6206 lib/types/private/final.rb
sorbet-runtime-0.5.6205 lib/types/private/final.rb
sorbet-runtime-0.5.6203 lib/types/private/final.rb
sorbet-runtime-0.5.6198 lib/types/private/final.rb
sorbet-runtime-0.5.6196 lib/types/private/final.rb
sorbet-runtime-0.5.6193 lib/types/private/final.rb
sorbet-runtime-0.5.6189 lib/types/private/final.rb
sorbet-runtime-0.5.6188 lib/types/private/final.rb
sorbet-runtime-0.5.6187 lib/types/private/final.rb
sorbet-runtime-0.5.6183 lib/types/private/final.rb
sorbet-runtime-0.5.6173 lib/types/private/final.rb
sorbet-runtime-0.5.6164 lib/types/private/final.rb
sorbet-runtime-0.5.6161 lib/types/private/final.rb
sorbet-runtime-0.5.6155 lib/types/private/final.rb
sorbet-runtime-0.5.6154 lib/types/private/final.rb
sorbet-runtime-0.5.6145 lib/types/private/final.rb
sorbet-runtime-0.5.6141 lib/types/private/final.rb
sorbet-runtime-0.5.6140 lib/types/private/final.rb
sorbet-runtime-0.5.6134 lib/types/private/final.rb
sorbet-runtime-0.5.6133 lib/types/private/final.rb