Class: YardTypes::CollectionType

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

Overview

TODO:

The current implementation of type checking here requires that the collection respond to all?; this may not be ideal.

A CollectionType is specified with the syntax Kind<Some, #thing>, and indicates that the object is a kind of Kind, containing only objects which type check against Some or #thing.

Direct Known Subclasses

TupleType

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, types) ⇒ CollectionType

Returns a new instance of CollectionType

Parameters:

  • name (String)

    the name of the module the collection must be a kind of.

  • types (Array<Type>)

    the acceptable types for the collection's contents.


218
219
220
221
# File 'lib/yard_types/types.rb', line 218

def initialize(name, types)
  @name = name
  @types = types
end

Instance Attribute Details

#typesArray<Type>

Returns the acceptable types for this collection's contents.

Returns:

  • (Array<Type>)

    the acceptable types for this collection's contents.


214
215
216
# File 'lib/yard_types/types.rb', line 214

def types
  @types
end

Instance Method Details

#check(obj) ⇒ Boolean

Returns true if the object is both a kind of name, and all of its contents (if any) are of the types in types. Any combination, order, and count of content types is acceptable.

Parameters:

  • obj (Object)

    Any object.

Returns:

  • (Boolean)

    true if the object is both a kind of name, and all of its contents (if any) are of the types in types. Any combination, order, and count of content types is acceptable.


239
240
241
242
243
244
245
246
# File 'lib/yard_types/types.rb', line 239

def check(obj)
  return false unless KindType.new(name).check(obj)

  obj.all? do |el|
    # TODO -- could probably just use another TypeConstraint here
    types.any? { |type| type.check(el) }
  end
end

#descriptionString

Returns an English phrase describing this type.

Returns:

  • (String)

    an English phrase describing this type.


229
230
231
232
233
# File 'lib/yard_types/types.rb', line 229

def description
  article = name[0] =~ /[aeiou]/i ? 'an' : 'a'
  type_descriptions = types.map(&:description)
  "#{article} #{name} of (#{or_list(type_descriptions)})"
end

#to_sString

Returns a YARD type string describing this type.

Returns:

  • (String)

    a YARD type string describing this type.


224
225
226
# File 'lib/yard_types/types.rb', line 224

def to_s
  "%s<%s>" % [name, types.map(&:to_s).join(', ')]
end