Module: Literal::Types

Extended by:
Types
Included in:
Array, Properties, Types
Defined in:
lib/literal/types.rb

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, UnitType, 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

Instance Method Details

#_AnyObject

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? “`

Returns:

  • (Boolean)


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) “`

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/literal/types.rb', line 34

def _Array?(type)
  _Nilable(
    _Array(type)
  )
end

#_BooleanObject

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? “`

Returns:

  • (Boolean)


52
53
54
# File 'lib/literal/types.rb', line 52

def _Boolean?
  NilableBooleanType
end

#_CallableObject

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? “`

Returns:

  • (Boolean)


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) “`

Returns:

  • (Boolean)


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) “`

Returns:

  • (Boolean)


106
107
108
109
110
# File 'lib/literal/types.rb', line 106

def _Constraint?(...)
  _Nilable(
    _Constraint(...)
  )
end

#_DateObject

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`.

Returns:

  • (Boolean)


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`.

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/literal/types.rb', line 138

def _Deferred?(&type)
  _Nilable(
    _Deferred(&type)
  )
end

#_DescendantObject

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`.

Returns:

  • (Boolean)


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`.

Returns:

  • (Boolean)


168
169
170
171
172
# File 'lib/literal/types.rb', line 168

def _Enumerable?(...)
  _Nilable(
    _Enumerable(...)
  )
end

#_FalsyObject

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

#_FloatObject

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`.

Returns:

  • (Boolean)


190
191
192
193
194
# File 'lib/literal/types.rb', line 190

def _Float?(...)
  _Nilable(
    _Float(...)
  )
end

#_FrozenObject

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`

Returns:

  • (Boolean)


202
203
204
205
206
# File 'lib/literal/types.rb', line 202

def _Frozen?(...)
  _Nilable(
    _Frozen(...)
  )
end

#_HashObject

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`

Returns:

  • (Boolean)


214
215
216
217
218
# File 'lib/literal/types.rb', line 214

def _Hash?(...)
  _Nilable(
    _Hash(...)
  )
end

#_IntegerObject

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`.

Examples:

attribute :age, _Integer(18..127)


225
226
227
# File 'lib/literal/types.rb', line 225

def _Integer(...)
  _Constraint(Integer, ...)
end

#_Integer?Boolean

Nilable version of ‘_Integer`

Returns:

  • (Boolean)


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`

Returns:

  • (Boolean)


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`

Returns:

  • (Boolean)


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`

Returns:

  • (Boolean)


270
271
272
273
274
# File 'lib/literal/types.rb', line 270

def _JSONData?(...)
  _Nilable(
    _JSONData(...)
  )
end

#_LambdaObject

Matches if the value is a ‘Proc` and `#lambda?` returns truthy.



277
278
279
# File 'lib/literal/types.rb', line 277

def _Lambda
  LambdaType
end

#_Lambda?Boolean

Nilable version of ‘_Lambda`

Returns:

  • (Boolean)


282
283
284
# File 'lib/literal/types.rb', line 282

def _Lambda?
  NilableLambdaType
end

#_Map(**shape) ⇒ Object

“‘ruby _Map(name: String, age: Integer) “`



289
290
291
# File 'lib/literal/types.rb', line 289

def _Map(**shape)
  MapType.new(shape)
end

#_Map?Boolean

Nilable version of ‘_Map` “`ruby _Map?(name: String, age: Integer) “`

Returns:

  • (Boolean)


297
298
299
300
301
# File 'lib/literal/types.rb', line 297

def _Map?(...)
  _Nilable(
    _Map(...)
  )
end

#_NeverObject

Never matches any value.



304
305
306
# File 'lib/literal/types.rb', line 304

def _Never
  NeverType::Instance
end

#_NilableObject

Matches if the value is either ‘nil` or the given type.



309
310
311
# File 'lib/literal/types.rb', line 309

def _Nilable(...)
  NilableType.new(...)
end

#_Not(*types) ⇒ Object

Matches if the given type is not matched.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/literal/types.rb', line 314

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

Raises:



331
332
333
334
335
336
337
338
339
340
341
# File 'lib/literal/types.rb', line 331

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



343
344
345
# File 'lib/literal/types.rb', line 343

def _Predicate(message, &block)
  PredicateType.new(message:, block:)
end

#_ProcableObject

Matches if the value is a ‘Proc` or responds to `#to_proc`.



348
349
350
# File 'lib/literal/types.rb', line 348

def _Procable
  ProcableType
end

#_Procable?Boolean

Nilable version ofo ‘_Procable`

Returns:

  • (Boolean)


353
354
355
# File 'lib/literal/types.rb', line 353

def _Procable?
  NilableProcableType
end

#_RangeObject

Matches if the value is a ‘Range` of the given type.



358
359
360
# File 'lib/literal/types.rb', line 358

def _Range(...)
  RangeType.new(...)
end

#_Range?Boolean

Nilable version of ‘_Range`

Returns:

  • (Boolean)


363
364
365
366
367
# File 'lib/literal/types.rb', line 363

def _Range?(...)
  _Nilable(
    _Range(...)
  )
end

#_SetObject

Matches if the value is a ‘Set` and all the elements match the given type.



370
371
372
# File 'lib/literal/types.rb', line 370

def _Set(...)
  SetType.new(...)
end

#_Set?Boolean

Nilable version of ‘_Set`

Returns:

  • (Boolean)


375
376
377
378
379
# File 'lib/literal/types.rb', line 375

def _Set?(...)
  _Nilable(
    _Set(...)
  )
end

#_StringObject

Matches if the value is a ‘String` and matches the given constraints. If you don’t need any constraints, use ‘String` instead of `_String`.



383
384
385
# File 'lib/literal/types.rb', line 383

def _String(...)
  _Constraint(String, ...)
end

#_String?Boolean

Nilable version of ‘_String`

Returns:

  • (Boolean)


388
389
390
391
392
# File 'lib/literal/types.rb', line 388

def _String?(...)
  _Nilable(
    _String(...)
  )
end

#_SymbolObject

Matches if the value is a ‘Symbol` and matches the given constraints.



395
396
397
# File 'lib/literal/types.rb', line 395

def _Symbol(...)
  _Constraint(Symbol, ...)
end

#_Symbol?Boolean

Nilable version of ‘_Symbol`

Returns:

  • (Boolean)


400
401
402
403
404
# File 'lib/literal/types.rb', line 400

def _Symbol?(...)
  _Nilable(
    _Symbol(...)
  )
end

#_TimeObject

Matches if the value is a ‘Time` and matches the given constraints. If you don’t need any constraints, use ‘Time` instead of `_Time`.



408
409
410
# File 'lib/literal/types.rb', line 408

def _Time(...)
  _Constraint(Time, ...)
end

#_Time?Boolean

Nilable version of ‘_Time`

Returns:

  • (Boolean)


413
414
415
416
417
# File 'lib/literal/types.rb', line 413

def _Time?(...)
  _Nilable(
    _Time(...)
  )
end

#_TruthyObject

Matches *“truthy”* values (anything except ‘nil` and `false`).



420
421
422
# File 'lib/literal/types.rb', line 420

def _Truthy
  TruthyType::Instance
end

#_Tuple(*types) ⇒ Object

Matches if the value is an ‘Array` and each element matches the given types in order. “`ruby _Tuple(String, Integer, Integer) “`



428
429
430
# File 'lib/literal/types.rb', line 428

def _Tuple(*types)
  TupleType.new(types)
end

#_Tuple?Boolean

Nilable version of ‘_Typle` “`ruby _Tuple?(String, Integer, Integer) “`

Returns:

  • (Boolean)


436
437
438
439
440
# File 'lib/literal/types.rb', line 436

def _Tuple?(...)
  _Nilable(
    _Tuple(...)
  )
end

#_Union(*types) ⇒ Object

Matches if any given type is matched.



443
444
445
# File 'lib/literal/types.rb', line 443

def _Union(*types)
  UnionType.new(types)
end

#_Union?Boolean

Nilable version of ‘_Union`

Returns:

  • (Boolean)


448
449
450
451
452
# File 'lib/literal/types.rb', line 448

def _Union?(...)
  _Nilable(
    _Union(...)
  )
end

#_Unit(object) ⇒ Object

The unit type is a type that matches only the same object



455
456
457
# File 'lib/literal/types.rb', line 455

def _Unit(object)
  UnitType.new(object)
end

#_Unit?Boolean

Nilable version of ‘_Unit`

Returns:

  • (Boolean)


460
461
462
463
464
# File 'lib/literal/types.rb', line 460

def _Unit?(...)
  _Nilable(
    _Unit(...)
  )
end

#_VoidObject



466
467
468
# File 'lib/literal/types.rb', line 466

def _Void
  VoidType::Instance
end