Class: SleepingKingStudios::Tools::Assertions
- Defined in:
- lib/sleeping_king_studios/tools/assertions.rb
Overview
Methods for asserting on the state of a function or application.
Direct Known Subclasses
Defined Under Namespace
Classes: Aggregator, AssertionError
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_group(error_class: AssertionError, message: nil) {|aggregator| ... } ⇒ void
(also: #aggregate)
Evaluates a series of assertions and combines all failures.
-
#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.
-
#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(scope, as: 'value', **options) ⇒ 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_group(message: nil) {|aggregator| ... } ⇒ void
Evaluates a series of validations and combines all failures.
-
#validate_instance_of(value, expected:, as: 'value', message: nil, optional: false) ⇒ void
Asserts that the value is an example of the given Class.
-
#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
Instance Method Details
#aggregator_class ⇒ Class
Returns the class used to aggregate grouped assertion failures.
177 178 179 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 177 def aggregator_class Aggregator end |
#assert(error_class: AssertionError, message: nil) { ... } ⇒ void
This method returns an undefined value.
Asserts that the block returns a truthy value.
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 199 def assert(error_class: AssertionError, message: nil, &block) return if block.call ||= ( 'sleeping_king_studios.tools.assertions.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.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 234 def assert_blank( value, as: 'value', error_class: AssertionError, message: nil ) return if value.nil? return if value.respond_to?(:empty?) && value.empty? ||= ( 'sleeping_king_studios.tools.assertions.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.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 275 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) ||= ( 'sleeping_king_studios.tools.assertions.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.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 312 def assert_class( value, as: 'value', error_class: AssertionError, message: nil, optional: false ) return if optional && value.nil? return if value.is_a?(Class) ||= ( 'sleeping_king_studios.tools.assertions.class', as: ) 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.
349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 349 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_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.
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 385 def assert_instance_of( # rubocop:disable Metrics/ParameterLists value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false ) unless expected.is_a?(Class) raise ArgumentError, 'expected must be a Class' 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 #===.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 424 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.
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 468 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 ||= ( 'sleeping_king_studios.tools.assertions.presence', as: ) return handle_error(error_class:, message:) end unless value.is_a?(String) || value.is_a?(Symbol) ||= ( 'sleeping_king_studios.tools.assertions.name', as: ) return handle_error(error_class:, message:) end return unless value.empty? ||= ( 'sleeping_king_studios.tools.assertions.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.
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 522 def assert_nil( value, as: 'value', error_class: AssertionError, message: nil ) return if value.nil? ||= ( 'sleeping_king_studios.tools.assertions.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.
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 555 def assert_not_nil( value, as: 'value', error_class: AssertionError, message: nil ) return unless value.nil? ||= ( 'sleeping_king_studios.tools.assertions.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.
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 596 def assert_presence( # rubocop:disable Metrics/MethodLength value, as: 'value', error_class: AssertionError, message: nil, optional: false ) if value.nil? return if optional ||= ( 'sleeping_king_studios.tools.assertions.presence', as: ) handle_error(error_class:, message:) end return unless value.respond_to?(:empty?) && value.empty? ||= ( 'sleeping_king_studios.tools.assertions.presence', as: ) handle_error(error_class:, message:) end |
#error_message_for(scope, as: 'value', **options) ⇒ String
Generates an error message for a failed validation.
644 645 646 647 648 649 650 651 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 644 def (scope, as: 'value', **) = ERROR_MESSAGES .fetch(scope.to_s) { return "Error message missing: #{scope}" } .then { |raw| format(raw, **) } (as:, message:) end |
#validate(message: nil) { ... } ⇒ void
This method returns an undefined value.
Asserts that the block returns a truthy value.
670 671 672 673 674 675 676 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 670 def validate(message: nil, &block) assert( error_class: ArgumentError, message:, &block ) end |
#validate_blank(value, as: 'value', message: nil) ⇒ void
This method returns an undefined value.
Asserts that the value is either nil or empty.
701 702 703 704 705 706 707 708 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 701 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.
733 734 735 736 737 738 739 740 741 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 733 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.
760 761 762 763 764 765 766 767 768 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 760 def validate_class(value, as: 'value', message: nil, optional: false) assert_class( value, as:, error_class: ArgumentError, message:, optional: ) end |
#validate_group(message: nil) {|aggregator| ... } ⇒ void
This method returns an undefined value.
Evaluates a series of validations and combines all failures.
787 788 789 790 791 792 793 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 787 def validate_group(message: nil, &validations) assert_group( error_class: ArgumentError, message:, &validations ) 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.
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 814 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 #===.
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 849 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.
893 894 895 896 897 898 899 900 901 902 903 904 905 906 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 893 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.
924 925 926 927 928 929 930 931 932 933 934 935 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 924 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.
953 954 955 956 957 958 959 960 961 962 963 964 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 953 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.
990 991 992 993 994 995 996 997 998 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 990 def validate_presence(value, as: 'value', message: nil, optional: false) assert_presence( value, as:, error_class: ArgumentError, message:, optional: ) end |