Module: Literal::Types
Defined Under Namespace
Classes: AnyType, ArrayType, BooleanType, ClassType, ConstraintType, DeferredType, DescendantType, EnumerableType, FalsyType, FrozenType, HashType, InterfaceType, IntersectionType, JSONDataType, KindType, MapType, NeverType, NilableType, NotType, PredicateType, RangeType, SameObjectType, SetType, TaggedUnionType, TruthyType, TupleType, UnionType, VoidType
Constant Summary collapse
- ProcableType =
_Interface(:to_proc)
- CallableType =
_Interface(:call)
- LambdaType =
_Constraint(Proc, lambda?: true)
- NilableBooleanType =
_Nilable(BooleanType::Instance)
- NilableCallableType =
_Nilable(CallableType)
- NilableJSONDataType =
_Nilable(JSONDataType)
- NilableLambdaType =
_Nilable(LambdaType)
- NilableProcableType =
_Nilable(ProcableType)
Instance Method Summary collapse
-
#_Any ⇒ Object
Matches any value except
nil. -
#_Any? ⇒ Boolean
Matches any value including
nil. -
#_Array(type) ⇒ Object
Matches if the value is an
Arrayand all the elements of the array match the given type. -
#_Array?(type) ⇒ Boolean
Nilable version of
_Array. -
#_Boolean ⇒ Object
Matches if the value is either
trueorfalse. -
#_Boolean? ⇒ Boolean
Nilable version of
_Boolean. -
#_Callable ⇒ Object
Matches if the value responds to
#call. -
#_Callable? ⇒ Boolean
Nilable version of
_Callable. -
#_Class(expected_class) ⇒ Object
Matches if the value either the given class or a subclass of it.
-
#_Class? ⇒ Boolean
Nilable version of
_Class. -
#_Constraint(*a, **k) ⇒ Object
Similar to
_Intersection, but allows you to specify attribute constraints as keyword arguments. -
#_Constraint? ⇒ Boolean
Nilable version of
_Constraintruby _Constraint?(Array, size: 1..3). -
#_Date ⇒ Object
Matches if the value is a
Dateand matches the given constraints. -
#_Date? ⇒ Boolean
Nilable version of
_Date. -
#_Deferred(&type) ⇒ Object
Takes a type as a block so it can be resolved when needed.
-
#_Deferred?(&type) ⇒ Boolean
Nilable version of
_Deferred. -
#_Descendant ⇒ Object
Matches if the value is a descendant of the given class.
-
#_Descendant? ⇒ Boolean
Nilable version of
_Descendant. -
#_Enumerable(type) ⇒ Object
Matches if the value is an
Enumerableand all its elements match the given type. -
#_Enumerable? ⇒ Boolean
Nilable version of
_Enumerable. -
#_Falsy ⇒ Object
Matches "falsy" values (
nilandfalse). -
#_Float ⇒ Object
Matches if the value is a
Floatand matches the given constraints. -
#_Float? ⇒ Boolean
Nilable version of
_Float. -
#_Frozen ⇒ Object
Matches if the value is frozen.
-
#_Frozen? ⇒ Boolean
Nilable version of
_Frozen. -
#_Hash ⇒ Object
Matches if the value is a
Hashand all the keys and values match the given types. -
#_Hash? ⇒ Boolean
Nilable version of
_Hash. -
#_Integer ⇒ Object
Matches if the value is an
Integerand matches the given constraint. -
#_Integer? ⇒ Boolean
Nilable version of
_Integer. -
#_Interface(*methods) ⇒ Object
Matches if the value responds to all the given methods.
-
#_Interface? ⇒ Boolean
Nilable version of
_Interface. -
#_Intersection(*types) ⇒ Object
Matches if all given types are matched.
-
#_Intersection? ⇒ Boolean
Nilable version of
_Intersection. -
#_JSONData(*a, **k) ⇒ Object
Ensures the value is valid JSON data (i.e. it came from JSON.parse).
-
#_JSONData? ⇒ Boolean
Nilable version of
_JSONData. - #_Kind(type) ⇒ Object
-
#_Lambda ⇒ Object
Matches if the value is a
Procand#lambda?returns truthy. -
#_Lambda? ⇒ Boolean
Nilable version of
_Lambda. -
#_Map(**shape) ⇒ Object
ruby _Map(name: String, age: Integer). -
#_Map? ⇒ Boolean
Nilable version of
_Mapruby _Map?(name: String, age: Integer). -
#_Never ⇒ Object
Never matches any value.
-
#_Nilable ⇒ Object
Matches if the value is either
nilor the given type. -
#_Not(*types) ⇒ Object
Matches if the given type is not matched.
- #_Pattern(regex, &block) ⇒ Object
- #_Predicate(message, &block) ⇒ Object
-
#_Procable ⇒ Object
Matches if the value is a
Procor responds to#to_proc. -
#_Procable? ⇒ Boolean
Nilable version ofo
_Procable. -
#_Range ⇒ Object
Matches if the value is a
Rangeof the given type. -
#_Range? ⇒ Boolean
Nilable version of
_Range. -
#_SameObject(object) ⇒ Object
Matches only the same object identity.
-
#_SameObject? ⇒ Boolean
Nilable version of
_SameObject. -
#_Set ⇒ Object
Matches if the value is a
Setand all the elements match the given type. -
#_Set? ⇒ Boolean
Nilable version of
_Set. -
#_String ⇒ Object
Matches if the value is a
Stringand matches the given constraints. -
#_String? ⇒ Boolean
Nilable version of
_String. -
#_Symbol ⇒ Object
Matches if the value is a
Symboland matches the given constraints. -
#_Symbol? ⇒ Boolean
Nilable version of
_Symbol. -
#_TaggedUnion(**members) ⇒ Object
Matches if the value matches any of the given tagged types.
-
#_TaggedUnion? ⇒ Boolean
Nilable version of
_TaggedUnion. -
#_Time ⇒ Object
Matches if the value is a
Timeand matches the given constraints. -
#_Time? ⇒ Boolean
Nilable version of
_Time. -
#_Truthy ⇒ Object
Matches "truthy" values (anything except
nilandfalse). -
#_Tuple(*types) ⇒ Object
Matches if the value is an
Arrayand each element matches the given types in order. -
#_Tuple? ⇒ Boolean
Nilable version of
_Typleruby _Tuple?(String, Integer, Integer). -
#_Union(*types) ⇒ Object
Matches if any given type is matched.
-
#_Union? ⇒ Boolean
Nilable version of
_Union. - #_Void ⇒ Object
Instance Method Details
#_Any ⇒ Object
Matches any value except nil. Use _Any? or _Void to match any value including nil.
_Any
10 11 12 |
# File 'lib/literal/types.rb', line 10 def _Any AnyType::Instance end |
#_Any? ⇒ Boolean
Matches any value including nil. This is the same as _Void and the opposite of _Never.
_Any?
18 19 20 |
# File 'lib/literal/types.rb', line 18 def _Any? VoidType::Instance end |
#_Array(type) ⇒ Object
Matches if the value is an Array and all the elements of the array match the given type.
_Array(String)
26 27 28 |
# File 'lib/literal/types.rb', line 26 def _Array(type) ArrayType.new(type) end |
#_Array?(type) ⇒ Boolean
Nilable version of _Array.
_Array?(String)
34 35 36 37 38 |
# File 'lib/literal/types.rb', line 34 def _Array?(type) _Nilable( _Array(type) ) end |
#_Boolean ⇒ Object
Matches if the value is either true or false. This is equivalent to _Union(true, false).
_Boolean
44 45 46 |
# File 'lib/literal/types.rb', line 44 def _Boolean BooleanType::Instance end |
#_Boolean? ⇒ Boolean
Nilable version of _Boolean.
_Boolean?
52 53 54 |
# File 'lib/literal/types.rb', line 52 def _Boolean? NilableBooleanType end |
#_Callable ⇒ Object
Matches if the value responds to #call.
_Callable
60 61 62 |
# File 'lib/literal/types.rb', line 60 def _Callable CallableType end |
#_Callable? ⇒ Boolean
Nilable version of _Callable.
_Callable?
68 69 70 |
# File 'lib/literal/types.rb', line 68 def _Callable? NilableCallableType end |
#_Class(expected_class) ⇒ Object
Matches if the value either the given class or a subclass of it.
_Class(ActiveRecord::Base)
76 77 78 |
# File 'lib/literal/types.rb', line 76 def _Class(expected_class) ClassType.new(expected_class) end |
#_Class? ⇒ Boolean
Nilable version of _Class.
_Class?(ActiveRecord::Base)
84 85 86 87 88 |
# File 'lib/literal/types.rb', line 84 def _Class?(...) _Nilable( _Class(...) ) end |
#_Constraint(*a, **k) ⇒ Object
Similar to _Intersection, but allows you to specify attribute constraints as keyword arguments.
_Constraint(Array, size: 1..3)
94 95 96 97 98 99 100 |
# File 'lib/literal/types.rb', line 94 def _Constraint(*a, **k) if a.length == 1 && k.length == 0 a[0] else ConstraintType.new(a, k) end end |
#_Constraint? ⇒ Boolean
Nilable version of _Constraint
_Constraint?(Array, size: 1..3)
106 107 108 109 110 |
# File 'lib/literal/types.rb', line 106 def _Constraint?(...) _Nilable( _Constraint(...) ) end |
#_Date ⇒ Object
Matches if the value is a Date and matches the given constraints.
If you don't need any constraints, use Date instead of _Date. See also _Constraint.
_Date((Date.today)..)
_Date(year: 2025)
118 119 120 |
# File 'lib/literal/types.rb', line 118 def _Date(...) _Constraint(Date, ...) end |
#_Date? ⇒ Boolean
Nilable version of _Date.
123 124 125 126 127 |
# File 'lib/literal/types.rb', line 123 def _Date?(...) _Nilable( _Date(...) ) end |
#_Deferred(&type) ⇒ Object
Takes a type as a block so it can be resolved when needed. This is useful if declaring your type now would cause an error because constants haven’t been defined yet.
_Deferred { _Class(SomeFutureConstant) }
133 134 135 |
# File 'lib/literal/types.rb', line 133 def _Deferred(&type) DeferredType.new(&type) end |
#_Deferred?(&type) ⇒ Boolean
Nilable version of _Deferred.
138 139 140 141 142 |
# File 'lib/literal/types.rb', line 138 def _Deferred?(&type) _Nilable( _Deferred(&type) ) end |
#_Descendant ⇒ Object
Matches if the value is a descendant of the given class.
_Descendant(ActiveRecord::Base)
148 149 150 |
# File 'lib/literal/types.rb', line 148 def _Descendant(...) DescendantType.new(...) end |
#_Descendant? ⇒ Boolean
Nilable version of _Descendant.
153 154 155 156 157 |
# File 'lib/literal/types.rb', line 153 def _Descendant?(...) _Nilable( _Descendant(...) ) end |
#_Enumerable(type) ⇒ Object
Matches if the value is an Enumerable and all its elements match the given type.
_Enumerable(String)
163 164 165 |
# File 'lib/literal/types.rb', line 163 def _Enumerable(type) EnumerableType.new(type) end |
#_Enumerable? ⇒ Boolean
Nilable version of _Enumerable.
168 169 170 171 172 |
# File 'lib/literal/types.rb', line 168 def _Enumerable?(...) _Nilable( _Enumerable(...) ) end |
#_Falsy ⇒ Object
Matches "falsy" values (nil and false). This is equivalent to _Nilable(false) or _Union(nil, false).
175 176 177 |
# File 'lib/literal/types.rb', line 175 def _Falsy FalsyType::Instance end |
#_Float ⇒ Object
Matches if the value is a Float and matches the given constraints.
You could use a Range, for example, as a constraint.
If you don't need a constraint, use Float instead of _Float.
_Float(5..10)
185 186 187 |
# File 'lib/literal/types.rb', line 185 def _Float(...) _Constraint(Float, ...) end |
#_Float? ⇒ Boolean
Nilable version of _Float.
190 191 192 193 194 |
# File 'lib/literal/types.rb', line 190 def _Float?(...) _Nilable( _Float(...) ) end |
#_Frozen ⇒ Object
Matches if the value is frozen.
197 198 199 |
# File 'lib/literal/types.rb', line 197 def _Frozen(...) FrozenType.new(...) end |
#_Frozen? ⇒ Boolean
Nilable version of _Frozen
202 203 204 205 206 |
# File 'lib/literal/types.rb', line 202 def _Frozen?(...) _Nilable( _Frozen(...) ) end |
#_Hash ⇒ Object
Matches if the value is a Hash and all the keys and values match the given types.
209 210 211 |
# File 'lib/literal/types.rb', line 209 def _Hash(...) HashType.new(...) end |
#_Hash? ⇒ Boolean
Nilable version of _Hash
214 215 216 217 218 |
# File 'lib/literal/types.rb', line 214 def _Hash?(...) _Nilable( _Hash(...) ) end |
#_Integer ⇒ Object
Matches if the value is an Integer and matches the given constraint.
You could use a Range, for example, as a constraint.
If you don't need a constraint, use Integer instead of _Integer.
225 226 227 |
# File 'lib/literal/types.rb', line 225 def _Integer(...) _Constraint(Integer, ...) end |
#_Integer? ⇒ Boolean
Nilable version of _Integer
230 231 232 233 234 |
# File 'lib/literal/types.rb', line 230 def _Integer?(...) _Nilable( _Integer(...) ) end |
#_Interface(*methods) ⇒ Object
Matches if the value responds to all the given methods.
237 238 239 |
# File 'lib/literal/types.rb', line 237 def _Interface(*methods) InterfaceType.new(methods) end |
#_Interface? ⇒ Boolean
Nilable version of _Interface
242 243 244 245 246 |
# File 'lib/literal/types.rb', line 242 def _Interface?(...) _Nilable( _Interface(...) ) end |
#_Intersection(*types) ⇒ Object
Matches if all given types are matched.
249 250 251 |
# File 'lib/literal/types.rb', line 249 def _Intersection(*types) IntersectionType.new(types) end |
#_Intersection? ⇒ Boolean
Nilable version of _Intersection
254 255 256 257 258 |
# File 'lib/literal/types.rb', line 254 def _Intersection?(...) _Nilable( _Intersection(...) ) end |
#_JSONData(*a, **k) ⇒ Object
Ensures the value is valid JSON data (i.e. it came from JSON.parse).
261 262 263 264 265 266 267 |
# File 'lib/literal/types.rb', line 261 def _JSONData(*a, **k) if a.length > 0 || k.length > 0 _Constraint(JSONDataType::Instance, *a, **k) else JSONDataType::Instance end end |
#_JSONData? ⇒ Boolean
Nilable version of _JSONData
270 271 272 273 274 |
# File 'lib/literal/types.rb', line 270 def _JSONData?(...) _Nilable( _JSONData(...) ) end |
#_Kind(type) ⇒ Object
276 277 278 |
# File 'lib/literal/types.rb', line 276 def _Kind(type) KindType.new(type) end |
#_Lambda ⇒ Object
Matches if the value is a Proc and #lambda? returns truthy.
281 282 283 |
# File 'lib/literal/types.rb', line 281 def _Lambda LambdaType end |
#_Lambda? ⇒ Boolean
Nilable version of _Lambda
286 287 288 |
# File 'lib/literal/types.rb', line 286 def _Lambda? NilableLambdaType end |
#_Map(**shape) ⇒ Object
_Map(name: String, age: Integer)
293 294 295 |
# File 'lib/literal/types.rb', line 293 def _Map(**shape) MapType.new(shape) end |
#_Map? ⇒ Boolean
Nilable version of _Map
_Map?(name: String, age: Integer)
301 302 303 304 305 |
# File 'lib/literal/types.rb', line 301 def _Map?(...) _Nilable( _Map(...) ) end |
#_Never ⇒ Object
Never matches any value.
308 309 310 |
# File 'lib/literal/types.rb', line 308 def _Never NeverType::Instance end |
#_Nilable ⇒ Object
Matches if the value is either nil or the given type.
313 314 315 |
# File 'lib/literal/types.rb', line 313 def _Nilable(...) NilableType.new(...) end |
#_Not(*types) ⇒ Object
Matches if the given type is not matched.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/literal/types.rb', line 318 def _Not(*types) if types.length > 1 NotType.new( _Union(*types) ) else type = types[0] case type when NotType type.type else NotType.new(type) end end end |
#_Pattern(regex, &block) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/literal/types.rb', line 335 def _Pattern(regex, &block) raise ArgumentError.new("Block required for Pattern") unless block -> (value) { if (data = regex.match(value)) !!block.call(*data.captures, **data.named_captures&.transform_keys(&:to_sym)) else false end } end |
#_Predicate(message, &block) ⇒ Object
347 348 349 |
# File 'lib/literal/types.rb', line 347 def _Predicate(, &block) PredicateType.new(message:, block:) end |
#_Procable ⇒ Object
Matches if the value is a Proc or responds to #to_proc.
352 353 354 |
# File 'lib/literal/types.rb', line 352 def _Procable ProcableType end |
#_Procable? ⇒ Boolean
Nilable version ofo _Procable
357 358 359 |
# File 'lib/literal/types.rb', line 357 def _Procable? NilableProcableType end |
#_Range ⇒ Object
Matches if the value is a Range of the given type.
362 363 364 |
# File 'lib/literal/types.rb', line 362 def _Range(...) RangeType.new(...) end |
#_Range? ⇒ Boolean
Nilable version of _Range
367 368 369 370 371 |
# File 'lib/literal/types.rb', line 367 def _Range?(...) _Nilable( _Range(...) ) end |
#_SameObject(object) ⇒ Object
Matches only the same object identity.
471 472 473 |
# File 'lib/literal/types.rb', line 471 def _SameObject(object) SameObjectType.new(object) end |
#_SameObject? ⇒ Boolean
Nilable version of _SameObject
476 477 478 479 480 |
# File 'lib/literal/types.rb', line 476 def _SameObject?(...) _Nilable( _SameObject(...) ) end |
#_Set ⇒ Object
Matches if the value is a Set and all the elements match the given type.
374 375 376 |
# File 'lib/literal/types.rb', line 374 def _Set(...) SetType.new(...) end |
#_Set? ⇒ Boolean
Nilable version of _Set
379 380 381 382 383 |
# File 'lib/literal/types.rb', line 379 def _Set?(...) _Nilable( _Set(...) ) end |
#_String ⇒ Object
Matches if the value is a String and matches the given constraints.
If you don't need any constraints, use String instead of _String.
387 388 389 |
# File 'lib/literal/types.rb', line 387 def _String(...) _Constraint(String, ...) end |
#_String? ⇒ Boolean
Nilable version of _String
392 393 394 395 396 |
# File 'lib/literal/types.rb', line 392 def _String?(...) _Nilable( _String(...) ) end |
#_Symbol ⇒ Object
Matches if the value is a Symbol and matches the given constraints.
399 400 401 |
# File 'lib/literal/types.rb', line 399 def _Symbol(...) _Constraint(Symbol, ...) end |
#_Symbol? ⇒ Boolean
Nilable version of _Symbol
404 405 406 407 408 |
# File 'lib/literal/types.rb', line 404 def _Symbol?(...) _Nilable( _Symbol(...) ) end |
#_TaggedUnion(**members) ⇒ Object
Matches if the value matches any of the given tagged types. Tags are used to identify which type was matched.
447 448 449 |
# File 'lib/literal/types.rb', line 447 def _TaggedUnion(**members) TaggedUnionType.new(**members) end |
#_TaggedUnion? ⇒ Boolean
Nilable version of _TaggedUnion
452 453 454 455 456 |
# File 'lib/literal/types.rb', line 452 def _TaggedUnion?(...) _Nilable( _TaggedUnion(...) ) end |
#_Time ⇒ Object
Matches if the value is a Time and matches the given constraints.
If you don't need any constraints, use Time instead of _Time.
412 413 414 |
# File 'lib/literal/types.rb', line 412 def _Time(...) _Constraint(Time, ...) end |
#_Time? ⇒ Boolean
Nilable version of _Time
417 418 419 420 421 |
# File 'lib/literal/types.rb', line 417 def _Time?(...) _Nilable( _Time(...) ) end |
#_Truthy ⇒ Object
Matches "truthy" values (anything except nil and false).
424 425 426 |
# File 'lib/literal/types.rb', line 424 def _Truthy TruthyType::Instance end |
#_Tuple(*types) ⇒ Object
Matches if the value is an Array and each element matches the given types in order.
_Tuple(String, Integer, Integer)
432 433 434 |
# File 'lib/literal/types.rb', line 432 def _Tuple(*types) TupleType.new(types) end |
#_Tuple? ⇒ Boolean
Nilable version of _Typle
_Tuple?(String, Integer, Integer)
440 441 442 443 444 |
# File 'lib/literal/types.rb', line 440 def _Tuple?(...) _Nilable( _Tuple(...) ) end |
#_Union(*types) ⇒ Object
Matches if any given type is matched.
459 460 461 |
# File 'lib/literal/types.rb', line 459 def _Union(*types) UnionType.new(types) end |
#_Union? ⇒ Boolean
Nilable version of _Union
464 465 466 467 468 |
# File 'lib/literal/types.rb', line 464 def _Union?(...) _Nilable( _Union(...) ) end |