class MatchyMatchy::Match

Represents a proposed match in the Stable Match algorithm.

Attributes

candidate[R]

Returns the candidate being matched.

index[R]

Returns the (zero-based) index of this match in the candidate’s preferences.

Public Class Methods

new(candidate:, index: 0) click to toggle source

Initializes the match with a candidate and an index.

@param candidate [MatchyMatchy::Candidate] A candidate @param index [Integer] (Zero-based) index of preference to try.

Defaults to 0, indicating the candidate’s first choice.
# File lib/matchy_matchy/match.rb, line 21
def initialize(candidate:, index: 0)
  raise ArgumentError, "candidate does not have #{i+1} choices" \
    if index > candidate.preferences.size

  @candidate = candidate
  @index = index
end

Public Instance Methods

<=>(other) click to toggle source

Compares two matches, in order of preference within the target. For comparison to be meaningful, the two matches must have the same target, and both matches’ candidates must be preferred by the target.

@param other [MatchyMatchy::Match] A second match for comparison @return [Integer]

* negative if `self < other`
* 0 if `self == other`
* positive if `self > other`
# File lib/matchy_matchy/match.rb, line 55
def <=>(other)
  raise ArgumentError, "matches must have the same target" \
    unless target == other.target
  target.index(candidate) <=> target.index(other.candidate)
end
eql?(other) click to toggle source

Returns true if two matches are equal. True if the two matches’ candidates and targets are identical, respectively.

@return [Boolean] True if `self` and `other` are equal, false otherwise.

# File lib/matchy_matchy/match.rb, line 42
def eql?(other)
  target.eql?(other.target) && candidate.eql?(other.candidate)
end
mutual?() click to toggle source

Returns true if the target also prefers the candidate.

@return True if target’s preferences include this match’s candidate,

false otherwise.
# File lib/matchy_matchy/match.rb, line 65
def mutual?
  target.include?(candidate)
end
reject!() click to toggle source

Rejects the match. This may be handled by attaching a block with Cry’s `on` semantics:

match.on(:reject) { do_something }
# File lib/matchy_matchy/match.rb, line 73
def reject!
  publish!(:reject)
end
target() click to toggle source

Returns the target of the match by looking it up in the candidate’s preferences

@return [MatchyMatchy::Target] The target object

# File lib/matchy_matchy/match.rb, line 33
def target
  candidate.preferences[index]
end