Sha256: 9483d323e5a95c385a8b85c87f9b55109d9e1126ad10dab6e69cd0fdc8a504d5

Contents?: true

Size: 1.06 KB

Versions: 108

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  # Validates that an object belongs to the specified class.
  class ClassOf < Base
    attr_reader :type

    def initialize(type)
      @type = type
    end

    def build_type
      nil
    end

    # overrides Base
    def name
      "T.class_of(#{@type})"
    end

    # overrides Base
    def valid?(obj)
      obj.is_a?(Module) && obj <= @type
    end

    # overrides Base
    def subtype_of_single?(other)
      case other
      when ClassOf
        @type <= other.type
      when Simple
        @type.is_a?(other.raw_type)
      when TypedClass
        true
      else
        false
      end
    end

    # overrides Base
    def describe_obj(obj)
      obj.inspect
    end

    # So that `T.class_of(...)[...]` syntax is valid.
    # Mirrors the definition of T::Generic#[] (generics are erased).
    #
    # We avoid simply writing `include T::Generic` because we don't want any of
    # the other methods to appear (`T.class_of(A).type_member` doesn't make sense)
    def [](*types)
      self
    end
  end
end

Version data entries

108 entries across 108 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11495 lib/types/types/class_of.rb
sorbet-runtime-0.5.11493 lib/types/types/class_of.rb
sorbet-runtime-0.5.11492 lib/types/types/class_of.rb
sorbet-runtime-0.5.11491 lib/types/types/class_of.rb
sorbet-runtime-0.5.11487 lib/types/types/class_of.rb
sorbet-runtime-0.5.11485 lib/types/types/class_of.rb
sorbet-runtime-0.5.11481 lib/types/types/class_of.rb
sorbet-runtime-0.5.11480 lib/types/types/class_of.rb
sorbet-runtime-0.5.11478 lib/types/types/class_of.rb
sorbet-runtime-0.5.11475 lib/types/types/class_of.rb
sorbet-runtime-0.5.11473 lib/types/types/class_of.rb
sorbet-runtime-0.5.11471 lib/types/types/class_of.rb
sorbet-runtime-0.5.11466 lib/types/types/class_of.rb
sorbet-runtime-0.5.11465 lib/types/types/class_of.rb
sorbet-runtime-0.5.11463 lib/types/types/class_of.rb
sorbet-runtime-0.5.11460 lib/types/types/class_of.rb
sorbet-runtime-0.5.11450 lib/types/types/class_of.rb
sorbet-runtime-0.5.11447 lib/types/types/class_of.rb
sorbet-runtime-0.5.11444 lib/types/types/class_of.rb
sorbet-runtime-0.5.11439 lib/types/types/class_of.rb