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).



112
113
114
115
116
117
118
# File 'lib/yard_types/types.rb', line 112

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.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/yard_types/types.rb', line 123

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

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

      const
    end
end