Sha256: 6b7200bd93b78c3305869ef28f7f9edde712afde2862a2b5364f8e2ac12e7e27

Contents?: true

Size: 1.05 KB

Versions: 161

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

161 entries across 161 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11604 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11602 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11600 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11597 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11595 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11592 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11589 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11585 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11582 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11581 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11577 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11572 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11566 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11558 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11557 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11554 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11553 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11551 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11549 lib/types/types/typed_hash.rb
sorbet-runtime-0.5.11547 lib/types/types/typed_hash.rb