class MatchyMatchy::MatchList

A sorted list of matches with a strict capacity.

Public Class Methods

new(capacity = 1) click to toggle source

Initializes the list.

@param capacity [Integer] The maximum number of matches this list can hold

# File lib/matchy_matchy/match_list.rb, line 9
def initialize(capacity = 1)
  @matches = []
  @capacity = capacity
end

Public Instance Methods

<<(match) click to toggle source

Pushes a match into the list. The list is re-sorted and any matches that don’t fit are rejected.

@param match [MatchyMatchy::Match] @return [MatchyMatchy::MatchList] Self

# File lib/matchy_matchy/match_list.rb, line 19
def <<(match)
  if include?(match)
    match.reject!
  else
    @matches << match
    @matches.sort!
    @matches.pop.reject! if @matches.size > @capacity
  end
  self
end
each(&block) click to toggle source

Returns an enumerator for the matches in the list. If a block is given, iterates through the matches in order, yielding them to the block.

@yield [MatchyMatchy::Match] @return Enumerator

# File lib/matchy_matchy/match_list.rb, line 44
def each(&block)
  return enum_for(:each) unless block_given?
  to_a.each(&block)
end
include?(match) click to toggle source

Returns true if the list contains the given match.

@param match [MatchyMatchy::Match] @return [Boolean] True if the list contains this match already.

# File lib/matchy_matchy/match_list.rb, line 34
def include?(match)
  any? { |m| m.eql?(match) }
end
to_a() click to toggle source

Returns an array of the matches in the list.

@return [Array]

# File lib/matchy_matchy/match_list.rb, line 52
def to_a
  @matches.dup.freeze
end
Also aliased as: to_ary
to_ary()
Alias for: to_a