class Rubolph::Picker

Public Class Methods

new(participants, exclusions={}) click to toggle source
# File lib/rubolph/picker.rb, line 4
def initialize(participants, exclusions={})
  @participants = participants
  @exclusions   = exclusions
  @results      = {}
end

Public Instance Methods

pick() click to toggle source
# File lib/rubolph/picker.rb, line 11
def pick
  @iteration = 0
  loop do
    @results  = { }
    receivers = @participants.shuffle
    @participants.each do |giver|
      @results[giver] = receivers.delete(receivers.sample)
    end

    valid = true
    @results.each do |giver, receiver|
      if giver == receiver || @exclusions[giver] == receiver
        valid = false
        break
      end
    end
    break if valid

    @iteration += 1 and handle_irreconcilable
  end
  @results
end

Private Instance Methods

handle_irreconcilable() click to toggle source
# File lib/rubolph/picker.rb, line 35
def handle_irreconcilable
  if @iteration > 10000
    raise ArgumentError.new 'Unable to reconcile participants with exclusions!'
  end
end