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

Inherits:
SleepingKingStudios::Tools::Assertions show all
Extended by:
Forwardable
Defined in:
lib/sleeping_king_studios/tools/assertions.rb

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'

Instance Method Summary collapse

Methods inherited from SleepingKingStudios::Tools::Assertions

#aggregator_class, #assert, #assert_blank, #assert_boolean, #assert_class, #assert_instance_of, #assert_matches, #assert_name, #assert_nil, #assert_not_nil, #assert_presence, #error_message_for, #validate, #validate_blank, #validate_boolean, #validate_class, #validate_group, #validate_instance_of, #validate_matches, #validate_name, #validate_nil, #validate_not_nil, #validate_presence

Methods inherited from Base

instance

Constructor Details

#initializeAggregator

Returns a new instance of Aggregator.



63
64
65
66
67
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 63

def initialize
  super

  @failures = []
end

Instance Method Details

#<<(message) ⇒ Array

Appends the message to the failure messages.

Parameters:

  • message (String)

    the message to append.

Returns:

  • (Array)

    the updated failure messages.

See Also:

  • Array#<<.


# File 'lib/sleeping_king_studios/tools/assertions.rb', line 69

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



132
133
134
135
136
137
138
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 132

def assert_group(error_class: AssertionError, message: nil, &assertions)
  return super if message

  raise ArgumentError, 'no block given' unless block_given?

  assertions.call(self)
end

#clearArray

Removes all items from the failure messages.

Returns:

  • (Array)

    the empty failure messages.

See Also:

  • Array#clear.


# File 'lib/sleeping_king_studios/tools/assertions.rb', line 78

#countInteger

Returns a count of the failure message.

Returns:

  • (Integer)

    the number of failure messages.

See Also:

  • Array#count.


# File 'lib/sleeping_king_studios/tools/assertions.rb', line 85

#eachEnumerator #each {|message| ... } ⇒ Object

Iterates over the failure messages.

Overloads:

  • #eachEnumerator

    Returns an enumerator that iterates over the failure messages.

    Returns:

    • (Enumerator)

      an enumerator over the messages.

    See Also:

    • Enumerable#each.
  • #each {|message| ... } ⇒ Object

    Yields each failure message to the block.

    Yield Parameters:

    • message (String)

      the current failure message.

    See Also:

    • Enumerable#each.


# File 'lib/sleeping_king_studios/tools/assertions.rb', line 92

#empty?true, false

Checks if there are any failure messages.

Returns:

  • (true, false)

    true if there are no failure messages; otherwise false.

See Also:

  • Enumerable#empty?


# File 'lib/sleeping_king_studios/tools/assertions.rb', line 109

#failure_messageString

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:

  • (String)

    the combined messages for each failed assertion.



158
159
160
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 158

def failure_message
  failures.join(', ')
end

#sizeInteger

Returns a count of the failure message.

Returns:

  • (Integer)

    the number of failure messages.

See Also:

  • Array#size.


123
124
125
126
127
128
129
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 123

def_delegators :@failures,
:<<,
:clear,
:count,
:each,
:empty?,
:size