Class: SleepingKingStudios::Tools::Assertions

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

Overview

Methods for asserting on the type or content of values

Direct Known Subclasses

Aggregator

Defined Under Namespace

Classes: Aggregator, AssertionError, MessagesStrategy

Instance Method Summary collapse

Methods inherited from Base

#initialize, instance, #toolbelt

Constructor Details

This class inherits a constructor from SleepingKingStudios::Tools::Base

Instance Method Details

#aggregator_classClass

Returns the class used to aggregate grouped assertion failures.

Returns:

  • (Class)

    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.

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:



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

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



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?

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



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)

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



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)

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

Examples:

Assertions.assert_exclusion('get', expected: %w[get post put])
#=> raises an AssertionError with message 'value is is one of "get", "post", "put"'

Assertions.assert_exclusion('delete', expected: %w[get post put])
#=> 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 (Enumerable)

    the expected values.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:



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:)

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

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:



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?

  message ||= aggregator.failure_message

  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.

Examples:

Assertions.assert_inclusion('get', expected: %w[get post put])
#=> does not raise an exception

Assertions.assert_inclusion('delete', expected: %w[get post put])
#=> raises an AssertionError with message 'value is is one of "get", "post", "put"'

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 (Enumerable)

    the expected values.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

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

    if false, ignores nil values even if nil is not one of the given values.

Raises:

  • (AssertionError)

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

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

Examples:

Assertions.assert_inherits_from(:foo, expected: StandardError)
#=> raises an AssertionError with message 'value is not a Class or Module'

Assertions.assert_inherits_from(RuntimeError, expected: StandardError)
#=> does not raise an exception

Assertions.assert_inherits_from(Array, expected: Enumerable)
#=> 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, Module)

    the expected ancestor.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

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

    if true, fails if the given and expected values are the same class or module.

Raises:

  • (AssertionError)

    if the value is not a class or module or does not have 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)
    message ||= error_message_for('class_or_module', as:)

    return handle_error(error_class:, message:)
  end

  return if strict ? value < expected : value <= expected

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

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, Module)

    the expected class or module.

  • 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.



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)

  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.



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

  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.



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

    message ||= error_message_for('presence', as:)

    return handle_error(error_class:, message:)
  end

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

    return handle_error(error_class:, message:)
  end

  return unless value.empty?

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



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?

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



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?

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



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

    message ||= error_message_for('presence', as:)

    handle_error(error_class:, message:)
  end

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

  message ||= error_message_for('presence', as:)

  handle_error(error_class:, message:)
end

#error_message_for(key, as: 'value', **parameters) ⇒ String

Generates an error message for a failed validation.

Examples:

key = 'sleeping_king_studios.tools.assertions.blank'

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

Parameters:

  • key (String)

    the message key.

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

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

  • parameters (Hash)

    additional options for generating the message.

Options Hash (**parameters):

  • expected (Object)

    the expected object, if any.

Returns:

  • (String)

    the generated error message.



578
579
580
581
582
583
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 578

def error_message_for(key, as: 'value', **parameters)
  key   = key.to_s
  scope = key.include?('.') ? nil : 'sleeping_king_studios.tools.assertions'

  toolbelt.messages.message(key, as:, parameters:, scope:)
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.



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.

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.



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.

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.



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.

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.



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.

Examples:

Assertions.validate_exclusion('get', expected: %w[get post put])
#=> raises an ArgumentError with message 'value is is one of "get", "post", "put"'

Assertions.validate_exclusion('delete', expected: %w[get post put])
#=> 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 (Enumerable)

    the expected values.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

Raises:

  • (ArgumentError)

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

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.



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.

Examples:

Assertions.validate_inclusion('get', expected: %w[get post put])
#=> does not raise an exception

Assertions.validate_inclusion('delete', expected: %w[get post put])
#=> raises an ArgumentError with message 'value is is one of "get", "post", "put"'

Parameters:

  • value (Object)

    the value to assert on.

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

    the name of the asserted value.

  • expected (Enumerable)

    the expected values.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

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

    if false, ignores nil values even if nil is not one of the given values.

Raises:

  • (ArgumentError)

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

Examples:

Assertions.validate_inherits_from(:foo, expected: StandardError)
#=> raises an ArgumentError with message 'value is not a Class or Module'

Assertions.validate_inherits_from(RuntimeError, expected: StandardError)
#=> does not raise an exception

Assertions.validate_inherits_from(Array, expected: Enumerable)
#=> 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, Module)

    the expected ancestor.

  • message (String) (defaults to: nil)

    the exception message to raise on a failure.

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

    if true, fails if the given and expected values are the same class or module.

Raises:

  • (ArgumentError)

    if the value is not a class or module or does not have 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.

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 or module.

  • 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 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 #===.

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.



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.

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.



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.

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.



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.

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.



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.

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.



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