Tools

A library of utility services and concerns.

Class: SleepingKingStudios::Tools::Assertions::Aggregator

Parent Namespace
SleepingKingStudios::Tools::Assertions
Inherited Classes
SleepingKingStudios::Tools::Assertions > SleepingKingStudios::Tools::Base > Object
Extended Modules
Forwardable
Defined In
lib/sleeping_king_studios/tools/assertions.rb

Table Of Contents

Overview

Utility for grouping multiple assertion statements.

Examples

rocket = Struct.new(:fuel, :launched).new(0.0, true)
aggregator = SleepingKingStudios::Tools::Assertions::Aggregator.new
aggregator.empty?
#=> true

aggregator.assert(message: 'is out of fuel') { rocket.fuel > 0 }
aggregator.assert(message: 'has already launched') { !rocket.launched }
aggregator.empty?
#=> false
aggregator.failure_message
#=> 'is out of fuel, has already launched'

Back To Top

Class Methods

.instance => SleepingKingStudios::Tools::Base

Inherited From
SleepingKingStudios::Tools::Base

Returns

Back To Top

Constructor

#initialize => Aggregator

Returns

Back To Top

Instance Methods

#<<(message) => Array

Appends the message to the failure messages.

Parameters

Returns

See Also

#aggregator_class => Class

Inherited From
SleepingKingStudios::Tools::Assertions

Returns

#assert(error_class: AssertionError, message: nil, &block) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Yields

Yield Returns

Returns

Raises

#assert_blank(value, as: 'value', error_class: AssertionError, message: nil) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_boolean(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_class(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_group(error_class: AssertionError, message: nil, &assertions) => void

Also known as: aggregate

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

Yields

Yield Parameters

Returns

Raises

#assert_instance_of(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_matches(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_name(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_nil(value, as: 'value', error_class: AssertionError, message: nil) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_not_nil(value, as: 'value', error_class: AssertionError, message: nil) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#assert_presence(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#clear => Array

Removes all items from the failure messages.

Returns

See Also

#count => Integer

Returns a count of the failure message.

Returns

See Also

#each => Enumerator
#each(&block) => Object

Iterates over the failure messages.

Overloads

#each => Enumerator

Returns an enumerator that iterates over the failure messages.

Returns
  • (Enumerator) — an enumerator over the messages.
See Also
  • Enumerable#each
#each(&block) => Object

Yields each failure message to the block.

Yield Parameters
  • message (String) — the current failure message.
See Also
  • Enumerable#each

#empty? => true, false

Checks if there are any failure messages.

Returns

See Also

#error_message_for(scope, as: 'value', **options) => String

Inherited From
SleepingKingStudios::Tools::Assertions

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

Options Hash (options)

Returns

#failure_message => String

Generates a combined failure message from the configured messages.

Examples

With an empty aggregator.

aggregator = SleepingKingStudios::Tools::Assertions::Aggregator.new

aggregator.failure_message
#=> ''

With an aggregator with failure messages.

aggregator = SleepingKingStudios::Tools::Assertions::Aggregator.new
aggrgator << 'rocket is out of fuel'
aggrgator << 'rocket is not pointed toward space'

aggregator.failure_message
#=> 'rocket is out of fuel, rocket is not pointed toward space'

Returns

#size => Integer

Returns a count of the failure message.

Returns

See Also

#validate(message: nil, &block) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Yields

Yield Returns

Returns

Raises

#validate_blank(value, as: 'value', message: nil) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_boolean(value, as: 'value', message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_class(value, as: 'value', message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_group(message: nil, &validations) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Yields

Yield Parameters

Returns

Raises

#validate_instance_of(value, expected:, as: 'value', message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_matches(value, expected:, as: 'value', message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_name(value, as: 'value', message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_nil(value, as: 'value', message: nil) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_not_nil(value, as: 'value', message: nil) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

#validate_presence(value, as: 'value', message: nil, optional: false) => void

Inherited From
SleepingKingStudios::Tools::Assertions

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

Returns

Raises

Back To Top


Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools | SleepingKingStudios::Tools::Assertions