Sha256: 1dbc0c40a6be48066dc8acb9922fcb4e60f5e294c2d3ae82367d50c88752486c

Contents?: true

Size: 1017 Bytes

Versions: 3

Compression:

Stored size: 1017 Bytes

Contents

# frozen_string_literal: true
# typed: true

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

    def initialize(raw_type)
      @raw_type = raw_type
    end

    # @override Base
    def name
      @raw_type.name
    end

    # @override Base
    def valid?(obj)
      obj.is_a?(@raw_type)
    end

    # @override Base
    private def subtype_of_single?(other)
      case other
      when Simple
        @raw_type <= other.raw_type
      else
        false
      end
    end

    def to_nilable
      @nilable ||= T::Types::Union.new([self, T::Utils::Nilable::NIL_TYPE])
    end

    module Private
      module Pool
        def self.type_for_module(mod)
          cached = mod.instance_variable_get(:@__as_sorbet_simple_type)
          return cached if cached

          type = Simple.new(mod)
          mod.instance_variable_set(:@__as_sorbet_simple_type, type) unless mod.frozen?
          type
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.5848 lib/types/types/simple.rb
sorbet-runtime-0.5.5846 lib/types/types/simple.rb
sorbet-runtime-0.5.5845 lib/types/types/simple.rb