Class: SleepingKingStudios::Tools::Assertions
- Defined in:
- lib/sleeping_king_studios/tools/assertions.rb
Overview
Methods for asserting on the type or content of values
Direct Known Subclasses
Defined Under Namespace
Classes: Aggregator, AssertionError, MessagesStrategy
Instance Method Summary collapse
-
#aggregator_class ⇒ Class
The class used to aggregate grouped assertion failures.
-
#assert(error_class: AssertionError, message: nil) { ... } ⇒ void
Asserts that the block returns a truthy value.
-
#assert_blank(value, as: 'value', error_class: AssertionError, message: nil) ⇒ void
Asserts that the value is either nil or empty.
-
#assert_boolean(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
Asserts that the value is either true or false.
-
#assert_class(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
Asserts that the value is a Class.
-
#assert_exclusion(value, expected:, as: 'value', error_class: AssertionError, message: nil) ⇒ void
Asserts that the value is not one of the given values.
-
#assert_group(error_class: AssertionError, message: nil) {|aggregator| ... } ⇒ void
(also: #aggregate)
Evaluates a series of assertions and combines all failures.
-
#assert_inclusion(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
Asserts that the value is one of the given values.
-
#assert_inherits_from(value, expected:, as: 'value', error_class: AssertionError, message: nil, strict: false) ⇒ void
(also: #assert_subclass)
Asserts that the value is a class or module with the given ancestor.
-
#assert_instance_of(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
Asserts that the value is an example of the given Class or Module.
-
#assert_matches(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
Asserts that the value matches the expected object using #===.
-
#assert_name(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
Asserts that the value is a non-empty String or Symbol.
-
#assert_nil(value, as: 'value', error_class: AssertionError, message: nil) ⇒ void
Asserts that the value is nil.
-
#assert_not_nil(value, as: 'value', error_class: AssertionError, message: nil) ⇒ void
Asserts that the value is not nil.
-
#assert_presence(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
Asserts that the value is not nil and not empty.
-
#error_message_for(key, as: 'value', **parameters) ⇒ String
Generates an error message for a failed validation.
-
#validate(message: nil) { ... } ⇒ void
Asserts that the block returns a truthy value.
-
#validate_blank(value, as: 'value', message: nil) ⇒ void
Asserts that the value is either nil or empty.
-
#validate_boolean(value, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value is either true or false.
-
#validate_class(value, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value is a Class.
-
#validate_exclusion(value, expected:, as: 'value', message: nil) ⇒ void
Asserts that the value is not one of the given values.
-
#validate_group(message: nil) {|aggregator| ... } ⇒ void
Evaluates a series of validations and combines all failures.
-
#validate_inclusion(value, expected:, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value is one of the given values.
-
#validate_inherits_from(value, expected:, as: 'value', message: nil, strict: false) ⇒ void
(also: #validate_subclass)
Asserts that the value is a class or module with the given ancestor.
-
#validate_instance_of(value, expected:, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value is an example of the given Class or Module.
-
#validate_matches(value, expected:, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value matches the expected object using #===.
-
#validate_name(value, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value is a non-empty String or Symbol.
-
#validate_nil(value, as: 'value', message: nil) ⇒ void
Asserts that the value is nil.
-
#validate_not_nil(value, as: 'value', message: nil) ⇒ void
Asserts that the value is not nil.
-
#validate_presence(value, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value is not nil and not empty.
Methods inherited from Base
#initialize, instance, #toolbelt
Constructor Details
This class inherits a constructor from SleepingKingStudios::Tools::Base
Instance Method Details
#aggregator_class ⇒ Class
Returns the class used to aggregate grouped assertion failures.
19 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 19 def aggregator_class = Aggregator |
#assert(error_class: AssertionError, message: nil) { ... } ⇒ void
This method returns an undefined value.
Asserts that the block returns a truthy value.
39 40 41 42 43 44 45 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 39 def assert(error_class: AssertionError, message: nil, &block) return if block.call ||= ('block', as: false) handle_error(error_class:, message:) end |
#assert_blank(value, as: 'value', error_class: AssertionError, message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is either nil or empty.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 71 def assert_blank( value, as: 'value', error_class: AssertionError, message: nil ) return if value.nil? return if value.respond_to?(:empty?) && value.empty? ||= ('blank', as:) handle_error(error_class:, message:) end |
#assert_boolean(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is either true or false.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 109 def assert_boolean( value, as: 'value', error_class: AssertionError, message: nil, optional: false ) return if optional && value.nil? return if value.equal?(true) || value.equal?(false) ||= ('boolean', as:) handle_error(error_class:, message:) end |
#assert_class(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is a Class.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 143 def assert_class( value, as: 'value', error_class: AssertionError, message: nil, optional: false ) return if optional && value.nil? return if value.is_a?(Class) ||= ('class', as:) handle_error(error_class:, message:) end |
#assert_exclusion(value, expected:, as: 'value', error_class: AssertionError, message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is not one of the given values.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 177 def assert_exclusion( value, expected:, as: 'value', error_class: AssertionError, message: nil ) unless expected.is_a?(Enumerable) raise ArgumentError, 'expected must be Enumerable' end return unless value_in_expected?(expected:, value:) ||= (as:, expected:) handle_error(error_class:, message:) end |
#assert_group(error_class: AssertionError, message: nil) {|aggregator| ... } ⇒ void Also known as: aggregate
This method returns an undefined value.
Evaluates a series of assertions and combines all failures.
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 213 def assert_group(error_class: AssertionError, message: nil, &assertions) raise ArgumentError, 'no block given' unless block_given? aggregator = aggregator_class.new assertions.call(aggregator) return if aggregator.empty? ||= aggregator. handle_error(error_class:, message:) end |
#assert_inclusion(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is one of the given values.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 248 def assert_inclusion( # rubocop:disable Metrics/ParameterLists value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false ) unless expected.is_a?(Enumerable) raise ArgumentError, 'expected must be Enumerable' end return if optional && value.nil? return if value_in_expected?(expected:, value:) ||= (as:, expected:) handle_error(error_class:, message:) end |
#assert_inherits_from(value, expected:, as: 'value', error_class: AssertionError, message: nil, strict: false) ⇒ void Also known as: assert_subclass
This method returns an undefined value.
Asserts that the value is a class or module with the given ancestor.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 293 def assert_inherits_from( # rubocop:disable Metrics/ParameterLists value, expected:, as: 'value', error_class: AssertionError, message: nil, strict: false ) unless expected.is_a?(Module) raise ArgumentError, 'expected must be a Class or Module' end unless value.is_a?(Module) ||= ('class_or_module', as:) return handle_error(error_class:, message:) end return if strict ? value < expected : value <= expected ||= ('inherit_from', as:, expected:) handle_error(error_class:, message:) end |
#assert_instance_of(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is an example of the given Class or Module.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 340 def assert_instance_of( # rubocop:disable Metrics/ParameterLists value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false ) unless expected.is_a?(Module) raise ArgumentError, 'expected must be a Class or Module' end return if optional && value.nil? return if value.is_a?(expected) ||= (as:, expected:) handle_error(error_class:, message:) end |
#assert_matches(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value matches the expected object using #===.
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 379 def assert_matches( # rubocop:disable Metrics/ParameterLists value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false ) return if optional && value.nil? return if expected === value # rubocop:disable Style/CaseEquality ||= (as:, expected:) handle_error(error_class:, message:) end |
#assert_name(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is a non-empty String or Symbol.
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 423 def assert_name( # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity value, as: 'value', error_class: AssertionError, message: nil, optional: false ) if value.nil? return if optional ||= ('presence', as:) return handle_error(error_class:, message:) end unless value.is_a?(String) || value.is_a?(Symbol) ||= ('name', as:) return handle_error(error_class:, message:) end return unless value.empty? ||= ('presence', as:) handle_error(error_class:, message:) end |
#assert_nil(value, as: 'value', error_class: AssertionError, message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is nil.
468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 468 def assert_nil( value, as: 'value', error_class: AssertionError, message: nil ) return if value.nil? ||= ('nil', as:) handle_error(error_class:, message:) end |
#assert_not_nil(value, as: 'value', error_class: AssertionError, message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is not nil.
498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 498 def assert_not_nil( value, as: 'value', error_class: AssertionError, message: nil ) return unless value.nil? ||= ('not_nil', as:) handle_error(error_class:, message:) end |
#assert_presence(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is not nil and not empty.
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 536 def assert_presence( value, as: 'value', error_class: AssertionError, message: nil, optional: false ) if value.nil? return if optional ||= ('presence', as:) handle_error(error_class:, message:) end return unless value.respond_to?(:empty?) && value.empty? ||= ('presence', as:) handle_error(error_class:, message:) end |
#error_message_for(key, as: 'value', **parameters) ⇒ String
Generates an error message for a failed validation.
578 579 580 581 582 583 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 578 def (key, as: 'value', **parameters) key = key.to_s scope = key.include?('.') ? nil : 'sleeping_king_studios.tools.assertions' toolbelt..(key, as:, parameters:, scope:) end |
#validate(message: nil) { ... } ⇒ void
This method returns an undefined value.
Asserts that the block returns a truthy value.
602 603 604 605 606 607 608 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 602 def validate(message: nil, &) assert( error_class: ArgumentError, message:, & ) end |
#validate_blank(value, as: 'value', message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is either nil or empty.
633 634 635 636 637 638 639 640 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 633 def validate_blank(value, as: 'value', message: nil) assert_blank( value, as:, error_class: ArgumentError, message: ) end |
#validate_boolean(value, as: 'value', message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is either true or false.
665 666 667 668 669 670 671 672 673 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 665 def validate_boolean(value, as: 'value', message: nil, optional: false) assert_boolean( value, as:, error_class: ArgumentError, message:, optional: ) end |
#validate_class(value, as: 'value', message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is a Class.
692 693 694 695 696 697 698 699 700 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 692 def validate_class(value, as: 'value', message: nil, optional: false) assert_class( value, as:, error_class: ArgumentError, message:, optional: ) end |
#validate_exclusion(value, expected:, as: 'value', message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is not one of the given values.
719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 719 def validate_exclusion( value, expected:, as: 'value', message: nil ) assert_exclusion( value, as:, error_class: ArgumentError, expected:, message: ) end |
#validate_group(message: nil) {|aggregator| ... } ⇒ void
This method returns an undefined value.
Evaluates a series of validations and combines all failures.
751 752 753 754 755 756 757 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 751 def validate_group(message: nil, &validations) assert_group( error_class: ArgumentError, message:, &validations ) end |
#validate_inclusion(value, expected:, as: 'value', message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is one of the given values.
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 778 def validate_inclusion( value, expected:, as: 'value', message: nil, optional: false ) assert_inclusion( value, as:, error_class: ArgumentError, expected:, message:, optional: ) end |
#validate_inherits_from(value, expected:, as: 'value', message: nil, strict: false) ⇒ void Also known as: validate_subclass
This method returns an undefined value.
Asserts that the value is a class or module with the given ancestor.
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 818 def validate_inherits_from( value, expected:, as: 'value', message: nil, strict: false ) assert_inherits_from( value, as:, error_class: ArgumentError, expected:, message:, strict: ) end |
#validate_instance_of(value, expected:, as: 'value', message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is an example of the given Class or Module.
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 855 def validate_instance_of( value, expected:, as: 'value', message: nil, optional: false ) assert_instance_of( value, as:, error_class: ArgumentError, expected:, message:, optional: ) end |
#validate_matches(value, expected:, as: 'value', message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value matches the expected object using #===.
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 890 def validate_matches( value, expected:, as: 'value', message: nil, optional: false ) assert_matches( value, as:, error_class: ArgumentError, expected:, message:, optional: ) end |
#validate_name(value, as: 'value', message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is a non-empty String or Symbol.
934 935 936 937 938 939 940 941 942 943 944 945 946 947 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 934 def validate_name( value, as: 'value', message: nil, optional: false ) assert_name( value, as:, error_class: ArgumentError, message:, optional: ) end |
#validate_nil(value, as: 'value', message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is nil.
965 966 967 968 969 970 971 972 973 974 975 976 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 965 def validate_nil( value, as: 'value', message: nil ) assert_nil( value, as:, error_class: ArgumentError, message: ) end |
#validate_not_nil(value, as: 'value', message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is not nil.
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 994 def validate_not_nil( value, as: 'value', message: nil ) assert_not_nil( value, as:, error_class: ArgumentError, message: ) end |
#validate_presence(value, as: 'value', message: nil, optional: false) ⇒ void
This method returns an undefined value.
Asserts that the value is not nil and not empty.
1031 1032 1033 1034 1035 1036 1037 1038 1039 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 1031 def validate_presence(value, as: 'value', message: nil, optional: false) assert_presence( value, as:, error_class: ArgumentError, message:, optional: ) end |