class MatchyMatchy::Candidate
Represents a candidate in the Stable Match
algorithm. Since the concept of a wrapped object with an ordered list of preferences is common to both candidates and targets, Target
inherits directly from Candidate
, so when reading this documentation in that context, the words ‘candidate’ and ‘target’ should be reversed.
Attributes
Returns the wrapped object being represented by this candidate
Returns an ordered list of this candidate’s preferred targets
Public Class Methods
@param object The object being represented by this candidate.
# File lib/matchy_matchy/candidate.rb, line 15 def initialize(object) @object = object @preferences = [] end
Public Instance Methods
Returns true if two candidates’ objects are equal, false otherwise.
@param other [MatchyMatchy::Candidate] Another object for comparison @return [Boolean] True if the two candidates are equal, false otherwise
# File lib/matchy_matchy/candidate.rb, line 45 def eql?(other) object.eql?(other.object) end
Returns true if this candidate has expressed a preference for the given target, false otherwise.
@param target A target entity @return [Boolean] True if the candidate prefers the target
# File lib/matchy_matchy/candidate.rb, line 37 def include?(target) preferences.include?(target) end
Determine the index of the given target within the candidate’s preferences.
@param target [MatchyMatchy::Candidate] A target entity to search for @return [Integer] The target’s position, or nil
# File lib/matchy_matchy/candidate.rb, line 54 def index(target) preferences.find_index(target) end
Indicates an order of preference for this candidate’s matches. The given targets will be appended to any existing preferences. Returns self
to maintain the semantics of `<<`.
@param targets [Array] One or more targets, in order of preference.
# File lib/matchy_matchy/candidate.rb, line 25 def prefer(*targets) preferences.push(*targets) self end
Returns a string representation of the candidate. Delegates to the wrapped object.
@return [String] a string for display
# File lib/matchy_matchy/candidate.rb, line 62 def to_s object.to_s end