Class: BracketTree::PositionalRelation
- Inherits:
-
Object
- Object
- BracketTree::PositionalRelation
- Defined in:
- lib/bracket_tree/positional_relation.rb
Overview
PositionalRelation
This class is a relational object used for constructing tree traversal when you do not know the exact position value that you are looking for. It uses two types of methods: relation condition methods and relation access methods.
Relation Condition Methods
Relation condition methods take the original PositionalRelation object and
add conditions, returning the original object for easy chaining of relation
conditions. Actual traversal does not happen with these methods.
bracket = BracketTree::Bracket::DoubleElimination.by_size(4)
relation = BracketTree::PositionalRelation.new(bracket) # => <BracketTree::PositionalRelation:0x007fa9431e1060>
relation.winners # => <BracketTree::PositionalRelation:0x007fa9431e1060>
relation.round(4) # => <BracketTree::PositionalRelation:0x007fa9431e1060>
Relation conditions can be chained to create very specific or very broad options. Once you call a Relation Access Method, it will build based on the chain.
bracket = BracketTree::Bracket::DoubleElimination.by_size(4)
relation = BracketTree::PositionalRelation.new(bracket)
relation.winners.round(1).seat(3) # => <BracketTree::Node:0x01e37 @position=5>
Typically you will not directly instance BracketTree::RelationalPosition due to the need to pass in a bracket. Instead, bracket objects have access to these methods directly, and will return a BracketTree::PositionalRelation object.
Relation Access Methods
Relation access methods take the complete set of relation conditions and uses them as instructions for traversing the tree to collect the Node(s) requested.
bracket = BracketTree::Bracket::DoubleElimination.by_size(4)
bracket.winners.round(4).first # => <BracketTree::Node:0x007fa92 @position=16>
bracket.losers.round(2).seat(1) # => <BracketTree::Node:0x001eaf9 @position=2>
Instance Attribute Summary collapse
-
#bracket ⇒ Object
readonly
Returns the value of attribute bracket.
Instance Method Summary collapse
-
#all ⇒ Array<BracketTree::Node>
Retrieves all seats based on the stored relation conditions.
-
#by_round(round, side) ⇒ Array
Retrieves an Array of Nodes for a given round on a given side.
-
#first ⇒ BracketTree::Node?
Retrieves the first seat based on the stored relation conditions.
-
#initialize(bracket) ⇒ PositionalRelation
constructor
A new instance of PositionalRelation.
-
#last ⇒ BracketTree::Node?
Retrieves the last seat based on the stored relation conditions.
-
#losers ⇒ BracketTree::PositionalRelation
Adds relation condition for right half of the bracket.
-
#round(number) ⇒ BracketTree::PositionalRelation
Adds relation condition for a given round.
-
#seat(number) ⇒ BracketTree::Node?
Retrieves the
number-th seat from the given relation conditions. -
#winners ⇒ BracketTree::PositionalRelation
Adds relation condition for left half of the bracket.
Constructor Details
#initialize(bracket) ⇒ PositionalRelation
Returns a new instance of PositionalRelation.
44 45 46 |
# File 'lib/bracket_tree/positional_relation.rb', line 44 def initialize bracket @bracket = bracket end |
Instance Attribute Details
#bracket ⇒ Object (readonly)
Returns the value of attribute bracket.
42 43 44 |
# File 'lib/bracket_tree/positional_relation.rb', line 42 def bracket @bracket end |
Instance Method Details
#all ⇒ Array<BracketTree::Node>
Retrieves all seats based on the stored relation conditions
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bracket_tree/positional_relation.rb', line 67 def all if @side if @round seats = by_round @round, @side else side_root = @bracket.root.send(@side) seats = [] @bracket.top_down(side_root) do |node| seats << node end end else if @round seats = by_round(@round, :left) + by_round(@round, :right) else seats = [] @bracket.top_down(@bracket.root) do |node| seats << node end end end seats end |
#by_round(round, side) ⇒ Array
Retrieves an Array of Nodes for a given round on a given side
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/bracket_tree/positional_relation.rb', line 108 def by_round round, side depth = @bracket.depth[side] - (round - 1) seats = [] side_root = @bracket.root.send(side) @bracket.top_down(side_root) do |node| if node.depth == depth seats << node end end seats end |
#first ⇒ BracketTree::Node?
Retrieves the first seat based on the stored relation conditions
51 52 53 54 |
# File 'lib/bracket_tree/positional_relation.rb', line 51 def first seats = all seats.first end |
#last ⇒ BracketTree::Node?
Retrieves the last seat based on the stored relation conditions
59 60 61 62 |
# File 'lib/bracket_tree/positional_relation.rb', line 59 def last seats = all seats.last end |
#losers ⇒ BracketTree::PositionalRelation
Adds relation condition for right half of the bracket. This is used in double-elimination brackets primarily.
135 136 137 138 |
# File 'lib/bracket_tree/positional_relation.rb', line 135 def losers @side = :right return self end |
#round(number) ⇒ BracketTree::PositionalRelation
Adds relation condition for a given round. This is represented by translating the total depth of the tree minus the number parameter.
145 146 147 148 |
# File 'lib/bracket_tree/positional_relation.rb', line 145 def round number @round = number return self end |
#seat(number) ⇒ BracketTree::Node?
Retrieves the number-th seat from the given relation conditions
97 98 99 100 101 |
# File 'lib/bracket_tree/positional_relation.rb', line 97 def seat number seats = all seats[number-1] end |
#winners ⇒ BracketTree::PositionalRelation
Adds relation condition for left half of the bracket. This is used in double-elimination brackets primarily.
126 127 128 129 |
# File 'lib/bracket_tree/positional_relation.rb', line 126 def winners @side = :left return self end |