Class: T::Types::ClassOf
Overview
Validates that an object belongs to the specified class.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#[](*types) ⇒ Object
So that ‘T.class_of(…)` syntax is valid.
- #build_type ⇒ Object
-
#describe_obj(obj) ⇒ Object
overrides Base.
-
#initialize(type) ⇒ ClassOf
constructor
A new instance of ClassOf.
-
#name ⇒ Object
overrides Base.
-
#subtype_of_single?(other) ⇒ Boolean
overrides Base.
-
#valid?(obj) ⇒ Boolean
overrides Base.
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
#type ⇒ Object (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_type ⇒ Object
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 |
#name ⇒ Object
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
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 @type.is_a?(other.) else false end end |
#valid?(obj) ⇒ Boolean
overrides Base
23 24 25 |
# File 'lib/types/types/class_of.rb', line 23 def valid?(obj) obj.is_a?(Module) && (obj <= @type || false) end |