Skip to content
Browse files

Adds Bracket#seed

  • Loading branch information...
1 parent 3641304 commit 297ed50ef4afda205460368149cbf8b06abe0af3 @cadwallion cadwallion committed May 22, 2012
Showing with 39 additions and 0 deletions.
  1. +18 −0 lib/bracket_tree/bracket.rb
  2. +21 −0 spec/unit/bracket_tree/bracket_spec.rb
View
18 lib/bracket_tree/bracket.rb
@@ -55,6 +55,24 @@ def replace position, payload
end
end
+ # Seeds bracket based on `seed_order` value of bracket. Provide an iterator
+ # with players that will be inserted in the appropriate location. Will raise a
+ # SeedLimitExceededError if too many players are sent, and a NoSeedOrderError if
+ # the `seed_order` attribute is nil
+ #
+ # @param [Enumerable] players - players to be seeded
+ def seed players
+ if @seed_order.nil?
+ raise NoSeedOrderError, 'Bracket does not have a seed order.'
+ elsif players.size > @seed_order.size
+ raise SeedLimitExceededError, 'cannot seed more players than seed order list.'
+ else
+ @seed_order.each do |position|
+ replace position, players.shift
+ end
+ end
+ end
+
# This needs refactoring. Inconsistent interface
def add_winner winner
@root.payload.seed_value = winner.seed_position
View
21 spec/unit/bracket_tree/bracket_spec.rb
@@ -104,4 +104,25 @@
bracket.root.right.payload.should == { baz: 'baz' }
end
end
+
+ describe '#seed' do
+ let(:bracket) { BracketTree::Template::SingleElimination.by_size(4).generate_blank_bracket }
+ let(:players) do
+ [
+ { name: 'player4' },
+ { name: 'player1' },
+ { name: 'player3' },
+ { name: 'player2' }
+ ]
+ end
+
+ it 'should place the players in the bracket by seed order' do
+ bracket.seed players
+
+ bracket.find { |n| n.position == 1 }.payload.should == { name: 'player4' }
+ bracket.find { |n| n.position == 3 }.payload.should == { name: 'player1' }
+ bracket.find { |n| n.position == 5 }.payload.should == { name: 'player3' }
+ bracket.find { |n| n.position == 7 }.payload.should == { name: 'player2' }
+ end
+ end
end

0 comments on commit 297ed50

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