Module: Literal::Types
Defined Under Namespace
Classes: AnyType, ArrayType, BooleanType, ClassType, ConstraintType, DeferredType, DescendantType, EnumerableType, FalsyType, FrozenType, HashType, InterfaceType, IntersectionType, JSONDataType, MapType, NeverType, NilableType, NotType, PredicateType, RangeType, SetType, 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 ‘Array` and 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 ‘true` or `false`.
-
#_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 ‘_Constraint` “`ruby _Constraint?(Array, size: 1..3) “`.
-
#_Date ⇒ Object
Matches if the value is a ‘Date` and 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 ‘Enumerable` and all its elements match the given type.
-
#_Enumerable? ⇒ Boolean
Nilable version of ‘_Enumerable`.
-
#_Falsy ⇒ Object
Matches *“falsy”* values (‘nil` and `false`).
-
#_Float ⇒ Object
Matches if the value is a ‘Float` and 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 ‘Hash` and all the keys and values match the given types.
-
#_Hash? ⇒ Boolean
Nilable version of ‘_Hash`.
-
#_Integer ⇒ Object
Matches if the value is an ‘Integer` and matches the given constraint.
-
#_Integer? ⇒ Boolean
Nilable version of ‘_Integer`.
-
#_Interface ⇒ Object
Matches if the value responds to all the given methods.
-
#_Interface? ⇒ Boolean
Nilable version of ‘_Interface`.
-
#_Intersection ⇒ Object
Matches if all given types are matched.
-
#_Intersection? ⇒ Boolean
Nilable version of ‘_Intersection`.
-
#_JSONData ⇒ Object
Ensures the value is valid JSON data (i.e. it came from JSON.parse).
-
#_JSONData? ⇒ Boolean
Nilable version of ‘_JSONData`.
-
#_Lambda ⇒ Object
Matches if the value is a ‘Proc` and `#lambda?` returns truthy.
-
#_Lambda? ⇒ Boolean
Nilable version of ‘_Lambda`.
-
#_Map ⇒ Object
“‘ruby _Map(name: String, age: Integer) “`.
-
#_Map? ⇒ Boolean
Nilable version of ‘_Map` “`ruby _Map?(name: String, age: Integer) “`.
-
#_Never ⇒ Object
Never matches any value.
-
#_Nilable ⇒ Object
Matches if the value is either ‘nil` or the given type.
-
#_Not(type) ⇒ Object
Matches if the given type is not matched.
- #_Predicate(message, &block) ⇒ Object
-
#_Procable ⇒ Object
Matches if the value is a ‘Proc` or responds to `#to_proc`.
-
#_Procable? ⇒ Boolean
Nilable version ofo ‘_Procable`.
-
#_Range ⇒ Object
Matches if the value is a ‘Range` of the given type.
-
#_Range? ⇒ Boolean
Nilable version of ‘_Range`.
-
#_Set ⇒ Object
Matches if the value is a ‘Set` and all the elements match the given type.
-
#_Set? ⇒ Boolean
Nilable version of ‘_Set`.
-
#_String ⇒ Object
Matches if the value is a ‘String` and matches the given constraints.
-
#_String? ⇒ Boolean
Nilable version of ‘_String`.
-
#_Symbol ⇒ Object
Matches if the value is a ‘Symbol` and matches the given constraints.
-
#_Symbol? ⇒ Boolean
Nilable version of ‘_Symbol`.
-
#_Time ⇒ Object
Matches if the value is a ‘Time` and matches the given constraints.
-
#_Time? ⇒ Boolean
Nilable version of ‘_Time`.
-
#_Truthy ⇒ Object
Matches *“truthy”* values (anything except ‘nil` and `false`).
-
#_Tuple ⇒ Object
Matches if the value is an ‘Array` and each element matches the given types in order.
-
#_Tuple? ⇒ Boolean
Nilable version of ‘_Typle` “`ruby _Tuple?(String, Integer, Integer) “`.
-
#_Union ⇒ 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`. “`ruby _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`. “`ruby _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. “`ruby _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`. “`ruby _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)`. “`ruby _Boolean “`
44 45 46 |
# File 'lib/literal/types.rb', line 44 def _Boolean BooleanType::Instance end |
#_Boolean? ⇒ Boolean
Nilable version of ‘_Boolean`. “`ruby _Boolean? “`
52 53 54 |
# File 'lib/literal/types.rb', line 52 def _Boolean? NilableBooleanType end |
#_Callable ⇒ Object
Matches if the value responds to ‘#call`. “`ruby _Callable “`
60 61 62 |
# File 'lib/literal/types.rb', line 60 def _Callable CallableType end |
#_Callable? ⇒ Boolean
Nilable version of ‘_Callable`. “`ruby _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. “‘ruby _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`. “`ruby _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. “`ruby _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` “`ruby _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`. “`ruby _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. “‘ruby _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. “‘ruby _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. “`ruby _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`. “`ruby _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 ⇒ Object
Matches if the value responds to all the given methods.
237 238 239 |
# File 'lib/literal/types.rb', line 237 def _Interface(...) InterfaceType.new(...) 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 ⇒ Object
Matches if all given types are matched.
249 250 251 |
# File 'lib/literal/types.rb', line 249 def _Intersection(...) IntersectionType.new(...) 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 ⇒ Object
Ensures the value is valid JSON data (i.e. it came from JSON.parse).
261 262 263 |
# File 'lib/literal/types.rb', line 261 def _JSONData JSONDataType::Instance end |
#_JSONData? ⇒ Boolean
Nilable version of ‘_JSONData`
266 267 268 |
# File 'lib/literal/types.rb', line 266 def _JSONData? NilableJSONDataType end |
#_Lambda ⇒ Object
Matches if the value is a ‘Proc` and `#lambda?` returns truthy.
271 272 273 |
# File 'lib/literal/types.rb', line 271 def _Lambda LambdaType end |
#_Lambda? ⇒ Boolean
Nilable version of ‘_Lambda`
276 277 278 |
# File 'lib/literal/types.rb', line 276 def _Lambda? NilableLambdaType end |
#_Map ⇒ Object
“‘ruby _Map(name: String, age: Integer) “`
283 284 285 |
# File 'lib/literal/types.rb', line 283 def _Map(...) MapType.new(...) end |
#_Map? ⇒ Boolean
Nilable version of ‘_Map` “`ruby _Map?(name: String, age: Integer) “`
291 292 293 294 295 |
# File 'lib/literal/types.rb', line 291 def _Map?(...) _Nilable( _Map(...) ) end |
#_Never ⇒ Object
Never matches any value.
298 299 300 |
# File 'lib/literal/types.rb', line 298 def _Never NeverType::Instance end |
#_Nilable ⇒ Object
Matches if the value is either ‘nil` or the given type.
303 304 305 |
# File 'lib/literal/types.rb', line 303 def _Nilable(...) NilableType.new(...) end |
#_Not(type) ⇒ Object
Matches if the given type is not matched.
308 309 310 311 312 313 314 315 |
# File 'lib/literal/types.rb', line 308 def _Not(type) case type when NotType type.type else NotType.new(type) end end |
#_Predicate(message, &block) ⇒ Object
317 318 319 |
# File 'lib/literal/types.rb', line 317 def _Predicate(, &block) PredicateType.new(message:, block:) end |
#_Procable ⇒ Object
Matches if the value is a ‘Proc` or responds to `#to_proc`.
322 323 324 |
# File 'lib/literal/types.rb', line 322 def _Procable ProcableType end |
#_Procable? ⇒ Boolean
Nilable version ofo ‘_Procable`
327 328 329 |
# File 'lib/literal/types.rb', line 327 def _Procable? NilableProcableType end |
#_Range ⇒ Object
Matches if the value is a ‘Range` of the given type.
332 333 334 |
# File 'lib/literal/types.rb', line 332 def _Range(...) RangeType.new(...) end |
#_Range? ⇒ Boolean
Nilable version of ‘_Range`
337 338 339 340 341 |
# File 'lib/literal/types.rb', line 337 def _Range?(...) _Nilable( _Range(...) ) end |
#_Set ⇒ Object
Matches if the value is a ‘Set` and all the elements match the given type.
344 345 346 |
# File 'lib/literal/types.rb', line 344 def _Set(...) SetType.new(...) end |
#_Set? ⇒ Boolean
Nilable version of ‘_Set`
349 350 351 352 353 |
# File 'lib/literal/types.rb', line 349 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`.
357 358 359 |
# File 'lib/literal/types.rb', line 357 def _String(...) _Constraint(String, ...) end |
#_String? ⇒ Boolean
Nilable version of ‘_String`
362 363 364 365 366 |
# File 'lib/literal/types.rb', line 362 def _String?(...) _Nilable( _String(...) ) end |
#_Symbol ⇒ Object
Matches if the value is a ‘Symbol` and matches the given constraints.
369 370 371 |
# File 'lib/literal/types.rb', line 369 def _Symbol(...) _Constraint(Symbol, ...) end |
#_Symbol? ⇒ Boolean
Nilable version of ‘_Symbol`
374 375 376 377 378 |
# File 'lib/literal/types.rb', line 374 def _Symbol?(...) _Nilable( _Symbol(...) ) 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`.
382 383 384 |
# File 'lib/literal/types.rb', line 382 def _Time(...) _Constraint(Time, ...) end |
#_Time? ⇒ Boolean
Nilable version of ‘_Time`
387 388 389 390 391 |
# File 'lib/literal/types.rb', line 387 def _Time?(...) _Nilable( _Time(...) ) end |
#_Truthy ⇒ Object
Matches *“truthy”* values (anything except ‘nil` and `false`).
394 395 396 |
# File 'lib/literal/types.rb', line 394 def _Truthy TruthyType::Instance end |
#_Tuple ⇒ Object
Matches if the value is an ‘Array` and each element matches the given types in order. “`ruby _Tuple(String, Integer, Integer) “`
402 403 404 |
# File 'lib/literal/types.rb', line 402 def _Tuple(...) TupleType.new(...) end |
#_Tuple? ⇒ Boolean
Nilable version of ‘_Typle` “`ruby _Tuple?(String, Integer, Integer) “`
410 411 412 413 414 |
# File 'lib/literal/types.rb', line 410 def _Tuple?(...) _Nilable( _Tuple(...) ) end |
#_Union ⇒ Object
Matches if any given type is matched.
417 418 419 |
# File 'lib/literal/types.rb', line 417 def _Union(...) UnionType.new(...) end |
#_Union? ⇒ Boolean
Nilable version of ‘_Union`
422 423 424 425 426 |
# File 'lib/literal/types.rb', line 422 def _Union?(...) _Nilable( _Union(...) ) end |
#_Void ⇒ Object
428 429 430 |
# File 'lib/literal/types.rb', line 428 def _Void VoidType::Instance end |