Permalink
Browse files

Add Bracket#at(position) convenience method.

  • Loading branch information...
1 parent 72c1866 commit 88609b6405f964e728c975b21652f4d6612ab442 @hypomodern hypomodern committed May 23, 2012
Showing with 18 additions and 1 deletion.
  1. +5 −1 lib/bracket_tree/bracket.rb
  2. +13 −0 spec/unit/bracket_tree/bracket_spec.rb
@@ -49,7 +49,7 @@ def add position, data
# @param [Fixnum] position - the node position to replace
# @param payload - the new payload object to replace
def replace position, payload
- node = find { |n| n.position == position }
+ node = at position
if node
node.payload = payload
true
@@ -99,6 +99,10 @@ def nodes
to_a.sort_by { |node| @insertion_order.index(node.position) }
end
+ def at position
+ find { |n| n.position == position }
+ end
+
alias_method :size, :count
def in_order(node, block)
@@ -114,6 +114,19 @@
end
end
+ describe "#at" do
+ before do
+ bracket.add 3, { foo: 'foo' }
+ bracket.add 2, { bar: 'bar' }
+ bracket.add 1, { baz: 'baz' }
+ end
+ it "should return the node at the given position in the bracket" do
+ bracket.at(1).payload.should == { baz: 'baz'}
+ bracket.at(2).payload.should == { bar: 'bar'}
+ bracket.at(3).payload.should == { foo: 'foo'}
+ end
+ end
+
describe '#seed' do
let(:bracket) { BracketTree::Template::SingleElimination.by_size(4).generate_blank_bracket }
let(:players) do

0 comments on commit 88609b6

Please sign in to comment.