SusSourceSusRespondTo

class RespondTo

Represents a predicate that checks if an object responds to a method.

Nested

Definitions

def initialize(method)

Initialize a new RespondTo predicate.

Signature

parameter method Symbol, String

The method name to check for.

Implementation

def initialize(method)
	@method = method
	@parameters = nil
	@options = nil
end

def with_options(*options)

Specify that the method should have specific keyword options.

Signature

parameter options Array(Symbol)

The option names that should be present.

returns RespondTo

Returns self for method chaining.

Implementation

def with_options(*options)
	@options = WithOptions.new(options)
	return self
end

def print(output)

Print a representation of this predicate.

Signature

parameter output Output

The output target.

Implementation

def print(output)
	output.write("respond to ", :variable, @method.to_s, :reset)
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|
		condition = subject.respond_to?(@method)
		assertions.assert(condition, self)
		
		if condition and (@parameters or @options)
			parameters = subject.method(@method).parameters
			@parameters.call(assertions, parameters) if @parameters
			@options.call(assertions, parameters) if @options
		end
	end
end