Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks, a convenience method, and speculative refactor. #1

Merged
merged 5 commits into from May 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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