Skip to content
This repository
Browse code

Adds Bracket#match_winner and Bracket#match_loser

  • Loading branch information...
commit a6c6fbf11d990b97ad8ddce30a96547785832f74 1 parent 3946c93
Andrew Nordman authored
34  lib/bracket_tree/bracket.rb
@@ -122,16 +122,40 @@ def at position
122 122
     # manipulating bracket positions
123 123
     #
124 124
     # @param Fixnum seat - winning seat position
  125
+    # @return Boolean result - result of progression
125 126
     def match_winner seat
126 127
       match = @matches.find { |m| m.include? seat }
127  
-      losing_seat = match.seats.find { |s| s != seat }
128 128
 
129  
-      if match.winner_to
130  
-        self.replace match.winner_to, seats.at(seat).payload
  129
+      if match
  130
+        losing_seat = match.seats.find { |s| s != seat }
  131
+
  132
+        if match.winner_to
  133
+          replace match.winner_to, at(seat).payload
  134
+        end
  135
+
  136
+        if match.loser_to
  137
+          replace match.loser_to, at(losing_seat).payload
  138
+        end
  139
+
  140
+        return true
  141
+      else
  142
+        return false
131 143
       end
  144
+    end
132 145
 
133  
-      if match.loser_to
134  
-        self.replace match.loser_to, seats.at(losing_seat).payload
  146
+    # Inverse of `match_winner`, progresses the bracket based on seat. See `match_winner`
  147
+    # for more details
  148
+    #
  149
+    # @param Fixnum seat - losing seat position
  150
+    # @return Boolean result - result of progression
  151
+    def match_loser seat
  152
+      match = @matches.find { |m| m.include? seat }
  153
+      
  154
+      if match
  155
+        winning_seat = match.seats.find { |s| s != seat }
  156
+        match_winner winning_seat
  157
+      else
  158
+        return false
135 159
       end
136 160
     end
137 161
 
4  spec/unit/bracket_tree/bracket_spec.rb
@@ -173,8 +173,10 @@
173 173
     let(:bracket) { BracketTree::Template::DoubleElimination.by_size(4).generate_blank_bracket }
174 174
 
175 175
     it 'copies the seat data to the seat specified in the match winner_to' do
  176
+      bracket.at(1).payload[:seed_value] = 1
  177
+
176 178
       bracket.match_winner 1
177  
-      bracket.at(2).payload.should == bracket.at(11).payload
  179
+      bracket.at(2).payload.should == bracket.at(1).payload
178 180
     end
179 181
   end
180 182
 end

0 notes on commit a6c6fbf

Please sign in to comment.
Something went wrong with that request. Please try again.