class Aruba::Matchers::IncludeAnObject

@api private Provides the implementation for `include_an_object`. Not intended to be instantiated directly.

Attributes

any_succeeded_object[RW]

@private

failed_objects[R]

@private

matcher[R]

@private

Public Class Methods

new(matcher) click to toggle source
# File lib/aruba/matchers/collection/include_an_object.rb, line 23
def initialize(matcher)
  @matcher              = matcher
  @failed_objects       = []
  @any_succeeded_object = false
end

Public Instance Methods

description() click to toggle source

@api private @return [String]

# File lib/aruba/matchers/collection/include_an_object.rb, line 45
def description
  improve_hash_formatting "include an object #{description_of matcher}"
end
does_not_match?(actual) click to toggle source
# File lib/aruba/matchers/collection/include_an_object.rb, line 49
def does_not_match?(actual)
  @actual = actual

  return false unless iterable?

  index_objects

  any_succeeded_object == false
end
failure_message() click to toggle source

@api private @return [String]

# File lib/aruba/matchers/collection/include_an_object.rb, line 31
def failure_message
  return "#{improve_hash_formatting(super)}, but was not iterable" unless iterable?

  return failed_objects.first if failed_objects.size == 1

  all_messages = [improve_hash_formatting(super)]
  failed_objects.each_with_index do |matcher_failure_message, index|
    all_messages << failure_message_for_item(index, matcher_failure_message)
  end
  all_messages.join("\n\n")
end

Private Instance Methods

add_new_line_if_needed(message) click to toggle source
# File lib/aruba/matchers/collection/include_an_object.rb, line 97
def add_new_line_if_needed(message)
  message.start_with?("\n") ? message : "\n#{message}"
end
failure_message_for_item(index, failure_message) click to toggle source
# File lib/aruba/matchers/collection/include_an_object.rb, line 90
def failure_message_for_item(index, failure_message)
  failure_message = indent_multiline_message(add_new_line_if_needed(failure_message))
  indent_multiline_message(
    "object at index #{index} failed to match:#{failure_message}"
  )
end
index_objects() click to toggle source
# File lib/aruba/matchers/collection/include_an_object.rb, line 72
def index_objects
  actual.each_with_index do |actual_item, index|
    cloned_matcher = matcher.clone
    begin
      matches = cloned_matcher.matches?(actual_item)
    rescue StandardError
      matches = nil
    end

    if matches
      self.any_succeeded_object = true
      break
    else
      failed_objects[index] = cloned_matcher.failure_message
    end
  end
end
match(expected, actual) click to toggle source
# File lib/aruba/matchers/collection/include_an_object.rb, line 61
def match(expected, actual)
  @actual   = actual
  @expected = expected

  return false unless iterable?

  index_objects

  any_succeeded_object == true
end