Class: YardTypes::HashType

Inherits:
Type
  • Object
show all
Defined in:
lib/yard_types/types.rb

Overview

TODO:

Enforce kind, eg HashWithIndifferentAccess{#to_sym => Array}, in case you really care that it’s indifferent. Maybe?

A HashType is specified with the syntax {KeyType => ValueType}, and indicates that all keys in the hash must be of type KeyType, and all values must be of type ValueType.

An alternate syntax for HashType is also available as Hash<A, B>, but its usage is not recommended; it is less capable than the {A => B} syntax, as some inner type constraints can not be parsed reliably.

A HashType actually only requires that the object respond to both keys and values; it should be capable of type checking any object which conforms to that interface.

Instance Attribute Summary collapse

Attributes inherited from Type

#name

Instance Method Summary collapse

Methods inherited from Type

for

Constructor Details

#initialize(name, key_types, value_types) ⇒ HashType

Returns a new instance of HashType.

Parameters:

  • name (String)

    the kind of the expected object; currently unused.

  • key_types (Array<Type>)

    the set of acceptable types for keys

  • value_types (Array<Type>)

    the set of acceptable types for values



265
266
267
268
269
# File 'lib/yard_types/types.rb', line 265

def initialize(name, key_types, value_types)
  @name = name
  @key_types = key_types
  @value_types = value_types
end

Instance Attribute Details

#key_typesArray<Type> (readonly)

Returns the set of acceptable types for keys.

Returns:

  • (Array<Type>)

    the set of acceptable types for keys



257
258
259
# File 'lib/yard_types/types.rb', line 257

def key_types
  @key_types
end

#value_typesArray<Type> (readonly)

Returns the set of acceptable types for values.

Returns:

  • (Array<Type>)

    the set of acceptable types for values



260
261
262
# File 'lib/yard_types/types.rb', line 260

def value_types
  @value_types
end

Instance Method Details

#check(obj) ⇒ Boolean

Returns true if the object responds to both keys and values, and every key type checks against a type in key_types, and every value type checks against a type in value_types.

Parameters:

  • obj (Object)

    Any object.

Returns:

  • (Boolean)

    true if the object responds to both keys and values, and every key type checks against a type in key_types, and every value type checks against a type in value_types.



286
287
288
289
290
# File 'lib/yard_types/types.rb', line 286

def check(obj)
  return false unless obj.respond_to?(:keys) && obj.respond_to?(:values)
  obj.keys.all? { |key| key_types.any? { |t| t.check(key) } } &&
    obj.values.all? { |value| value_types.any? { |t| t.check(value) } }
end

#to_sString

Unlike the other types, YardTypes::HashType can result from two alternate syntaxes; however, this method will only return the {A => B} syntax.

Returns:

  • (String)

    a YARD type string describing this type.



275
276
277
278
279
280
# File 'lib/yard_types/types.rb', line 275

def to_s
  "{%s => %s}" % [
    key_types.map(&:to_s).join(', '),
    value_types.map(&:to_s).join(', ')
  ]
end