Class: YardTypes::KindType

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

Overview

A KindType constraint is specified as SomeModule or SomeClass, and indicates that the object must be a kind of that module.

Instance Attribute Summary

Attributes inherited from Type

#name

Instance Method Summary collapse

Methods inherited from Type

for, #initialize, #to_s

Constructor Details

This class inherits a constructor from YardTypes::Type

Instance Method Details

#check(obj) ⇒ Boolean

Type checks a given object. Special consideration is given to the pseudo-class Boolean, which does not actually exist in Ruby, but is commonly used to mean TrueClass, FalseClass.

Parameters:

  • obj (Object)

    Any object.

Returns:

  • (Boolean)

    true if obj.kind_of?(constant).


135
136
137
138
139
140
141
# File 'lib/yard_types/types.rb', line 135

def check(obj)
  if name == 'Boolean'
    obj == true || obj == false
  else
    obj.kind_of? constant
  end
end

#constantModule

Returns the constant specified by name.

Returns:

  • (Module)

    the constant specified by name.

Raises:

  • (TypeError)

    if the constant is neither a module nor a class

  • (NameError)

    if the specified constant could not be loaded.


151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/yard_types/types.rb', line 151

def constant
  @constant ||=
    begin
      const = name.split('::').reduce(Object) { |namespace, inner_const|
        namespace.const_get(inner_const)
      }

      unless const.kind_of?(Module)
        raise TypeError, "class or module required; #{name} is a #{const.class}"
      end

      const
    end
end

#descriptionString

Returns an English phrase describing this type.

Returns:

  • (String)

    an English phrase describing this type.


144
145
146
# File 'lib/yard_types/types.rb', line 144

def description
  name
end