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, #error_message_for_obj_recursive, #hash, method_added, #recursively_valid?, #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

#[](*types) ⇒ Object

So that ‘T.class_of(…)` syntax is valid. Mirrors the definition of T::Generic#[] (generics are erased).

We avoid simply writing ‘include T::Generic` because we don’t want any of the other methods to appear (‘T.class_of(A).type_member` doesn’t make sense)



51
52
53
# File 'lib/types/types/class_of.rb', line 51

def [](*types)
  self
end

#build_typeObject



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

def build_type
  nil
end

#describe_obj(obj) ⇒ Object

overrides Base



42
43
44
# File 'lib/types/types/class_of.rb', line 42

def describe_obj(obj)
  obj.inspect
end

#nameObject

overrides Base



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

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

#subtype_of_single?(other) ⇒ Boolean

overrides Base

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/types/types/class_of.rb', line 28

def subtype_of_single?(other)
  case other
  when ClassOf
    @type <= other.type
  when Simple
    @type.is_a?(other.raw_type)
  when TypedClass
    true
  else
    false
  end
end

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



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

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