Permalink
Browse files

Aliases BracketTree#nodes to BracketTree#to_a

  • Loading branch information...
1 parent 3beeb3e commit 81f2a71b961a503d4e87262dc7e2346ce484b7cc @cadwallion cadwallion committed May 23, 2012
Showing with 24 additions and 2 deletions.
  1. +10 −2 lib/bracket_tree/bracket.rb
  2. +14 −0 spec/unit/bracket_tree/bracket_spec.rb
@@ -12,6 +12,11 @@ def initialize
@insertion_order = []
end
+ # Adds a Node at the given position, setting the data as the payload. Maps to
+ # binary tree under the hood. The `data` can be any serializable object.
+ #
+ # @param Fixnum position - Seat position to add
+ # @param Object data - the player object to store in the Seat position
def add position, data
node = Node.new position, data
@insertion_order << position
@@ -88,10 +93,13 @@ def to_h
@root.to_h
end
- def nodes
- to_a.sort_by { |node| @insertion_order.index(node.position) }
+ # returns an array of nodes based on insertion_order
+ def to_a
+ entries.sort_by { |node| @insertion_order.index(node.position) }
end
+ alias_method :nodes, :to_a
+
def at position
find { |n| n.position == position }
end
@@ -158,4 +158,18 @@
expect { bracket.seed players }.to raise_error(BracketTree::Bracket::SeedLimitExceededError)
end
end
+
+ describe '#nodes' do
+ let(:bracket) { BracketTree::Template::SingleElimination.by_size(4).generate_blank_bracket }
+
+ it 'should return an array of nodes based on insertion order' do
+ bracket.nodes.should be_a Array
+ bracket.nodes.map { |n| n.position }.should == bracket.insertion_order
+ end
+
+ it 'returns an empty array when no nodes are present' do
+ bracket = BracketTree::Bracket.new
+ bracket.nodes.should == []
+ end
+ end
end

0 comments on commit 81f2a71

Please sign in to comment.