Sha256: 66046db3800bcf49ece6c6a140f1ee3f360d6d6ead3b315e6d32a370af297cad

Contents?: true

Size: 853 Bytes

Versions: 793

Compression:

Stored size: 853 Bytes

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  # validates that the provided value is within a given set/enum
  class Enum < Base
    extend T::Sig

    attr_reader :values

    # TODO Ideally Hash would not be accepted but there are a lot of uses with prop enum.
    sig {params(values: T.any(Array, Set, Hash, T::Range[T.untyped])).void}
    def initialize(values)
      @values = values
    end

    # @override Base
    def valid?(obj)
      @values.member?(obj)
    end

    # @override Base
    private def subtype_of_single?(other)
      case other
      when Enum
        (other.values - @values).empty?
      else
        false
      end
    end

    # @override Base
    def name
      "T.enum([#{@values.map(&:inspect).join(', ')}])"
    end

    # @override Base
    def describe_obj(obj)
      obj.inspect
    end
  end
end

Version data entries

793 entries across 793 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.9019 lib/types/types/enum.rb
sorbet-runtime-0.5.9013 lib/types/types/enum.rb
sorbet-runtime-0.5.9006 lib/types/types/enum.rb
sorbet-runtime-0.5.9001 lib/types/types/enum.rb
sorbet-runtime-0.5.9000 lib/types/types/enum.rb
sorbet-runtime-0.5.8993 lib/types/types/enum.rb
sorbet-runtime-0.5.6539 lib/types/types/enum.rb
sorbet-runtime-0.5.6537 lib/types/types/enum.rb
sorbet-runtime-0.5.6530 lib/types/types/enum.rb
sorbet-runtime-0.5.6526 lib/types/types/enum.rb
sorbet-runtime-0.5.6519 lib/types/types/enum.rb
sorbet-runtime-0.5.6517 lib/types/types/enum.rb
sorbet-runtime-0.5.6514 lib/types/types/enum.rb
sorbet-runtime-0.5.6513 lib/types/types/enum.rb
sorbet-runtime-0.5.6511 lib/types/types/enum.rb
sorbet-runtime-0.5.6508 lib/types/types/enum.rb
sorbet-runtime-0.5.6500 lib/types/types/enum.rb
sorbet-runtime-0.5.6497 lib/types/types/enum.rb
sorbet-runtime-0.5.6492 lib/types/types/enum.rb
sorbet-runtime-0.5.6486 lib/types/types/enum.rb