Class: SleepingKingStudios::Tools::Assertions

Inherits:
Base
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/assertions.rb

Overview

Methods for asserting on the state of a function or application.

Direct Known Subclasses

Aggregator

Defined Under Namespace

Classes: Aggregator, AssertionError

Instance Method Summary collapse

Methods inherited from Base

instance

Instance Method Details

#aggregator_classClass

Returns the class used to aggregate grouped assertion failures.

Returns:

  • (Class)

    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.

Examples:

Assertions.assert { true == false }
#=> raises an AssertionError with message 'block returned a falsy value'

Assertions.assert { true == true }
#=> does not raise an exception

Parameters:

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Yields:

  • the block to evaluate.

Yield Returns:

  • (Object)

    the returned value of the block.

Raises:



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

  message ||= error_message_for(
    '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.

Examples:

Assertions.assert_blank(nil)
#=> does not raise an exception

Assertions.assert_blank(Object.new)
#=> raises an AssertionError with message 'value must be nil or empty'

Assertions.assert_blank([])
#=> does not raise an exception

Assertions.assert_blank([1, 2, 3])
#=> raises an AssertionError with message 'value must be nil or empty'

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:

  • (AssertionError)

    if the value is not nil and either does not respond to #empty? or value.empty returns false.



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?

  message ||= error_message_for(
    '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.

Examples:

Assertions.assert_boolean(nil)
#=> raises an AssertionError with message 'value must be true or false'

Assertions.assert_boolean(Object.new)
#=> raises an AssertionError with message 'value must be true or false'

Assertions.assert_boolean(false)
#=> does not raise an exception

Assertions.assert_boolean(true)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:



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)

  message ||= error_message_for(
    '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.

Examples:

Assertions.assert_class(Object.new)
#=> raises an AssertionError with message 'value is not a class'

Assertions.assert_class(String)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:



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)

  message ||= error_message_for(
    '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.

Examples:

Assertions.assert_group do |group|
  group.assert_name(nil, as: 'label')
  group.assert_instance_of(0.0, expected: Integer, as: 'quantity')
end
# raises an AssertionError with message: "label can't be blank, quantity is not an instance of Integer"

Parameters:

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Yields:

  • the assertions to evaluate.

Yield Parameters:

  • aggregator (Aggregator)

    the aggregator object.

Raises:



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?

  message ||= aggregator.failure_message

  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.

Examples:

Assertions.assert_instance_of(:foo, expected: String)
#=> raises an AssertionError with message 'value is not an instance of String'

Assertions.assert_instance_of('foo', expected: String)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • expected (Class)

    the expected class.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (ArgumentError)

    if the expected class is not a Class.

  • (AssertionError)

    if the value is not an instance of the expected 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)

  message ||= error_message_for_instance_of(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 #===.

Examples:

Assertions.assert_matches('bar', expected: /foo/)
#=> raises an AssertionError with message 'value does not match the pattern /foo/'

Assertions.assert_matches('foo', expected: /foo/)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • expected (#===)

    the expected object.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (AssertionError)

    if the value does not match the expected object.



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

  message ||= error_message_for_matches(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.

Examples:

Assertions.assert_name(nil)
#=> raises an AssertionError with message "value can't be blank"

Assertions.assert_name(Object.new)
#=> raises an AssertionError with message 'value is not a String or a Symbol'

Assertions.assert_name('')
#=> raises an AssertionError with message "value can't be blank"

Assertions.assert_name('foo')
#=> does not raise an exception

Assertions.assert_name(:bar)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (AssertionError)

    if the value is not a String or a Symbol, or if the value is empty.



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

    message ||= error_message_for(
      'sleeping_king_studios.tools.assertions.presence',
      as:
    )

    return handle_error(error_class:, message:)
  end

  unless value.is_a?(String) || value.is_a?(Symbol)
    message ||= error_message_for(
      'sleeping_king_studios.tools.assertions.name',
      as:
    )

    return handle_error(error_class:, message:)
  end

  return unless value.empty?

  message ||= error_message_for(
    '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.

Examples:

Assertions.assert_nil(nil)
#=> does not raise an exception

Assertions.assert_nil(Object.new)
#=> raises an AssertionError with message 'value must be nil'

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:



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?

  message ||= error_message_for(
    '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.

Examples:

Assertions.assert_not_nil(nil)
#=> raises an AssertionError with message 'value must not be nil'

Assertions.assert_not_nil(Object.new)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:



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?

  message ||= error_message_for(
    '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.

Examples:

Assertions.assert_presence(nil)
#=> raises an AssertionError with message "can't be blank"

Assertions.assert_presence(Object.new)
#=> does not raise an exception

Assertions.assert_presence([])
#=> raises an AssertionError with message "can't be blank"

Assertions.assert_presence([1, 2, 3])
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    the exception class to raise on a failure.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (AssertionError)

    if the value is nil, or if the value responds to #empty? and value.empty is true.



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

    message ||= error_message_for(
      'sleeping_king_studios.tools.assertions.presence',
      as:
    )

    handle_error(error_class:, message:)
  end

  return unless value.respond_to?(:empty?) && value.empty?

  message ||= error_message_for(
    '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.

Examples:

scope = 'sleeping_king_studios.tools.assertions.blank'

assertions.error_message_for(scope)
#=> 'value must be nil or empty'
assertions.error_message_for(scope, as: false)
#=> 'must be nil or empty'
assertions.error_message_for(scope, as: 'item')
#=> 'item must be nil or empty'

Parameters:

  • scope (String)

    the message scope.

  • options (Hash)

    additional options for generating the message.

Options Hash (**options):

  • as (String)

    the name of the validated property. Defaults to ‘value’.

  • expected (Object)

    the expected object, if any.

Returns:

  • (String)

    the generated error message.



644
645
646
647
648
649
650
651
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 644

def error_message_for(scope, as: 'value', **options)
  message =
    ERROR_MESSAGES
    .fetch(scope.to_s) { return "Error message missing: #{scope}" }
    .then { |raw| format(raw, **options) }

  join_error_message(as:, message:)
end

#validate(message: nil) { ... } ⇒ void

This method returns an undefined value.

Asserts that the block returns a truthy value.

Examples:

Assertions.validate { true == false }
#=> raises an ArgumentError with message 'block returned a falsy value'

Assertions.validate { true == true }
#=> does not raise an exception

Parameters:

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Yields:

  • The block to evaluate.

Yield Returns:

  • (Object)

    the returned value of the block.

Raises:

  • (ArgumentError)

    if the block does not return 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.

Examples:

Assertions.validate_blank(nil)
#=> does not raise an exception

Assertions.validate_blank(Object.new)
#=> raises an ArgumentError with message 'value must be nil or empty'

Assertions.validate_blank([])
#=> does not raise an exception

Assertions.validate_blank([1, 2, 3])
#=> raises an ArgumentError with message 'value must be nil or empty'

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:

  • (ArgumentError)

    if the value is not nil and either does not respond to #empty? or value.empty returns false.



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.

Examples:

Assertions.validate_boolean(nil)
#=> raises an ArgumentError with message 'value must be true or false'

Assertions.validate_boolean(Object.new)
#=> raises an ArgumentError with message 'value must be true or false'

Assertions.validate_boolean(false)
#=> does not raise an exception

Assertions.validate_boolean(true)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (ArgumentError)

    if the value is not 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.

Examples:

Assertions.validate_class(Object.new)
#=> raises an ArgumentError with message 'value is not a class'

Assertions.validate_class(String)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (ArgumentError)

    if the value is not 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.

Examples:

Assertions.validate_group do |group|
  group.validate_name(nil, as: 'label')
  group.validate_instance_of(0.0, expected: Integer, as: 'quantity')
end
# raises an ArgumentError with message: "label can't be blank, quantity is not an instance of Integer"

Parameters:

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Yields:

  • the validations to evaluate.

Yield Parameters:

  • aggregator (Aggregator)

    the aggregator object.

Raises:

  • (ArgumentError)

    if any of the validations fail.



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.

Examples:

Assertions.validate_instance_of(:foo, expected: String)
#=> raises an AssertionError with message 'value is not an instance of String'

Assertions.validate_instance_of('foo', expected: String)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • expected (Class)

    the expected class.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (ArgumentError)

    if the value is not an instance of the expected 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 #===.

Examples:

Assertions.validate_matches('bar', expected: /foo/)
#=> raises an ArgumentError with message 'value does not match the pattern /foo/'

Assertions.validate_matches('foo', expected: /foo/)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • expected (#===)

    the expected object.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (ArgumentError)

    if the value does not match the expected object.



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.

Examples:

Assertions.validate_name(nil)
#=> raises an ArgumentError with message "value can't be blank"

Assertions.validate_name(Object.new)
#=> raises an AssertionError with message 'value is not a String or a Symbol'

Assertions.validate_name('')
#=> raises an ArgumentError with message "value can't be blank"

Assertions.validate_name('foo')
#=> does not raise an exception

Assertions.validate_name(:bar)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (ArgumentError)

    if the value is not a String or a Symbol, or if the value is empty.



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.

Examples:

Assertions.validate_nil(nil)
#=> does not raise an exception

Assertions.validate_nil(Object.new)
#=> raises an ArgumentError with message 'value must be nil'

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:

  • (ArgumentError)

    if the value is not 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.

Examples:

Assertions.validate_not_nil(nil)
#=> raises an ArgumentError with message 'value must not be nil'

Assertions.validate_not_nil(Object.new)
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:

  • (ArgumentError)

    if the value is 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.

Examples:

Assertions.validate_presence(nil)
#=> raises an ArgumentError with message "can't be blank"

Assertions.validate_presence(Object.new)
#=> does not raise an exception

Assertions.validate_presence([])
#=> raises an ArgumentError with message "can't be blank"

Assertions.validate_presence([1, 2, 3])
#=> does not raise an exception

Parameters:

  • value (Object)

    the value to assert on.

  • as (String) (defaults to: 'value')

    the name of the asserted value.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    if true, allows nil values.

Raises:

  • (ArgumentError)

    if the value is nil, or if the value responds to #empty? and value.empty is true.



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