SusSourceSusBeOr

class Or

Represents a logical OR combination of multiple predicates.

Definitions

def initialize(predicates)

Initialize a new OR predicate.

Signature

parameter predicates Array

The predicates to combine with OR logic.

Implementation

def initialize(predicates)
	@predicates = predicates
end

def print(output)

Print a representation of this predicate.

Signature

parameter output Output

The output target.

Implementation

def print(output)
	@predicates.each_with_index do |predicate, index|
		if index > 0
			output.write(" or ", :reset)
		end
		
		predicate.print(output)
	end
end

def call(assertions, subject)

Evaluate this predicate against a subject.

Signature

parameter assertions Assertions

The assertions instance to use.

parameter subject Object

The subject to evaluate.

Implementation

def call(assertions, subject)
	assertions.nested(self) do |assertions|
		@predicates.each do |predicate|
			predicate.call(assertions, subject)
		end
		
		if assertions.passed.any?
			# At least one passed, so we don't care about failures:
			assertions.failed.clear
		else
			# Nothing passed, so we failed:
			assertions.assert(false, "could not find any matching predicate")
		end
	end
end

def &(other)

Combine this predicate with another using AND logic.

Signature

parameter other Object

Another predicate to combine.

returns And

A new AND predicate.

Implementation

def &(other)
	And.new([self, other])
end

def |(other)

Combine this predicate with another using OR logic.

Signature

parameter other Object

Another predicate to combine.

returns Or

A new OR predicate.

Implementation

def |(other)
	Or.new(@predicates + [other])
end