A library of utility services and concerns.
Methods for asserting on the state of a function or application.
SleepingKingStudios::Tools::Assertions::Aggregator
.instance => SleepingKingStudios::Tools::Base
#aggregator_class => Class
#assert(error_class: AssertionError, message: nil, &block) => void
Asserts that the block returns a truthy value.
Assertions.assert { true == false }
#=> raises an AssertionError with message 'block returned a falsy value'
Assertions.assert { true == true }
#=> does not raise an exception
#assert_blank(value, as: 'value', error_class: AssertionError, message: nil) => void
Asserts that the value is either nil or empty.
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'
#assert_boolean(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void
Asserts that the value is either true or false.
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
#assert_class(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void
Asserts that the value is a Class.
Assertions.assert_class(Object.new)
#=> raises an AssertionError with message 'value is not a class'
Assertions.assert_class(String)
#=> does not raise an exception
#assert_group(error_class: AssertionError, message: nil, &assertions) => void
Also known as:
aggregate
Evaluates a series of assertions and combines all failures.
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"
#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.
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
#assert_matches(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) => void
Asserts that the value matches the expected object using #===.
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
#assert_name(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void
Asserts that the value is a non-empty String or Symbol.
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
#assert_nil(value, as: 'value', error_class: AssertionError, message: nil) => void
Asserts that the value is nil.
Assertions.assert_nil(nil)
#=> does not raise an exception
Assertions.assert_nil(Object.new)
#=> raises an AssertionError with message 'value must be nil'
#assert_not_nil(value, as: 'value', error_class: AssertionError, message: nil) => void
Asserts that the value is not nil.
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
#assert_presence(value, as: 'value', error_class: AssertionError, message: nil, optional: false) => void
Asserts that the value is not nil and not empty.
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
#error_message_for(scope, as: 'value', **options) => String
Generates an error message for a failed validation.
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'
#validate(message: nil, &block) => void
Asserts that the block returns a truthy value.
Assertions.validate { true == false }
#=> raises an ArgumentError with message 'block returned a falsy value'
Assertions.validate { true == true }
#=> does not raise an exception
#validate_blank(value, as: 'value', message: nil) => void
Asserts that the value is either nil or empty.
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'
#validate_boolean(value, as: 'value', message: nil, optional: false) => void
Asserts that the value is either true or false.
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
#validate_class(value, as: 'value', message: nil, optional: false) => void
Asserts that the value is a Class.
Assertions.validate_class(Object.new)
#=> raises an ArgumentError with message 'value is not a class'
Assertions.validate_class(String)
#=> does not raise an exception
#validate_group(message: nil, &validations) => void
Evaluates a series of validations and combines all failures.
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"
#validate_instance_of(value, expected:, as: 'value', message: nil, optional: false) => void
Asserts that the value is an example of the given Class.
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
#validate_matches(value, expected:, as: 'value', message: nil, optional: false) => void
Asserts that the value matches the expected object using #===.
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
#validate_name(value, as: 'value', message: nil, optional: false) => void
Asserts that the value is a non-empty String or Symbol.
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
#validate_nil(value, as: 'value', message: nil) => void
Asserts that the value is nil.
Assertions.validate_nil(nil)
#=> does not raise an exception
Assertions.validate_nil(Object.new)
#=> raises an ArgumentError with message 'value must be nil'
#validate_not_nil(value, as: 'value', message: nil) => void
Asserts that the value is not nil.
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
#validate_presence(value, as: 'value', message: nil, optional: false) => void
Asserts that the value is not nil and not empty.
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
Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools