Class: T::Types::ClassOf

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

Overview

Validates that an object belongs to the specified class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #error_message_for_obj, #hash, method_added, #subtype_of?, #to_s, #validate!

Constructor Details

#initialize(type) ⇒ ClassOf

Returns a new instance of ClassOf.



9
10
11
# File 'lib/types/types/class_of.rb', line 9

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/types/types/class_of.rb', line 7

def type
  @type
end

Instance Method Details

#describe_obj(obj) ⇒ Object



34
35
36
# File 'lib/types/types/class_of.rb', line 34

def describe_obj(obj)
  obj.inspect
end

#nameObject



14
15
16
# File 'lib/types/types/class_of.rb', line 14

def name
  "T.class_of(#{@type})"
end

#subtype_of_single?(other) ⇒ Boolean

Returns:



24
25
26
27
28
29
30
31
# File 'lib/types/types/class_of.rb', line 24

def subtype_of_single?(other)
  case other
  when ClassOf
    @type <= other.type
  else
    false
  end
end

#valid?(obj) ⇒ Boolean

Returns:



19
20
21
# File 'lib/types/types/class_of.rb', line 19

def valid?(obj)
  obj.is_a?(Module) && obj <= @type
end