Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Various improvements to LSP docs

+81 -24
+81 -24
lib/literal/types.rb
··· 37 37 NilableProcableType = NilableType.new(ProcableType).freeze 38 38 39 39 # Matches any value except `nil`. Use `_Any?` or `_Void` to match any value including `nil`. 40 + # ```ruby 41 + # _Any 42 + # ``` 40 43 def _Any 41 44 AnyType::Instance 42 45 end 43 46 47 + # Matches any value including `nil`. This is the same as `_Void` and the opposite of `_Never`. 48 + # ```ruby 49 + # _Any? 50 + # ``` 44 51 def _Any? 45 52 VoidType::Instance 46 53 end 47 54 48 - # Matches if the value is an `Array` and all the elements match the given type. 49 - def _Array(...) 50 - ArrayType.new(...) 55 + # Matches if the value is an `Array` and all the elements of the array match the given type. 56 + # ```ruby 57 + # _Array(String) 58 + # ``` 59 + def _Array(type) 60 + ArrayType.new(type) 51 61 end 52 62 53 - # Nilable version of `_Array` 54 - def _Array?(...) 63 + # Nilable version of `_Array`. 64 + # ```ruby 65 + # _Array?(String) 66 + # ``` 67 + def _Array?(type) 55 68 NilableType.new( 56 - ArrayType.new(...) 69 + ArrayType.new(type) 57 70 ) 58 71 end 59 72 60 - # Matches if the value is `true` or `false`. 73 + # Matches if the value is either `true` or `false`. This is equivalent to `_Union(true, false)`. 74 + # ```ruby 75 + # _Boolean 76 + # ``` 61 77 def _Boolean 62 78 BooleanType::Instance 63 79 end 64 80 65 - # Nilable version of `_Boolean` 81 + # Nilable version of `_Boolean`. 82 + # ```ruby 83 + # _Boolean? 84 + # ``` 66 85 def _Boolean? 67 86 NilableBooleanType 68 87 end 69 88 70 89 # Matches if the value responds to `#call`. 90 + # ```ruby 91 + # _Callable 92 + # ``` 71 93 def _Callable 72 94 CallableType 73 95 end 74 96 75 - # Nilabl version of `_Callable` 97 + # Nilable version of `_Callable`. 98 + # ```ruby 99 + # _Callable? 100 + # ``` 76 101 def _Callable? 77 102 NilableCallableType 78 103 end 79 104 80 105 # Matches if the value either the given class or a subclass of it. 81 - def _Class(...) 82 - ClassType.new(...) 106 + # ```ruby 107 + # _Class(ActiveRecord::Base) 108 + # ``` 109 + def _Class(expected_class) 110 + ClassType.new(expected_class) 83 111 end 84 112 85 - # Nilable version of `_Class` 113 + # Nilable version of `_Class`. 114 + # ```ruby 115 + # _Class?(ActiveRecord::Base) 116 + # ``` 86 117 def _Class?(...) 87 118 NilableType.new( 88 119 ClassType.new(...) ··· 90 121 end 91 122 92 123 # Similar to `_Intersection`, but allows you to specify attribute constraints as keyword arguments. 93 - # @example 94 - # _Constraint(Array, size: 1..3) 124 + # ```ruby 125 + # _Constraint(Array, size: 1..3) 126 + # ``` 95 127 def _Constraint(...) 96 128 ConstraintType.new(...) 97 129 end 98 130 99 131 # Nilable version of `_Constraint` 132 + # ```ruby 133 + # _Constraint?(Array, size: 1..3) 134 + # ``` 100 135 def _Constraint?(...) 101 136 NilableType.new( 102 137 ConstraintType.new(...) ··· 104 139 end 105 140 106 141 # Matches if the value is a `Date` and matches the given constraints. 107 - # If you don't need any constraints, use `Date` instead of `_Date`. 142 + # If you don't need any constraints, use `Date` instead of `_Date`. See also `_Constraint`. 143 + # ```ruby 144 + # _Date((Date.today)..) 145 + # _Date(year: 2025) 146 + # ``` 108 147 def _Date(...) 109 148 _Constraint(Date, ...) 110 149 end 111 150 112 - # Nilable version of `_Date` 151 + # Nilable version of `_Date`. 113 152 def _Date?(...) 114 153 _Nilable( 115 154 _Date(...) 116 155 ) 117 156 end 118 157 119 - def _Deferred(...) 120 - DeferredType.new(...) 158 + # 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. 159 + # ```ruby 160 + # _Deferred { _Class(SomeFutureConstant) } 161 + # ``` 162 + def _Deferred(&type) 163 + DeferredType.new(&type) 164 + end 165 + 166 + # Nilable version of `_Deferred`. 167 + def _Deferred?(&type) 168 + _Nilable(_Deferred(&type)) 121 169 end 122 170 123 171 # Matches if the value is a descendant of the given class. 172 + # ```ruby 173 + # _Descendant(ActiveRecord::Base) 174 + # ``` 124 175 def _Descendant(...) 125 176 DescendantType.new(...) 126 177 end 127 178 128 - # Nilable version of `_Descendant` 179 + # Nilable version of `_Descendant`. 129 180 def _Descendant?(...) 130 181 NilableType.new( 131 182 DescendantType.new(...) ··· 133 184 end 134 185 135 186 #  Matches if the value is an `Enumerable` and all its elements match the given type. 136 - def _Enumerable(...) 137 - EnumerableType.new(...) 187 + # ```ruby 188 + # _Enumerable(String) 189 + # ``` 190 + def _Enumerable(type) 191 + EnumerableType.new(type) 138 192 end 139 193 140 - # Nilable version of `_Enumerable` 194 + # Nilable version of `_Enumerable`. 141 195 def _Enumerable?(...) 142 196 NilableType.new( 143 197 EnumerableType.new(...) 144 198 ) 145 199 end 146 200 147 - # Matches *"falsy"* values (`nil` and `false`). 201 + # Matches *"falsy"* values (`nil` and `false`). This is equivalent to `_Nilable(false)` or `_Union(nil, false)`. 148 202 def _Falsy 149 203 FalsyType::Instance 150 204 end ··· 152 206 # Matches if the value is a `Float` and matches the given constraints. 153 207 # You could use a `Range`, for example, as a constraint. 154 208 # If you don't need a constraint, use `Float` instead of `_Float`. 209 + # ```ruby 210 + # _Float(5..10) 211 + # ``` 155 212 def _Float(...) 156 213 _Constraint(Float, ...) 157 214 end 158 215 159 - # Nilable version of `_Float` 216 + # Nilable version of `_Float`. 160 217 def _Float?(...) 161 218 _Nilable( 162 219 _Float(...)