Permalink
Browse files

Adds Bracket#match_winner and Bracket#match_loser

  • Loading branch information...
1 parent 3946c93 commit a6c6fbf11d990b97ad8ddce30a96547785832f74 @cadwallion cadwallion committed May 24, 2012
Showing with 32 additions and 6 deletions.
  1. +29 −5 lib/bracket_tree/bracket.rb
  2. +3 −1 spec/unit/bracket_tree/bracket_spec.rb
View
34 lib/bracket_tree/bracket.rb
@@ -122,16 +122,40 @@ def at position
# manipulating bracket positions
#
# @param Fixnum seat - winning seat position
+ # @return Boolean result - result of progression
def match_winner seat
match = @matches.find { |m| m.include? seat }
- losing_seat = match.seats.find { |s| s != seat }
- if match.winner_to
- self.replace match.winner_to, seats.at(seat).payload
+ if match
+ losing_seat = match.seats.find { |s| s != seat }
+
+ if match.winner_to
+ replace match.winner_to, at(seat).payload
+ end
+
+ if match.loser_to
+ replace match.loser_to, at(losing_seat).payload
+ end
+
+ return true
+ else
+ return false
end
+ end
- if match.loser_to
- self.replace match.loser_to, seats.at(losing_seat).payload
+ # Inverse of `match_winner`, progresses the bracket based on seat. See `match_winner`
+ # for more details
+ #
+ # @param Fixnum seat - losing seat position
+ # @return Boolean result - result of progression
+ def match_loser seat
+ match = @matches.find { |m| m.include? seat }
+
+ if match
+ winning_seat = match.seats.find { |s| s != seat }
+ match_winner winning_seat
+ else
+ return false
end
end
View
4 spec/unit/bracket_tree/bracket_spec.rb
@@ -173,8 +173,10 @@
let(:bracket) { BracketTree::Template::DoubleElimination.by_size(4).generate_blank_bracket }
it 'copies the seat data to the seat specified in the match winner_to' do
+ bracket.at(1).payload[:seed_value] = 1
+
bracket.match_winner 1
- bracket.at(2).payload.should == bracket.at(11).payload
+ bracket.at(2).payload.should == bracket.at(1).payload
end
end
end

0 comments on commit a6c6fbf

Please sign in to comment.