Permalink
Browse files

Merge pull request #1 from agoragames/tweaks_mwilson

Tweaks, a convenience method, and speculative refactor.
  • Loading branch information...
2 parents e6b72d3 + f20ca33 commit 68be8fcfe37fca32d4e0e3f6229457ec90a40e67 @cadwallion cadwallion committed May 23, 2012
Showing with 33 additions and 9 deletions.
  1. +7 −5 lib/bracket_tree/bracket.rb
  2. +4 −4 lib/bracket_tree/template.rb
  3. +22 −0 spec/unit/bracket_tree/bracket_spec.rb
@@ -7,7 +7,7 @@ class SeedLimitExceededError < Exception ; end
include Enumerable
attr_accessor :root, :seed_order, :insertion_order
-
+
def initialize
@insertion_order = []
end
@@ -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,14 +99,16 @@ def nodes
to_a.sort_by { |node| @insertion_order.index(node.position) }
end
- def size
- @insertion_order.size
+ def at position
+ find { |n| n.position == position }
end
+ alias_method :size, :count
+
def in_order(node, block)
if node
unless node.left.nil?
- in_order(node.left, block)
+ in_order(node.left, block)
end
block.call(node)
@@ -18,13 +18,13 @@ def by_size size
end
# Generates Template from JSON
- #
+ #
# @param [String] json - the bracket template in its standard data specification
# @return [BracketTree::Template]
def from_json json
template = new
if json['seats']
- template.seats = json['seats'].map { |s| s['position'] }
+ template.seats = json['seats'].map { |s| s['position'] }
end
if json['startingSeats']
@@ -68,12 +68,12 @@ def generate_blank_bracket
bracket
end
-
+
# Returns hash representation of the Template
#
# @return [Hash] template
def to_h
- hash = {
+ {
'seats' => @seats.map { |s| { 'position' => s } },
'startingSeats' => @starting_seats,
'nodes' => @nodes
@@ -105,6 +105,28 @@
end
end
+ describe "#size" do
+ it "should return the number of nodes in the bracket" do
+ bracket.add 3, { foo: 'foo' }
+ bracket.add 2, { bar: 'bar' }
+ bracket.add 4, { baz: 'baz' }
+ bracket.size.should == 3
+ 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 68be8fc

Please sign in to comment.