Class: YardTypes::CollectionType

Inherits:
Type
  • Object
show all
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 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.



183
184
185
186
# File 'lib/yard_types/types.rb', line 183

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.



179
180
181
# File 'lib/yard_types/types.rb', line 179

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.



197
198
199
200
201
202
203
204
# File 'lib/yard_types/types.rb', line 197

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

#to_sString

Returns a YARD type string describing this type.

Returns:

  • (String)

    a YARD type string describing this type.



189
190
191
# File 'lib/yard_types/types.rb', line 189

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