sleeping_king_studios-tools 1.3.0 → 1.4.0.rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -0
- data/README.md +0 -2
- data/lib/sleeping_king_studios/tools/assertions/messages_strategy.rb +1 -2
- data/lib/sleeping_king_studios/tools/assertions.rb +188 -76
- data/lib/sleeping_king_studios/tools/messages/strategies/file_strategy.rb +22 -4
- data/lib/sleeping_king_studios/tools/messages/strategies/hash_strategy.rb +73 -14
- data/lib/sleeping_king_studios/tools/messages/strategy.rb +103 -4
- data/lib/sleeping_king_studios/tools/messages.rb +31 -5
- data/lib/sleeping_king_studios/tools/toolbox/heritable_data.rb +98 -138
- data/lib/sleeping_king_studios/tools/version.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d1f7af7edc3cd3e319188aa4f03eaa99bc5819d916401a1d005dd7c77a543dbe
|
|
4
|
+
data.tar.gz: 27145205ab33397a8836491c290d7c1fdfc40f3bbccc513299ce5b340c634881
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3d0cae267831f06774aaeff7a3de65ea36f7ed04bc2a6f7b6275cbc0a710d3ff9333dd34d5578b50f937a0f7b01a7fdc643e3fa93255eed34dfb85f127dd7a53
|
|
7
|
+
data.tar.gz: 4b8280a92271a1f8117da678847c78b47c873fb61037289d6a599e96fc17aa038d060ae830ebeb4661ef3f55b46bb5c845a684ef6cef080ed598164aba05a231
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,45 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.4.0
|
|
4
|
+
|
|
5
|
+
### Heritable Data
|
|
6
|
+
|
|
7
|
+
Refactored `HeritableData` internals.
|
|
8
|
+
|
|
9
|
+
- Nested `HeritableData` classes now inherit properly, defining a correct chain of `ancestors`.
|
|
10
|
+
- Some approaches to defining a heritable Data class are now deprecated and will print a warning:
|
|
11
|
+
- Defining class methods or including modules indirectly using the `included` lifecycle hook. Define class methods and include modules directly in the block. Starting on version 2.0, the `included` hook **will not be called**.
|
|
12
|
+
- Calling `HeritableData.define` (or `.define` on a generated subclass) with a `lambda` that does not accept any parameters. Starting on version 2.0, this will raise an `ArgumentError`.
|
|
13
|
+
|
|
14
|
+
> Special thanks to `@havenwood` from the Ruby Discord on the solution here.
|
|
15
|
+
|
|
16
|
+
> **Note:** This change will cause a regression in the specific case where code expects the **parent class** of the generated `HeritableData` class to be `Data`. This is no longer true for nested Data subclasses.
|
|
17
|
+
|
|
18
|
+
### Tools
|
|
19
|
+
|
|
20
|
+
#### Assertions
|
|
21
|
+
|
|
22
|
+
Implemented predicate methods:
|
|
23
|
+
|
|
24
|
+
- `#blank?(value)` checks if the value is `nil` or `#empty?`.
|
|
25
|
+
- `#boolean?(value)` checks if the value is `true` or `false`.
|
|
26
|
+
- `#name?(value)` checks if the value is a non-empty `String` or `Symbol`.
|
|
27
|
+
- `#present?(value)` checks if the value is not `nil` and not `#empty?`.
|
|
28
|
+
|
|
29
|
+
#### Messages
|
|
30
|
+
|
|
31
|
+
Updated `Messages::Strategy`:
|
|
32
|
+
|
|
33
|
+
- Implemented `Messages::Strategy#get`, `#fetch` methods to query raw message templates.
|
|
34
|
+
- Added `:default` keyword to `Messages::Strategy#call`.
|
|
35
|
+
- Added `:reraise_exceptions` option to `Messages::Strategy#call`.
|
|
36
|
+
- Added internal support for `false` values when resolving templates.
|
|
37
|
+
|
|
38
|
+
Updated `Messages::HashStrategy`, `FileStrategy`:
|
|
39
|
+
|
|
40
|
+
- Added `:flatten_templates` keyword to `HashStrategy#initialize`: set to `false` to allow querying for `Hash` values using the `#get` or `#fetch` methods.
|
|
41
|
+
- Added `:validate_values` keyword to `HashStrategy#initialize`: set to `false` to disable validation, or pass a `Proc` to customize the validation (a value will fail the validation if the Proc returns an error message when called with the value).
|
|
42
|
+
|
|
3
43
|
## 1.3.0
|
|
4
44
|
|
|
5
45
|
### Tools
|
data/README.md
CHANGED
|
@@ -15,8 +15,6 @@ SleepingKingStudios::Tools is tested against MRI Ruby 3.2 through 4.0.
|
|
|
15
15
|
|
|
16
16
|
### Documentation
|
|
17
17
|
|
|
18
|
-
Method and class documentation is available courtesy of RubyDoc.
|
|
19
|
-
|
|
20
18
|
Documentation is generated using [YARD](https://yardoc.org/), and can be generated locally using the `yard` gem.
|
|
21
19
|
|
|
22
20
|
### License
|
|
@@ -8,7 +8,7 @@ module SleepingKingStudios::Tools
|
|
|
8
8
|
# Defines its own internal message templates, and automatically handles value
|
|
9
9
|
# name option (:as option) when generating messages.
|
|
10
10
|
class Assertions::MessagesStrategy < SleepingKingStudios::Tools::Messages::Strategies::HashStrategy
|
|
11
|
-
#
|
|
11
|
+
# Message fragments used to generate error messages for failed assertions.
|
|
12
12
|
ERROR_MESSAGES =
|
|
13
13
|
{
|
|
14
14
|
'blank' =>
|
|
@@ -51,7 +51,6 @@ module SleepingKingStudios::Tools
|
|
|
51
51
|
'presence' =>
|
|
52
52
|
"can't be blank"
|
|
53
53
|
}.freeze
|
|
54
|
-
# rubocop:enable Layout/HashAlignment
|
|
55
54
|
|
|
56
55
|
def initialize
|
|
57
56
|
templates =
|
|
@@ -31,10 +31,10 @@ module SleepingKingStudios::Tools
|
|
|
31
31
|
# @raise [AssertionError] if the block does not return a truthy value.
|
|
32
32
|
#
|
|
33
33
|
# @example
|
|
34
|
-
#
|
|
34
|
+
# assertions.assert { true == false }
|
|
35
35
|
# #=> raises an AssertionError with message 'block returned a falsy value'
|
|
36
36
|
#
|
|
37
|
-
#
|
|
37
|
+
# assertions.assert { true == true }
|
|
38
38
|
# #=> does not raise an exception
|
|
39
39
|
def assert(error_class: AssertionError, message: nil, &block)
|
|
40
40
|
return if block.call
|
|
@@ -46,6 +46,9 @@ module SleepingKingStudios::Tools
|
|
|
46
46
|
|
|
47
47
|
# Asserts that the value is either nil or empty.
|
|
48
48
|
#
|
|
49
|
+
# A value is blank if it is nil, or if the value responds to #empty? and the
|
|
50
|
+
# #empty? method returns true.
|
|
51
|
+
#
|
|
49
52
|
# @param value [Object] the value to assert on.
|
|
50
53
|
# @param as [String] the name of the asserted value.
|
|
51
54
|
# @param error_class [Class] the exception class to raise on a failure.
|
|
@@ -57,16 +60,16 @@ module SleepingKingStudios::Tools
|
|
|
57
60
|
# to #empty? or value.empty returns false.
|
|
58
61
|
#
|
|
59
62
|
# @example
|
|
60
|
-
#
|
|
63
|
+
# assertions.assert_blank(nil)
|
|
61
64
|
# #=> does not raise an exception
|
|
62
65
|
#
|
|
63
|
-
#
|
|
66
|
+
# assertions.assert_blank(Object.new)
|
|
64
67
|
# #=> raises an AssertionError with message 'value must be nil or empty'
|
|
65
68
|
#
|
|
66
|
-
#
|
|
69
|
+
# assertions.assert_blank([])
|
|
67
70
|
# #=> does not raise an exception
|
|
68
71
|
#
|
|
69
|
-
#
|
|
72
|
+
# assertions.assert_blank([1, 2, 3])
|
|
70
73
|
# #=> raises an AssertionError with message 'value must be nil or empty'
|
|
71
74
|
def assert_blank(
|
|
72
75
|
value,
|
|
@@ -74,8 +77,7 @@ module SleepingKingStudios::Tools
|
|
|
74
77
|
error_class: AssertionError,
|
|
75
78
|
message: nil
|
|
76
79
|
)
|
|
77
|
-
return if value
|
|
78
|
-
return if value.respond_to?(:empty?) && value.empty?
|
|
80
|
+
return if blank?(value)
|
|
79
81
|
|
|
80
82
|
message ||= error_message_for('blank', as:)
|
|
81
83
|
|
|
@@ -95,16 +97,16 @@ module SleepingKingStudios::Tools
|
|
|
95
97
|
# @raise [AssertionError] if the value is not true or false.
|
|
96
98
|
#
|
|
97
99
|
# @example
|
|
98
|
-
#
|
|
100
|
+
# assertions.assert_boolean(nil)
|
|
99
101
|
# #=> raises an AssertionError with message 'value must be true or false'
|
|
100
102
|
#
|
|
101
|
-
#
|
|
103
|
+
# assertions.assert_boolean(Object.new)
|
|
102
104
|
# #=> raises an AssertionError with message 'value must be true or false'
|
|
103
105
|
#
|
|
104
|
-
#
|
|
106
|
+
# assertions.assert_boolean(false)
|
|
105
107
|
# #=> does not raise an exception
|
|
106
108
|
#
|
|
107
|
-
#
|
|
109
|
+
# assertions.assert_boolean(true)
|
|
108
110
|
# #=> does not raise an exception
|
|
109
111
|
def assert_boolean(
|
|
110
112
|
value,
|
|
@@ -135,10 +137,10 @@ module SleepingKingStudios::Tools
|
|
|
135
137
|
# @raise [AssertionError] if the value is not a Class.
|
|
136
138
|
#
|
|
137
139
|
# @example
|
|
138
|
-
#
|
|
140
|
+
# assertions.assert_class(Object.new)
|
|
139
141
|
# #=> raises an AssertionError with message 'value is not a class'
|
|
140
142
|
#
|
|
141
|
-
#
|
|
143
|
+
# assertions.assert_class(String)
|
|
142
144
|
# #=> does not raise an exception
|
|
143
145
|
def assert_class(
|
|
144
146
|
value,
|
|
@@ -169,10 +171,10 @@ module SleepingKingStudios::Tools
|
|
|
169
171
|
# @raise [AssertionError] if the value is one of the expected values.
|
|
170
172
|
#
|
|
171
173
|
# @example
|
|
172
|
-
#
|
|
174
|
+
# assertions.assert_exclusion('get', expected: %w[get post put])
|
|
173
175
|
# #=> raises an AssertionError with message 'value is is one of "get", "post", "put"'
|
|
174
176
|
#
|
|
175
|
-
#
|
|
177
|
+
# assertions.assert_exclusion('delete', expected: %w[get post put])
|
|
176
178
|
# #=> does not raise an exception
|
|
177
179
|
def assert_exclusion(
|
|
178
180
|
value,
|
|
@@ -205,7 +207,7 @@ module SleepingKingStudios::Tools
|
|
|
205
207
|
# @raise [AssertionError] if any of the assertions fail.
|
|
206
208
|
#
|
|
207
209
|
# @example
|
|
208
|
-
#
|
|
210
|
+
# assertions.assert_group do |group|
|
|
209
211
|
# group.assert_name(nil, as: 'label')
|
|
210
212
|
# group.assert_instance_of(0.0, expected: Integer, as: 'quantity')
|
|
211
213
|
# end
|
|
@@ -240,10 +242,10 @@ module SleepingKingStudios::Tools
|
|
|
240
242
|
# @raise [AssertionError] if the value is not one of the expected values.
|
|
241
243
|
#
|
|
242
244
|
# @example
|
|
243
|
-
#
|
|
245
|
+
# assertions.assert_inclusion('get', expected: %w[get post put])
|
|
244
246
|
# #=> does not raise an exception
|
|
245
247
|
#
|
|
246
|
-
#
|
|
248
|
+
# assertions.assert_inclusion('delete', expected: %w[get post put])
|
|
247
249
|
# #=> raises an AssertionError with message 'value is is one of "get", "post", "put"'
|
|
248
250
|
def assert_inclusion( # rubocop:disable Metrics/ParameterLists
|
|
249
251
|
value,
|
|
@@ -282,13 +284,13 @@ module SleepingKingStudios::Tools
|
|
|
282
284
|
# have the given ancestor.
|
|
283
285
|
#
|
|
284
286
|
# @example
|
|
285
|
-
#
|
|
287
|
+
# assertions.assert_inherits_from(:foo, expected: StandardError)
|
|
286
288
|
# #=> raises an AssertionError with message 'value is not a Class or Module'
|
|
287
289
|
#
|
|
288
|
-
#
|
|
290
|
+
# assertions.assert_inherits_from(RuntimeError, expected: StandardError)
|
|
289
291
|
# #=> does not raise an exception
|
|
290
292
|
#
|
|
291
|
-
#
|
|
293
|
+
# assertions.assert_inherits_from(Array, expected: Enumerable)
|
|
292
294
|
# #=> does not raise an exception
|
|
293
295
|
def assert_inherits_from( # rubocop:disable Metrics/ParameterLists
|
|
294
296
|
value,
|
|
@@ -332,10 +334,10 @@ module SleepingKingStudios::Tools
|
|
|
332
334
|
# class.
|
|
333
335
|
#
|
|
334
336
|
# @example
|
|
335
|
-
#
|
|
337
|
+
# assertions.assert_instance_of(:foo, expected: String)
|
|
336
338
|
# #=> raises an AssertionError with message 'value is not an instance of String'
|
|
337
339
|
#
|
|
338
|
-
#
|
|
340
|
+
# assertions.assert_instance_of('foo', expected: String)
|
|
339
341
|
# #=> does not raise an exception
|
|
340
342
|
def assert_instance_of( # rubocop:disable Metrics/ParameterLists
|
|
341
343
|
value,
|
|
@@ -371,10 +373,10 @@ module SleepingKingStudios::Tools
|
|
|
371
373
|
# @raise [AssertionError] if the value does not match the expected object.
|
|
372
374
|
#
|
|
373
375
|
# @example
|
|
374
|
-
#
|
|
376
|
+
# assertions.assert_matches('bar', expected: /foo/)
|
|
375
377
|
# #=> raises an AssertionError with message 'value does not match the pattern /foo/'
|
|
376
378
|
#
|
|
377
|
-
#
|
|
379
|
+
# assertions.assert_matches('foo', expected: /foo/)
|
|
378
380
|
# #=> does not raise an exception
|
|
379
381
|
def assert_matches( # rubocop:disable Metrics/ParameterLists
|
|
380
382
|
value,
|
|
@@ -406,19 +408,19 @@ module SleepingKingStudios::Tools
|
|
|
406
408
|
# the value is empty.
|
|
407
409
|
#
|
|
408
410
|
# @example
|
|
409
|
-
#
|
|
411
|
+
# assertions.assert_name(nil)
|
|
410
412
|
# #=> raises an AssertionError with message "value can't be blank"
|
|
411
413
|
#
|
|
412
|
-
#
|
|
414
|
+
# assertions.assert_name(Object.new)
|
|
413
415
|
# #=> raises an AssertionError with message 'value is not a String or a Symbol'
|
|
414
416
|
#
|
|
415
|
-
#
|
|
417
|
+
# assertions.assert_name('')
|
|
416
418
|
# #=> raises an AssertionError with message "value can't be blank"
|
|
417
419
|
#
|
|
418
|
-
#
|
|
420
|
+
# assertions.assert_name('foo')
|
|
419
421
|
# #=> does not raise an exception
|
|
420
422
|
#
|
|
421
|
-
#
|
|
423
|
+
# assertions.assert_name(:bar)
|
|
422
424
|
# #=> does not raise an exception
|
|
423
425
|
def assert_name( # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
424
426
|
value,
|
|
@@ -460,10 +462,10 @@ module SleepingKingStudios::Tools
|
|
|
460
462
|
# @raise [AssertionError] if the value is not nil.
|
|
461
463
|
#
|
|
462
464
|
# @example
|
|
463
|
-
#
|
|
465
|
+
# assertions.assert_nil(nil)
|
|
464
466
|
# #=> does not raise an exception
|
|
465
467
|
#
|
|
466
|
-
#
|
|
468
|
+
# assertions.assert_nil(Object.new)
|
|
467
469
|
# #=> raises an AssertionError with message 'value must be nil'
|
|
468
470
|
def assert_nil(
|
|
469
471
|
value,
|
|
@@ -490,10 +492,10 @@ module SleepingKingStudios::Tools
|
|
|
490
492
|
# @raise [AssertionError] if the value is nil.
|
|
491
493
|
#
|
|
492
494
|
# @example
|
|
493
|
-
#
|
|
495
|
+
# assertions.assert_not_nil(nil)
|
|
494
496
|
# #=> raises an AssertionError with message 'value must not be nil'
|
|
495
497
|
#
|
|
496
|
-
#
|
|
498
|
+
# assertions.assert_not_nil(Object.new)
|
|
497
499
|
# #=> does not raise an exception
|
|
498
500
|
def assert_not_nil(
|
|
499
501
|
value,
|
|
@@ -510,6 +512,9 @@ module SleepingKingStudios::Tools
|
|
|
510
512
|
|
|
511
513
|
# Asserts that the value is not nil and not empty.
|
|
512
514
|
#
|
|
515
|
+
# A value is present if it is not nil, and if the value responds to #empty?
|
|
516
|
+
# then the #empty? method must returns false.
|
|
517
|
+
#
|
|
513
518
|
# @param value [Object] the value to assert on.
|
|
514
519
|
# @param as [String] the name of the asserted value.
|
|
515
520
|
# @param error_class [Class] the exception class to raise on a failure.
|
|
@@ -522,16 +527,16 @@ module SleepingKingStudios::Tools
|
|
|
522
527
|
# #empty? and value.empty is true.
|
|
523
528
|
#
|
|
524
529
|
# @example
|
|
525
|
-
#
|
|
530
|
+
# assertions.assert_presence(nil)
|
|
526
531
|
# #=> raises an AssertionError with message "can't be blank"
|
|
527
532
|
#
|
|
528
|
-
#
|
|
533
|
+
# assertions.assert_presence(Object.new)
|
|
529
534
|
# #=> does not raise an exception
|
|
530
535
|
#
|
|
531
|
-
#
|
|
536
|
+
# assertions.assert_presence([])
|
|
532
537
|
# #=> raises an AssertionError with message "can't be blank"
|
|
533
538
|
#
|
|
534
|
-
#
|
|
539
|
+
# assertions.assert_presence([1, 2, 3])
|
|
535
540
|
# #=> does not raise an exception
|
|
536
541
|
def assert_presence(
|
|
537
542
|
value,
|
|
@@ -555,6 +560,57 @@ module SleepingKingStudios::Tools
|
|
|
555
560
|
handle_error(error_class:, message:)
|
|
556
561
|
end
|
|
557
562
|
|
|
563
|
+
# Checks if the value is either nil or empty.
|
|
564
|
+
#
|
|
565
|
+
# A value is blank if it is nil, or if the value responds to #empty? and the
|
|
566
|
+
# #empty? method returns true.
|
|
567
|
+
#
|
|
568
|
+
# @param value [Object] the value to assert on.
|
|
569
|
+
#
|
|
570
|
+
# @return [true, false] true if the value is nil or empty, otherwise returns
|
|
571
|
+
# false.
|
|
572
|
+
#
|
|
573
|
+
# @example
|
|
574
|
+
# assertions.blank?(nil)
|
|
575
|
+
# #=> true
|
|
576
|
+
#
|
|
577
|
+
# assertions.blank?(Object.new)
|
|
578
|
+
# #=> false
|
|
579
|
+
#
|
|
580
|
+
# assertions.blank?([])
|
|
581
|
+
# #=> true
|
|
582
|
+
#
|
|
583
|
+
# assertions.blank?([1, 2, 3])
|
|
584
|
+
# #=> false
|
|
585
|
+
def blank?(value)
|
|
586
|
+
return true if value.nil?
|
|
587
|
+
|
|
588
|
+
value.respond_to?(:empty?) && value.empty?
|
|
589
|
+
end
|
|
590
|
+
|
|
591
|
+
# Checks if value is either true or false.
|
|
592
|
+
#
|
|
593
|
+
# @param value [Object] the value to assert on.
|
|
594
|
+
#
|
|
595
|
+
# @return [true, false] true if the value is either true or false, otherwise
|
|
596
|
+
# returns false.
|
|
597
|
+
#
|
|
598
|
+
# @example
|
|
599
|
+
# assertions.boolean?(nil)
|
|
600
|
+
# #=> false
|
|
601
|
+
#
|
|
602
|
+
# assertions.boolean?(Object.new)
|
|
603
|
+
# #=> false
|
|
604
|
+
#
|
|
605
|
+
# assertions.boolean?(false)
|
|
606
|
+
# #=> true
|
|
607
|
+
#
|
|
608
|
+
# assertions.boolean?(true)
|
|
609
|
+
# #=> true
|
|
610
|
+
def boolean?(value)
|
|
611
|
+
true == value || false == value # rubocop:disable Style/MultipleComparison, Style/YodaCondition
|
|
612
|
+
end
|
|
613
|
+
|
|
558
614
|
# Generates an error message for a failed validation.
|
|
559
615
|
#
|
|
560
616
|
# @param key [String] the message key.
|
|
@@ -582,6 +638,62 @@ module SleepingKingStudios::Tools
|
|
|
582
638
|
toolbelt.messages.message(key, as:, parameters:, scope:)
|
|
583
639
|
end
|
|
584
640
|
|
|
641
|
+
# Asserts that the value is a non-empty String or Symbol.
|
|
642
|
+
#
|
|
643
|
+
# @param value [Object] the value to assert on.
|
|
644
|
+
#
|
|
645
|
+
# @return [true, false] true if the value is a non-empty String or Symbol,
|
|
646
|
+
# otherwise returns false.
|
|
647
|
+
#
|
|
648
|
+
# @example
|
|
649
|
+
# assertions.name?(nil)
|
|
650
|
+
# #=> false
|
|
651
|
+
#
|
|
652
|
+
# assertions.name?(Object.new)
|
|
653
|
+
# #=> false
|
|
654
|
+
#
|
|
655
|
+
# assertions.name?('')
|
|
656
|
+
# #=> false
|
|
657
|
+
#
|
|
658
|
+
# assertions.name?('foo')
|
|
659
|
+
# #=> true
|
|
660
|
+
#
|
|
661
|
+
# assertions.name?(:bar)
|
|
662
|
+
# #=> true
|
|
663
|
+
def name?(value)
|
|
664
|
+
return false unless value.is_a?(String) || value.is_a?(Symbol)
|
|
665
|
+
|
|
666
|
+
!value.empty?
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
# Checks if the value is not nil and not empty.
|
|
670
|
+
#
|
|
671
|
+
# A value is present if it is not nil, and if the value responds to #empty?
|
|
672
|
+
# then the #empty? method must returns false.
|
|
673
|
+
#
|
|
674
|
+
# @param value [Object] the value to assert on.
|
|
675
|
+
#
|
|
676
|
+
# @return [true, false] true if the value is nil and not empty, otherwise
|
|
677
|
+
# returns false.
|
|
678
|
+
#
|
|
679
|
+
# @example
|
|
680
|
+
# assertions.present?(nil)
|
|
681
|
+
# #=> false
|
|
682
|
+
#
|
|
683
|
+
# assertions.present?(Object.new)
|
|
684
|
+
# #=> true
|
|
685
|
+
#
|
|
686
|
+
# assertions.present?([])
|
|
687
|
+
# #=> false
|
|
688
|
+
#
|
|
689
|
+
# assertions.present?([1, 2, 3])
|
|
690
|
+
# #=> true
|
|
691
|
+
def present?(value)
|
|
692
|
+
return false if value.nil?
|
|
693
|
+
|
|
694
|
+
!(value.respond_to?(:empty?) && value.empty?)
|
|
695
|
+
end
|
|
696
|
+
|
|
585
697
|
# Asserts that the block returns a truthy value.
|
|
586
698
|
#
|
|
587
699
|
# @param message [String] the exception message to raise on a failure.
|
|
@@ -594,10 +706,10 @@ module SleepingKingStudios::Tools
|
|
|
594
706
|
# @raise [ArgumentError] if the block does not return a truthy value.
|
|
595
707
|
#
|
|
596
708
|
# @example
|
|
597
|
-
#
|
|
709
|
+
# assertions.validate { true == false }
|
|
598
710
|
# #=> raises an ArgumentError with message 'block returned a falsy value'
|
|
599
711
|
#
|
|
600
|
-
#
|
|
712
|
+
# assertions.validate { true == true }
|
|
601
713
|
# #=> does not raise an exception
|
|
602
714
|
def validate(message: nil, &)
|
|
603
715
|
assert(
|
|
@@ -619,16 +731,16 @@ module SleepingKingStudios::Tools
|
|
|
619
731
|
# to #empty? or value.empty returns false.
|
|
620
732
|
#
|
|
621
733
|
# @example
|
|
622
|
-
#
|
|
734
|
+
# assertions.validate_blank(nil)
|
|
623
735
|
# #=> does not raise an exception
|
|
624
736
|
#
|
|
625
|
-
#
|
|
737
|
+
# assertions.validate_blank(Object.new)
|
|
626
738
|
# #=> raises an ArgumentError with message 'value must be nil or empty'
|
|
627
739
|
#
|
|
628
|
-
#
|
|
740
|
+
# assertions.validate_blank([])
|
|
629
741
|
# #=> does not raise an exception
|
|
630
742
|
#
|
|
631
|
-
#
|
|
743
|
+
# assertions.validate_blank([1, 2, 3])
|
|
632
744
|
# #=> raises an ArgumentError with message 'value must be nil or empty'
|
|
633
745
|
def validate_blank(value, as: 'value', message: nil)
|
|
634
746
|
assert_blank(
|
|
@@ -651,16 +763,16 @@ module SleepingKingStudios::Tools
|
|
|
651
763
|
# @raise [ArgumentError] if the value is not true or false.
|
|
652
764
|
#
|
|
653
765
|
# @example
|
|
654
|
-
#
|
|
766
|
+
# assertions.validate_boolean(nil)
|
|
655
767
|
# #=> raises an ArgumentError with message 'value must be true or false'
|
|
656
768
|
#
|
|
657
|
-
#
|
|
769
|
+
# assertions.validate_boolean(Object.new)
|
|
658
770
|
# #=> raises an ArgumentError with message 'value must be true or false'
|
|
659
771
|
#
|
|
660
|
-
#
|
|
772
|
+
# assertions.validate_boolean(false)
|
|
661
773
|
# #=> does not raise an exception
|
|
662
774
|
#
|
|
663
|
-
#
|
|
775
|
+
# assertions.validate_boolean(true)
|
|
664
776
|
# #=> does not raise an exception
|
|
665
777
|
def validate_boolean(value, as: 'value', message: nil, optional: false)
|
|
666
778
|
assert_boolean(
|
|
@@ -684,10 +796,10 @@ module SleepingKingStudios::Tools
|
|
|
684
796
|
# @raise [ArgumentError] if the value is not a Class.
|
|
685
797
|
#
|
|
686
798
|
# @example
|
|
687
|
-
#
|
|
799
|
+
# assertions.validate_class(Object.new)
|
|
688
800
|
# #=> raises an ArgumentError with message 'value is not a class'
|
|
689
801
|
#
|
|
690
|
-
#
|
|
802
|
+
# assertions.validate_class(String)
|
|
691
803
|
# #=> does not raise an exception
|
|
692
804
|
def validate_class(value, as: 'value', message: nil, optional: false)
|
|
693
805
|
assert_class(
|
|
@@ -711,10 +823,10 @@ module SleepingKingStudios::Tools
|
|
|
711
823
|
# @raise [ArgumentError] if the value is one of the expected values.
|
|
712
824
|
#
|
|
713
825
|
# @example
|
|
714
|
-
#
|
|
826
|
+
# assertions.validate_exclusion('get', expected: %w[get post put])
|
|
715
827
|
# #=> raises an ArgumentError with message 'value is is one of "get", "post", "put"'
|
|
716
828
|
#
|
|
717
|
-
#
|
|
829
|
+
# assertions.validate_exclusion('delete', expected: %w[get post put])
|
|
718
830
|
# #=> does not raise an exception
|
|
719
831
|
def validate_exclusion(
|
|
720
832
|
value,
|
|
@@ -743,7 +855,7 @@ module SleepingKingStudios::Tools
|
|
|
743
855
|
# @raise [ArgumentError] if any of the validations fail.
|
|
744
856
|
#
|
|
745
857
|
# @example
|
|
746
|
-
#
|
|
858
|
+
# assertions.validate_group do |group|
|
|
747
859
|
# group.validate_name(nil, as: 'label')
|
|
748
860
|
# group.validate_instance_of(0.0, expected: Integer, as: 'quantity')
|
|
749
861
|
# end
|
|
@@ -770,10 +882,10 @@ module SleepingKingStudios::Tools
|
|
|
770
882
|
# @raise [ArgumentError] if the value is not one of the expected values.
|
|
771
883
|
#
|
|
772
884
|
# @example
|
|
773
|
-
#
|
|
885
|
+
# assertions.validate_inclusion('get', expected: %w[get post put])
|
|
774
886
|
# #=> does not raise an exception
|
|
775
887
|
#
|
|
776
|
-
#
|
|
888
|
+
# assertions.validate_inclusion('delete', expected: %w[get post put])
|
|
777
889
|
# #=> raises an ArgumentError with message 'value is is one of "get", "post", "put"'
|
|
778
890
|
def validate_inclusion(
|
|
779
891
|
value,
|
|
@@ -807,13 +919,13 @@ module SleepingKingStudios::Tools
|
|
|
807
919
|
# have the given ancestor.
|
|
808
920
|
#
|
|
809
921
|
# @example
|
|
810
|
-
#
|
|
922
|
+
# assertions.validate_inherits_from(:foo, expected: StandardError)
|
|
811
923
|
# #=> raises an ArgumentError with message 'value is not a Class or Module'
|
|
812
924
|
#
|
|
813
|
-
#
|
|
925
|
+
# assertions.validate_inherits_from(RuntimeError, expected: StandardError)
|
|
814
926
|
# #=> does not raise an exception
|
|
815
927
|
#
|
|
816
|
-
#
|
|
928
|
+
# assertions.validate_inherits_from(Array, expected: Enumerable)
|
|
817
929
|
# #=> does not raise an exception
|
|
818
930
|
def validate_inherits_from(
|
|
819
931
|
value,
|
|
@@ -847,10 +959,10 @@ module SleepingKingStudios::Tools
|
|
|
847
959
|
# class or module.
|
|
848
960
|
#
|
|
849
961
|
# @example
|
|
850
|
-
#
|
|
962
|
+
# assertions.validate_instance_of(:foo, expected: String)
|
|
851
963
|
# #=> raises an AssertionError with message 'value is not an instance of String'
|
|
852
964
|
#
|
|
853
|
-
#
|
|
965
|
+
# assertions.validate_instance_of('foo', expected: String)
|
|
854
966
|
# #=> does not raise an exception
|
|
855
967
|
def validate_instance_of(
|
|
856
968
|
value,
|
|
@@ -882,10 +994,10 @@ module SleepingKingStudios::Tools
|
|
|
882
994
|
# @raise [ArgumentError] if the value does not match the expected object.
|
|
883
995
|
#
|
|
884
996
|
# @example
|
|
885
|
-
#
|
|
997
|
+
# assertions.validate_matches('bar', expected: /foo/)
|
|
886
998
|
# #=> raises an ArgumentError with message 'value does not match the pattern /foo/'
|
|
887
999
|
#
|
|
888
|
-
#
|
|
1000
|
+
# assertions.validate_matches('foo', expected: /foo/)
|
|
889
1001
|
# #=> does not raise an exception
|
|
890
1002
|
def validate_matches(
|
|
891
1003
|
value,
|
|
@@ -917,19 +1029,19 @@ module SleepingKingStudios::Tools
|
|
|
917
1029
|
# value is empty.
|
|
918
1030
|
#
|
|
919
1031
|
# @example
|
|
920
|
-
#
|
|
1032
|
+
# assertions.validate_name(nil)
|
|
921
1033
|
# #=> raises an ArgumentError with message "value can't be blank"
|
|
922
1034
|
#
|
|
923
|
-
#
|
|
1035
|
+
# assertions.validate_name(Object.new)
|
|
924
1036
|
# #=> raises an AssertionError with message 'value is not a String or a Symbol'
|
|
925
1037
|
#
|
|
926
|
-
#
|
|
1038
|
+
# assertions.validate_name('')
|
|
927
1039
|
# #=> raises an ArgumentError with message "value can't be blank"
|
|
928
1040
|
#
|
|
929
|
-
#
|
|
1041
|
+
# assertions.validate_name('foo')
|
|
930
1042
|
# #=> does not raise an exception
|
|
931
1043
|
#
|
|
932
|
-
#
|
|
1044
|
+
# assertions.validate_name(:bar)
|
|
933
1045
|
# #=> does not raise an exception
|
|
934
1046
|
def validate_name(
|
|
935
1047
|
value,
|
|
@@ -957,10 +1069,10 @@ module SleepingKingStudios::Tools
|
|
|
957
1069
|
# @raise [ArgumentError] if the value is not nil.
|
|
958
1070
|
#
|
|
959
1071
|
# @example
|
|
960
|
-
#
|
|
1072
|
+
# assertions.validate_nil(nil)
|
|
961
1073
|
# #=> does not raise an exception
|
|
962
1074
|
#
|
|
963
|
-
#
|
|
1075
|
+
# assertions.validate_nil(Object.new)
|
|
964
1076
|
# #=> raises an ArgumentError with message 'value must be nil'
|
|
965
1077
|
def validate_nil(
|
|
966
1078
|
value,
|
|
@@ -986,10 +1098,10 @@ module SleepingKingStudios::Tools
|
|
|
986
1098
|
# @raise [ArgumentError] if the value is nil.
|
|
987
1099
|
#
|
|
988
1100
|
# @example
|
|
989
|
-
#
|
|
1101
|
+
# assertions.validate_not_nil(nil)
|
|
990
1102
|
# #=> raises an ArgumentError with message 'value must not be nil'
|
|
991
1103
|
#
|
|
992
|
-
#
|
|
1104
|
+
# assertions.validate_not_nil(Object.new)
|
|
993
1105
|
# #=> does not raise an exception
|
|
994
1106
|
def validate_not_nil(
|
|
995
1107
|
value,
|
|
@@ -1017,16 +1129,16 @@ module SleepingKingStudios::Tools
|
|
|
1017
1129
|
# #empty? and value.empty is true.
|
|
1018
1130
|
#
|
|
1019
1131
|
# @example
|
|
1020
|
-
#
|
|
1132
|
+
# assertions.validate_presence(nil)
|
|
1021
1133
|
# #=> raises an ArgumentError with message "can't be blank"
|
|
1022
1134
|
#
|
|
1023
|
-
#
|
|
1135
|
+
# assertions.validate_presence(Object.new)
|
|
1024
1136
|
# #=> does not raise an exception
|
|
1025
1137
|
#
|
|
1026
|
-
#
|
|
1138
|
+
# assertions.validate_presence([])
|
|
1027
1139
|
# #=> raises an ArgumentError with message "can't be blank"
|
|
1028
1140
|
#
|
|
1029
|
-
#
|
|
1141
|
+
# assertions.validate_presence([1, 2, 3])
|
|
1030
1142
|
# #=> does not raise an exception
|
|
1031
1143
|
def validate_presence(value, as: 'value', message: nil, optional: false)
|
|
1032
1144
|
assert_presence(
|