Class: YardTypes::HashType

Inherits:
Type
  • Object
show all
Includes:
OrList
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 included from OrList

#or_list

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


317
318
319
320
321
# File 'lib/yard_types/types.rb', line 317

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


309
310
311
# File 'lib/yard_types/types.rb', line 309

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


312
313
314
# File 'lib/yard_types/types.rb', line 312

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.


346
347
348
349
350
# File 'lib/yard_types/types.rb', line 346

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

#descriptionString

Returns an English phrase describing this type.

Returns:

  • (String)

    an English phrase describing this type.


335
336
337
338
339
340
# File 'lib/yard_types/types.rb', line 335

def description
  article = name[0] =~ /[aeiou]/i ? 'an' : 'a'
  key_descriptions = or_list(key_types.map(&:description))
  value_descriptions = or_list(value_types.map(&:description))
  "#{article} #{name} with keys of (#{key_descriptions}) and values of (#{value_descriptions})"
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.


327
328
329
330
331
332
# File 'lib/yard_types/types.rb', line 327

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