Class: YardTypes::TypeConstraint

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

Overview

A TypeConstraint specifies the set of acceptable types which can satisfy the constraint. Parsing any YARD type description will return a TypeConstraint instance.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ TypeConstraint

Returns a new instance of TypeConstraint

Parameters:

  • types (Array<Type>)

    the list of acceptable types


26
27
28
# File 'lib/yard_types/types.rb', line 26

def initialize(types)
  @accepted_types = types
end

Instance Attribute Details

#accepted_typesArray<Type> (readonly)

Returns the list of types that will satisfy this constraint

Returns:

  • (Array<Type>)

    the list of types that will satisfy this constraint


23
24
25
# File 'lib/yard_types/types.rb', line 23

def accepted_types
  @accepted_types
end

Instance Method Details

#[](i) ⇒ Type

TODO:

deprecate this; remnant from original TDD'd API.

Returns the type at index i

Parameters:

  • i (Fixnum)

Returns:

  • (Type)

    the type at index i


33
34
35
# File 'lib/yard_types/types.rb', line 33

def [](i)
  accepted_types[i]
end

#check(obj) ⇒ Type?

Returns the first type which matched obj, or nil if none.

Parameters:

  • obj (Object)

    Any object.

Returns:

  • (Type, nil)

    the first type which matched obj, or nil if none.


46
47
48
# File 'lib/yard_types/types.rb', line 46

def check(obj)
  accepted_types.find { |t| t.check(obj) }
end

#firstType

TODO:

deprecate this; remnant from original TDD'd API.

Returns the first type

Returns:

  • (Type)

    the first type


39
40
41
# File 'lib/yard_types/types.rb', line 39

def first
  self[0]
end

#to_sString

Returns a YARD type string describing this set of types.

Returns:

  • (String)

    a YARD type string describing this set of types.


52
53
54
# File 'lib/yard_types/types.rb', line 52

def to_s
  accepted_types.map(&:to_s).join(', ')
end