Class: Sus::RespondTo

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/respond_to.rb

Overview

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

Defined Under Namespace

Classes: WithOptions, WithParameters

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ RespondTo

Initialize a new RespondTo predicate.



77
78
79
80
81
# File 'lib/sus/respond_to.rb', line 77

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

Instance Method Details

#call(assertions, subject) ⇒ Object

Evaluate this predicate against a subject.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sus/respond_to.rb', line 100

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

Print a representation of this predicate.



93
94
95
# File 'lib/sus/respond_to.rb', line 93

def print(output)
  output.write("respond to ", :variable, @method.to_s, :reset)
end

#with_options(*options) ⇒ Object

Specify that the method should have specific keyword options.



86
87
88
89
# File 'lib/sus/respond_to.rb', line 86

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