Class: Pairity::PairGenerator
- Inherits:
-
Object
- Object
- Pairity::PairGenerator
- Defined in:
- lib/pairity/pair_generator.rb
Constant Summary collapse
- @@shuffle =
false
Instance Attribute Summary collapse
-
#last_pairs ⇒ Object
readonly
Returns the value of attribute last_pairs.
-
#pairs ⇒ Object
readonly
Returns the value of attribute pairs.
Class Method Summary collapse
Instance Method Summary collapse
- #abolish_pairing(person1, person2) ⇒ Object
- #days_for_pair(person1, person2) ⇒ Object
- #equilibrium ⇒ Object
- #generate_pairs ⇒ Object
- #include?(person1, person2) ⇒ Boolean
-
#initialize(matrix) ⇒ PairGenerator
constructor
A new instance of PairGenerator.
- #move_solo_to_the_end ⇒ Object
- #nope(p1, p2) ⇒ Object
- #possible_pairs ⇒ Object
- #resistance(person1, person2, resistance) ⇒ Object
- #revert ⇒ Object
- #save_pairs ⇒ Object
Constructor Details
#initialize(matrix) ⇒ PairGenerator
Returns a new instance of PairGenerator.
15 16 17 18 19 20 |
# File 'lib/pairity/pair_generator.rb', line 15 def initialize(matrix) @matrix = matrix @last_pairs = [] @pairs = [] @nopes = [] end |
Instance Attribute Details
#last_pairs ⇒ Object (readonly)
Returns the value of attribute last_pairs.
9 10 11 |
# File 'lib/pairity/pair_generator.rb', line 9 def last_pairs @last_pairs end |
#pairs ⇒ Object (readonly)
Returns the value of attribute pairs.
9 10 11 |
# File 'lib/pairity/pair_generator.rb', line 9 def pairs @pairs end |
Class Method Details
.shuffle=(toggle) ⇒ Object
11 12 13 |
# File 'lib/pairity/pair_generator.rb', line 11 def self.shuffle=(toggle) @@shuffle = toggle end |
Instance Method Details
#abolish_pairing(person1, person2) ⇒ Object
63 64 65 |
# File 'lib/pairity/pair_generator.rb', line 63 def abolish_pairing(person1, person2) @matrix.add_weight_to_pair(1_000_000,[person1,person2].sort) end |
#days_for_pair(person1, person2) ⇒ Object
59 60 61 |
# File 'lib/pairity/pair_generator.rb', line 59 def days_for_pair(person1, person2) @matrix[person1, person2].days end |
#equilibrium ⇒ Object
71 72 73 |
# File 'lib/pairity/pair_generator.rb', line 71 def equilibrium @matrix.people.combination(2).to_a.size / (@matrix.people.size / 2) end |
#generate_pairs ⇒ Object
22 23 24 25 26 27 |
# File 'lib/pairity/pair_generator.rb', line 22 def generate_pairs @pairs = [] @pairs = @matrix.optimal_pairs(@nopes) move_solo_to_the_end @pairs end |
#include?(person1, person2) ⇒ Boolean
75 76 77 |
# File 'lib/pairity/pair_generator.rb', line 75 def include?(person1, person2) @pairs.any? { |pair| pair == [person1, person2].sort } end |
#move_solo_to_the_end ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/pairity/pair_generator.rb', line 33 def move_solo_to_the_end solo = @pairs.find { |pair| pair.any? { |person| person.name == "Han Solo" } } return @pairs unless solo index = @pairs.index(solo) @pairs[index], @pairs[-1] = @pairs[-1], @pairs[index] @pairs end |
#nope(p1, p2) ⇒ Object
41 42 43 44 |
# File 'lib/pairity/pair_generator.rb', line 41 def nope(p1,p2) pair = [p1, p2].sort @nopes << pair end |
#possible_pairs ⇒ Object
29 30 31 |
# File 'lib/pairity/pair_generator.rb', line 29 def possible_pairs @matrix.possible_pairs.reject { |pairing| pairing.any? { |pair| @nopes.include?(pair) }} end |
#resistance(person1, person2, resistance) ⇒ Object
67 68 69 |
# File 'lib/pairity/pair_generator.rb', line 67 def resistance(person1, person2, resistance) @matrix[person1, person2].resistance = resistance end |
#revert ⇒ Object
55 56 57 |
# File 'lib/pairity/pair_generator.rb', line 55 def revert @pairs = @last_pairs end |
#save_pairs ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/pairity/pair_generator.rb', line 46 def save_pairs @pairs.each do |pair| @matrix.add_weight_to_pair(1, pair) @matrix.add_day_to_pair(pair) end @nopes = [] @last_pairs = @pairs end |