Class: Assigner
- Inherits:
-
Object
- Object
- Assigner
- Defined in:
- lib/assigner.rb
Defined Under Namespace
Modules: Assignable
Class Method Summary collapse
Class Method Details
.assign_each_other!(items, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/assigner.rb', line 3 def self.assign_each_other!(items, &block) # Wrap Items to add assignation behavior wrap(items, &block) # Random assignation between items items_copy = items.dup items.each do |item| item.assignation = items_copy.delete_at(rand(items_copy.size)) end # Switch assignation if it's not allowed items.each do |item| unless item.assignation.can_be_assigned_to? item candidates = items.select {|i| i.assignation.can_be_assigned_to?(item) && item.assignation.can_be_assigned_to?(i)} raise if candidates.empty? other = candidates[rand(candidates.size)] temp = item.assignation item.assignation = other.assignation other.assignation = temp finished = false end end end |