Skip to content

Commit

Permalink
Merge pull request #1 from agoragames/tweaks_mwilson
Browse files Browse the repository at this point in the history
Tweaks, a convenience method, and speculative refactor.
  • Loading branch information
cadwallion committed May 23, 2012
2 parents e6b72d3 + f20ca33 commit 68be8fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
12 changes: 7 additions & 5 deletions lib/bracket_tree/bracket.rb
Expand Up @@ -7,7 +7,7 @@ class SeedLimitExceededError < Exception ; end


include Enumerable include Enumerable
attr_accessor :root, :seed_order, :insertion_order attr_accessor :root, :seed_order, :insertion_order

def initialize def initialize
@insertion_order = [] @insertion_order = []
end end
Expand Down Expand Up @@ -49,7 +49,7 @@ def add position, data
# @param [Fixnum] position - the node position to replace # @param [Fixnum] position - the node position to replace
# @param payload - the new payload object to replace # @param payload - the new payload object to replace
def replace position, payload def replace position, payload
node = find { |n| n.position == position } node = at position
if node if node
node.payload = payload node.payload = payload
true true
Expand Down Expand Up @@ -99,14 +99,16 @@ def nodes
to_a.sort_by { |node| @insertion_order.index(node.position) } to_a.sort_by { |node| @insertion_order.index(node.position) }
end end


def size def at position
@insertion_order.size find { |n| n.position == position }
end end


alias_method :size, :count

def in_order(node, block) def in_order(node, block)
if node if node
unless node.left.nil? unless node.left.nil?
in_order(node.left, block) in_order(node.left, block)
end end


block.call(node) block.call(node)
Expand Down
8 changes: 4 additions & 4 deletions lib/bracket_tree/template.rb
Expand Up @@ -18,13 +18,13 @@ def by_size size
end end


# Generates Template from JSON # Generates Template from JSON
# #
# @param [String] json - the bracket template in its standard data specification # @param [String] json - the bracket template in its standard data specification
# @return [BracketTree::Template] # @return [BracketTree::Template]
def from_json json def from_json json
template = new template = new
if json['seats'] if json['seats']
template.seats = json['seats'].map { |s| s['position'] } template.seats = json['seats'].map { |s| s['position'] }
end end


if json['startingSeats'] if json['startingSeats']
Expand Down Expand Up @@ -68,12 +68,12 @@ def generate_blank_bracket


bracket bracket
end end

# Returns hash representation of the Template # Returns hash representation of the Template
# #
# @return [Hash] template # @return [Hash] template
def to_h def to_h
hash = { {
'seats' => @seats.map { |s| { 'position' => s } }, 'seats' => @seats.map { |s| { 'position' => s } },
'startingSeats' => @starting_seats, 'startingSeats' => @starting_seats,
'nodes' => @nodes 'nodes' => @nodes
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/bracket_tree/bracket_spec.rb
Expand Up @@ -105,6 +105,28 @@
end end
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 describe '#seed' do
let(:bracket) { BracketTree::Template::SingleElimination.by_size(4).generate_blank_bracket } let(:bracket) { BracketTree::Template::SingleElimination.by_size(4).generate_blank_bracket }
let(:players) do let(:players) do
Expand Down

0 comments on commit 68be8fc

Please sign in to comment.