Sha256: 6b7200bd93b78c3305869ef28f7f9edde712afde2862a2b5364f8e2ac12e7e27

Contents?: true

Size: 1.05 KB

Versions: 160

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true
# typed: true

module T::Types
  class TypedHash < TypedEnumerable
    def underlying_class
      Hash
    end

    def initialize(keys:, values:)
      @inner_keys = keys
      @inner_values = values
    end

    # Technically we don't need this, but it is a nice api
    def keys
      @keys ||= T::Utils.coerce(@inner_keys)
    end

    # Technically we don't need this, but it is a nice api
    def values
      @values ||= T::Utils.coerce(@inner_values)
    end

    def type
      @type ||= T::Utils.coerce([keys, values])
    end

    # overrides Base
    def name
      "T::Hash[#{keys.name}, #{values.name}]"
    end

    # overrides Base
    def recursively_valid?(obj)
      obj.is_a?(Hash) && super
    end

    # overrides Base
    def valid?(obj)
      obj.is_a?(Hash)
    end

    def new(*args, &blk)
      Hash.new(*T.unsafe(args), &blk)
    end

    class Untyped < TypedHash
      def initialize
        super(keys: T.untyped, values: T.untyped)
      end

      def valid?(obj)
        obj.is_a?(Hash)
      end
    end
  end
end

Version data entries

160 entries across 160 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11668 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11663 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11647 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11645 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11642 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11641 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11637 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11635 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11633 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11631 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11630 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11625 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11620 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11618 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11615 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11611 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11610 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11609 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11608 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11604 lib/types/types/typed_hash.rb