iry 0.7.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/sig/iry.rbs DELETED
@@ -1,520 +0,0 @@
1
- # Entrypoint of constraint validation, include in a class inheriting {ActiveRecord::Base} and the following class-level
2
- # methods will be available:
3
- # - {Macros#constraints}
4
- # - {Macros#check_constraint}
5
- # - {Macros#exclusion_constraint}
6
- # - {Macros#foreign_key_constraint}
7
- # - {Macros#unique_constraint}
8
- #
9
- # @example User unique constraint validation
10
- # # The database schema has a unique constraint on email field
11
- # class User < ActiveRecord::Base
12
- # include Iry
13
- #
14
- # unique_constraint :email
15
- # end
16
- #
17
- # user = User.create!(email: "user@example.com")
18
- # fail_user = User.new(email: "user@example.com")
19
- # Iry.save(fail_user)
20
- # fail_user.errors.details.fetch(:email) #=> [{error: :taken}]
21
- module Iry
22
- VERSION: String
23
-
24
- # Inherited from {ActiveRecord::RecordInvalid}, returns the model for
25
- # which the constraint violations have been detected
26
- def record: () -> Handlers::Model
27
-
28
- # _@param_ `klass`
29
- def self.included: (Module klass) -> void
30
-
31
- # Executes block and in case of constraints violations on `model`, block is
32
- # halted and errors are appended to `model`
33
- #
34
- # _@param_ `model` — model object for which constraints should be monitored and for which errors should be added to
35
- #
36
- # _@return_ — the `model` or `nil` if a a constraint is
37
- # violated
38
- #
39
- # Handle constraints for unique user
40
- # ```ruby
41
- # # The database schema has a unique constraint on email field
42
- # class User < ActiveRecord::Base
43
- # include Iry
44
- #
45
- # unique_constraint :email
46
- # end
47
- #
48
- # user = User.create!(email: "user@example.com")
49
- # fail_user = User.new(email: "user@example.com")
50
- # result = Iry.handle_constraints(fail_user) { fail_user.save }
51
- # result #=> nil
52
- # fail_user.errors.details.fetch(:email) #=> [{error: :taken}]
53
- # ```
54
- def self.handle_constraints: (Handlers::Model model) -> void
55
-
56
- # Executes block and in case of constraints violations on `model`, block is
57
- # halted, errors are appended to `model` and {StatementInvalid} is raised
58
- #
59
- # _@param_ `model` — model object for which constraints should be monitored and for which errors should be added to
60
- #
61
- # _@return_ — returns `model` parameter
62
- def self.handle_constraints!: (Handlers::Model model) -> Handlers::Model
63
-
64
- # Similar to {ActiveRecord::Base#save} but in case of constraint violations,
65
- # `false` is returned and `errors` are populated.
66
- # Aside from `model`, it takes the same arguments as
67
- # {ActiveRecord::Base#save}
68
- #
69
- # _@param_ `model` — model to save
70
- #
71
- # _@return_ — `true` if successful
72
- def self.save: (Handlers::Model model) -> bool
73
-
74
- # Similar to {ActiveRecord::Base#save!} but in case of constraint violations,
75
- # it raises {ConstraintViolation} and `errors` are populated.
76
- # Aside from `model`, it takes the same arguments as
77
- # {ActiveRecord::Base#save!}
78
- #
79
- # _@param_ `model` — model to save
80
- def self.save!: (Handlers::Model model) -> bool
81
-
82
- # Similar to {ActiveRecord::Base#destroy} but in case of constraint
83
- # violations, `false` is returned and `errors` are populated.
84
- #
85
- # _@param_ `model` — model to destroy
86
- #
87
- # _@return_ — the destroyed model
88
- def self.destroy: (Handlers::Model model) -> Handlers::Model
89
-
90
- # Included in all exceptions triggered by Iry, this allows to rescue any
91
- # gem-related exception by rescuing {Iry::Error}
92
- module Error
93
- end
94
-
95
- # Raised when constraints have been violated and have been converted to
96
- # model errors, on {ActiveRecord::Base#save!} calls, to simulate a behavior
97
- # similar to {ActiveRecord::RecordInvalid} when it's raised
98
- class ConstraintViolation < ActiveRecord::RecordInvalid
99
- include Iry::Error
100
- end
101
-
102
- # Raised when constraints errors happen and go through Iry, even if these
103
- # are not handled. This class inherits from {ActiveRecord::StatementInvalid}
104
- # to maximize compatibility with existing code
105
- class StatementInvalid < ActiveRecord::StatementInvalid
106
- include Iry::Error
107
-
108
- # sord warn - ActiveModel::Error wasn't able to be resolved to a constant in this project
109
- # sord omit - no YARD type given for "**kwargs", using untyped
110
- # _@param_ `message`
111
- #
112
- # _@param_ `record`
113
- #
114
- # _@param_ `error`
115
- def initialize: (
116
- ?String? message,
117
- record: Handlers::Model,
118
- error: ActiveModel::Error,
119
- **untyped kwargs
120
- ) -> void
121
-
122
- # _@return_ — model affected by the constraint violation
123
- attr_reader record: Handlers::Model
124
-
125
- # sord warn - ActiveModel::Error wasn't able to be resolved to a constant in this project
126
- # _@return_ — error attached to the `record` for the
127
- # constraint violation
128
- attr_reader error: ActiveModel::Error
129
- end
130
-
131
- # Overrides private API method {ActiveRecord#create_or_update} to handle
132
- # constraints and attach errors to the including model
133
- module Patch
134
- # Takes attributes as named arguments
135
- #
136
- # _@return_ — true if successful
137
- def create_or_update: () -> bool
138
- end
139
-
140
- # Class-level methods available to classes executing `include Iry`
141
- module Macros
142
- # Constraints by name
143
- def constraints: () -> ::Hash[String, Constraint]
144
-
145
- # Tracks check constraint for the given key and convert constraint errors into validation errors
146
- #
147
- # _@param_ `key` — key to apply validation errors to
148
- #
149
- # _@param_ `message` — the validation error message
150
- #
151
- # _@param_ `name` — constraint name. If omitted, it will be inferred using table name + key
152
- def check_constraint: (Symbol key, ?name: String?, ?message: (Symbol | String)) -> void
153
-
154
- # Tracks exclusion constraint for the given key and convert constraint errors into validation errors
155
- #
156
- # _@param_ `key` — key to apply validation errors to
157
- #
158
- # _@param_ `message` — the validation error message
159
- #
160
- # _@param_ `name` — constraint name. If omitted, it will be inferred using table name + key
161
- def exclusion_constraint: (Symbol key, ?name: String?, ?message: (Symbol | String)) -> void
162
-
163
- # Tracks foreign key constraint for the given key (or keys) and convert constraint errors into validation errors
164
- #
165
- # _@param_ `key_or_keys` — key or array of keys to track the foreign key constraint of
166
- #
167
- # _@param_ `message` — the validation error message
168
- #
169
- # _@param_ `name` — constraint name. If omitted, it will be inferred using table name + keys
170
- #
171
- # _@param_ `error_key` — key to which the validation error will be applied to. If omitted, it will be applied to the first key
172
- def foreign_key_constraint: (
173
- (Symbol | ::Array[Symbol]) key_or_keys,
174
- ?name: String?,
175
- ?message: (Symbol | String),
176
- ?error_key: Symbol?
177
- ) -> void
178
-
179
- # Tracks uniqueness constraint for the given key (or keys) and convert constraint errors into validation errors
180
- #
181
- # _@param_ `key_or_keys` — key or array of keys to track the uniqueness constraint of
182
- #
183
- # _@param_ `message` — the validation error message
184
- #
185
- # _@param_ `name` — constraint name. If omitted, it will be inferred using table name + keys
186
- #
187
- # _@param_ `error_key` — key to which the validation error will be applied to. If omitted, it will be applied to the first key
188
- def unique_constraint: (
189
- (Symbol | ::Array[Symbol]) key_or_keys,
190
- ?name: String?,
191
- ?message: (Symbol | String),
192
- ?error_key: Symbol?
193
- ) -> void
194
- end
195
-
196
- module Handlers
197
- # Interface for handlers of different database types
198
- # @abstract
199
- module Handler
200
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
201
- # _@param_ `err` — possible constraint error to handle
202
- #
203
- # _@return_ — true if this database handler is the correct one for this exception
204
- def handle?: (ActiveRecord::StatementInvalid err) -> bool
205
-
206
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
207
- # _@param_ `err` — possible constraint error to handle
208
- #
209
- # _@param_ `model`
210
- #
211
- # _@return_ — `nil` if couldn't handle the error,
212
- # otherwise the {ActiveModel::Error} added to the model
213
- def handle: (ActiveRecord::StatementInvalid err, Model model) -> void
214
- end
215
-
216
- # Interface of the model class. This class is usually inherits from {ActiveRecord::Base}
217
- # @abstract
218
- module ModelClass
219
- def table_name: () -> String
220
-
221
- def constraints: () -> ::Hash[String, Constraint]
222
- end
223
-
224
- # Interface of the model that should be used to handle constraints.
225
- # This object is an instance of {ActiveRecord::Base}
226
- # @abstract
227
- module Model
228
- # sord warn - ActiveModel::Errors wasn't able to be resolved to a constant in this project
229
- def errors: () -> ActiveModel::Errors
230
-
231
- def class: () -> ModelClass
232
- end
233
-
234
- # PostgreSQL handler through `pg` gem
235
- # @private
236
- module PG
237
- extend Iry::Handlers::PG
238
- REGEX: Regexp
239
-
240
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
241
- # When true, the handler is able to handle this exception, representing a constraint error in PostgreSQL.
242
- # This method must ensure not to raise exception in case the postgresql adapter is missing and as such, the
243
- # postgres constant is undefined
244
- #
245
- # _@param_ `err`
246
- def handle?: (ActiveRecord::StatementInvalid err) -> bool
247
-
248
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
249
- # Appends constraint errors as model errors
250
- #
251
- # _@param_ `err`
252
- #
253
- # _@param_ `model` — should inherit {ActiveRecord::Base} and`include Iry` to match the interface
254
- #
255
- # _@return_ — if handled constraint, returns the
256
- # error attached to the model. If constraint wasn't handled or handling
257
- # failed, `nil` is returned
258
- def handle: (ActiveRecord::StatementInvalid err, Model model) -> void
259
-
260
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
261
- # When true, the handler is able to handle this exception, representing a constraint error in PostgreSQL.
262
- # This method must ensure not to raise exception in case the postgresql adapter is missing and as such, the
263
- # postgres constant is undefined
264
- #
265
- # _@param_ `err`
266
- def self.handle?: (ActiveRecord::StatementInvalid err) -> bool
267
-
268
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
269
- # Appends constraint errors as model errors
270
- #
271
- # _@param_ `err`
272
- #
273
- # _@param_ `model` — should inherit {ActiveRecord::Base} and`include Iry` to match the interface
274
- #
275
- # _@return_ — if handled constraint, returns the
276
- # error attached to the model. If constraint wasn't handled or handling
277
- # failed, `nil` is returned
278
- def self.handle: (ActiveRecord::StatementInvalid err, Model model) -> void
279
- end
280
-
281
- # Catch-all handler for unrecognized database adapters
282
- # @private
283
- module Null
284
- extend Iry::Handlers::Null
285
-
286
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
287
- # Returns always true, catching any unhandled database exception
288
- #
289
- # _@param_ `err`
290
- def handle?: ((StandardError | ActiveRecord::StatementInvalid) err) -> bool
291
-
292
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
293
- # Return always false, failing to handle any constraint
294
- #
295
- # _@param_ `err`
296
- #
297
- # _@param_ `model` — should inherit {ActiveRecord::Base} and`include Iry` to match the interface
298
- def handle: (ActiveRecord::StatementInvalid err, Model model) -> void
299
-
300
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
301
- # Returns always true, catching any unhandled database exception
302
- #
303
- # _@param_ `err`
304
- def self.handle?: ((StandardError | ActiveRecord::StatementInvalid) err) -> bool
305
-
306
- # sord warn - ActiveRecord::StatementInvalid wasn't able to be resolved to a constant in this project
307
- # Return always false, failing to handle any constraint
308
- #
309
- # _@param_ `err`
310
- #
311
- # _@param_ `model` — should inherit {ActiveRecord::Base} and`include Iry` to match the interface
312
- def self.handle: (ActiveRecord::StatementInvalid err, Model model) -> void
313
- end
314
- end
315
-
316
- # Main function to kick-off **Iry** constraint-checking mechanism
317
- # If interested in adding support for other databases beside Postgres, modify this file.
318
- # @private
319
- module Callbacks
320
- extend Iry::Callbacks
321
-
322
- # _@param_ `model`
323
- def around_save: (Handlers::Model model) -> void
324
-
325
- # _@param_ `model`
326
- def self.around_save: (Handlers::Model model) -> void
327
- end
328
-
329
- # Interface representing a constraint.
330
- # A constraint has a name and can apply errors to an object inheriting from {ActiveRecord::Base}
331
- # @abstract
332
- module Constraint
333
- # sord warn - ActiveModel::Error wasn't able to be resolved to a constant in this project
334
- # Sets validation errors on the model
335
- #
336
- # _@param_ `model`
337
- def apply: (Handlers::Model model) -> ActiveModel::Error
338
-
339
- # Name of the constraint to be caught from the database
340
- def name: () -> String
341
-
342
- # Message to be attached as validation error to the model
343
- # (see Handlers::Model)
344
- def message: () -> (Symbol | String)
345
-
346
- class Check
347
- # Infers the check constraint name based on key and table name
348
- #
349
- # _@param_ `key`
350
- #
351
- # _@param_ `table_name`
352
- def self.infer_name: (Symbol key, String table_name) -> String
353
-
354
- # _@param_ `key` — key to apply error message for check constraint to
355
- #
356
- # _@param_ `message` — the validation error message
357
- #
358
- # _@param_ `name` — constraint name
359
- def initialize: (Symbol key, name: String, ?message: (Symbol | String)) -> void
360
-
361
- # sord warn - ActiveModel::Error wasn't able to be resolved to a constant in this project
362
- # _@param_ `model`
363
- def apply: (Handlers::Model model) -> ActiveModel::Error
364
-
365
- attr_accessor key: Symbol
366
-
367
- attr_accessor message: (Symbol | String)
368
-
369
- attr_accessor name: String
370
- end
371
-
372
- class Unique
373
- MAX_INFER_NAME_BYTE_SIZE: untyped
374
-
375
- # Infers the unique constraint name based on keys and table name
376
- #
377
- # _@param_ `keys`
378
- #
379
- # _@param_ `table_name`
380
- def self.infer_name: (::Array[Symbol] keys, String table_name) -> String
381
-
382
- # _@param_ `keys` — array of keys to track the uniqueness constraint of
383
- #
384
- # _@param_ `message` — the validation error message
385
- #
386
- # _@param_ `name` — constraint name
387
- #
388
- # _@param_ `error_key` — key to which the validation error will be applied to
389
- def initialize: (
390
- ::Array[Symbol] keys,
391
- name: String,
392
- error_key: Symbol,
393
- ?message: (Symbol | String)
394
- ) -> void
395
-
396
- # sord warn - ActiveModel::Error wasn't able to be resolved to a constant in this project
397
- # _@param_ `model`
398
- def apply: (Handlers::Model model) -> ActiveModel::Error
399
-
400
- attr_accessor keys: ::Array[Symbol]
401
-
402
- attr_accessor message: (Symbol | String)
403
-
404
- attr_accessor name: String
405
-
406
- attr_accessor error_key: Symbol
407
- end
408
-
409
- class Exclusion
410
- # Infers the exclusion constraint name based on key and table name
411
- #
412
- # _@param_ `key`
413
- #
414
- # _@param_ `table_name`
415
- def self.infer_name: (Symbol key, String table_name) -> String
416
-
417
- # _@param_ `key` — key to apply error message for exclusion constraint to
418
- #
419
- # _@param_ `message` — the validation error message
420
- #
421
- # _@param_ `name` — constraint name
422
- def initialize: (Symbol key, name: String, ?message: (Symbol | String)) -> void
423
-
424
- # sord warn - ActiveModel::Error wasn't able to be resolved to a constant in this project
425
- # _@param_ `model`
426
- def apply: (Handlers::Model model) -> ActiveModel::Error
427
-
428
- attr_accessor key: Symbol
429
-
430
- attr_accessor message: (Symbol | String)
431
-
432
- attr_accessor name: String
433
- end
434
-
435
- class ForeignKey
436
- # Infers the unique constraint name based on keys and table name
437
- #
438
- # _@param_ `keys`
439
- #
440
- # _@param_ `table_name`
441
- def self.infer_name: (::Array[Symbol] keys, String table_name) -> String
442
-
443
- # _@param_ `keys` — array of keys to track the uniqueness constraint of
444
- #
445
- # _@param_ `message` — the validation error message
446
- #
447
- # _@param_ `name` — constraint name
448
- #
449
- # _@param_ `error_key` — key to which the validation error will be applied to
450
- def initialize: (
451
- ::Array[Symbol] keys,
452
- name: String,
453
- error_key: Symbol,
454
- ?message: (Symbol | String)
455
- ) -> void
456
-
457
- # sord warn - ActiveModel::Error wasn't able to be resolved to a constant in this project
458
- # _@param_ `model`
459
- def apply: (Handlers::Model model) -> ActiveModel::Error
460
-
461
- attr_accessor keys: ::Array[Symbol]
462
-
463
- attr_accessor message: (Symbol | String)
464
-
465
- attr_accessor name: String
466
-
467
- attr_accessor error_key: Symbol
468
- end
469
- end
470
-
471
- # Implementation of {Iry} methods, helps ensuring the main module focus on
472
- # documentation
473
- module TransformConstraints
474
- extend Iry::TransformConstraints
475
-
476
- # _@param_ `model`
477
- def handle_constraints: (Handlers::Model model) -> void
478
-
479
- # _@param_ `model`
480
- def handle_constraints!: (Handlers::Model model) -> Handlers::Model
481
-
482
- # Tracks constraints of models saved as a consequence of saving another
483
- # model. This usually represents a situation of model using
484
- # `accepts_nested_attributes_for`
485
- #
486
- # _@param_ `model`
487
- def nested_constraints!: (Handlers::Model model) -> Handlers::Model
488
-
489
- # _@param_ `model`
490
- def save: (Handlers::Model model) -> bool
491
-
492
- # _@param_ `model`
493
- def save!: (Handlers::Model model) -> bool
494
-
495
- # _@param_ `model`
496
- def destroy: (Handlers::Model model) -> Handlers::Model
497
-
498
- # _@param_ `model`
499
- def self.handle_constraints: (Handlers::Model model) -> void
500
-
501
- # _@param_ `model`
502
- def self.handle_constraints!: (Handlers::Model model) -> Handlers::Model
503
-
504
- # Tracks constraints of models saved as a consequence of saving another
505
- # model. This usually represents a situation of model using
506
- # `accepts_nested_attributes_for`
507
- #
508
- # _@param_ `model`
509
- def self.nested_constraints!: (Handlers::Model model) -> Handlers::Model
510
-
511
- # _@param_ `model`
512
- def self.save: (Handlers::Model model) -> bool
513
-
514
- # _@param_ `model`
515
- def self.save!: (Handlers::Model model) -> bool
516
-
517
- # _@param_ `model`
518
- def self.destroy: (Handlers::Model model) -> Handlers::Model
519
- end
520
- end