Sha256: 44b86f060f278127e6a0a91f853f29255ad7c4c3c82041bf104c022fa263f53f

Contents?: true

Size: 1.1 KB

Versions: 98

Compression:

Stored size: 1.1 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 || false)
    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
        @type.is_a?(other.underlying_class)
      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

98 entries across 98 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11766 lib/types/types/class_of.rb
sorbet-runtime-0.5.11765 lib/types/types/class_of.rb
sorbet-runtime-0.5.11763 lib/types/types/class_of.rb
sorbet-runtime-0.5.11761 lib/types/types/class_of.rb
sorbet-runtime-0.5.11758 lib/types/types/class_of.rb
sorbet-runtime-0.5.11755 lib/types/types/class_of.rb
sorbet-runtime-0.5.11751 lib/types/types/class_of.rb
sorbet-runtime-0.5.11746 lib/types/types/class_of.rb
sorbet-runtime-0.5.11742 lib/types/types/class_of.rb
sorbet-runtime-0.5.11734 lib/types/types/class_of.rb
sorbet-runtime-0.5.11725 lib/types/types/class_of.rb
sorbet-runtime-0.5.11718 lib/types/types/class_of.rb
sorbet-runtime-0.5.11717 lib/types/types/class_of.rb
sorbet-runtime-0.5.11711 lib/types/types/class_of.rb
sorbet-runtime-0.5.11710 lib/types/types/class_of.rb
sorbet-runtime-0.5.11709 lib/types/types/class_of.rb
sorbet-runtime-0.5.11708 lib/types/types/class_of.rb
sorbet-runtime-0.5.11707 lib/types/types/class_of.rb
sorbet-runtime-0.5.11704 lib/types/types/class_of.rb
sorbet-runtime-0.5.11699 lib/types/types/class_of.rb