Class: BracketTree::Template::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bracket_tree/template.rb

Direct Known Subclasses

DoubleElimination, SingleElimination

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



50
51
52
53
54
# File 'lib/bracket_tree/template.rb', line 50

def initialize
  @seats = []
  @starting_seats = []
  @matches = []
end

Instance Attribute Details

#matchesObject

Returns the value of attribute matches.



48
49
50
# File 'lib/bracket_tree/template.rb', line 48

def matches
  @matches
end

#seatsObject

Returns the value of attribute seats.



48
49
50
# File 'lib/bracket_tree/template.rb', line 48

def seats
  @seats
end

#starting_seatsObject Also known as: seed_order

Returns the value of attribute starting_seats.



48
49
50
# File 'lib/bracket_tree/template.rb', line 48

def starting_seats
  @starting_seats
end

Class Method Details

.by_size(size) ⇒ Object

Reads stored JSON files to generate a Template

return [nil, BracketTree::Template] template - the resulting bracket template

Parameters:

  • size (Fixnum)
    • player count



10
11
12
13
14
15
16
17
18
# File 'lib/bracket_tree/template.rb', line 10

def by_size size
  filename = File.join location, "#{size}.json"

  if File.exists? filename
    from_json JSON.parse(File.read(filename), symbolize_names: true)
  else
    return nil
  end
end

.from_json(json) ⇒ BracketTree::Template

Generates Template from JSON

Parameters:

  • json (String)
    • the bracket template in its standard data specification

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bracket_tree/template.rb', line 24

def from_json json
  template = new
  if json[:seats]
    template.seats = json[:seats].map { |s| s[:position] }
  end

  if json[:starting_seats]
    template.starting_seats = json[:starting_seats]
  end

  if json[:matches]
    template.matches = json[:matches]
  end

  template
end

.locationString

Folder location of the template JSON files. Abstract method

Returns:

  • (String)

    location - the folder name of the JSON files

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/bracket_tree/template.rb', line 43

def location
  raise NotImplementedError, 'Abstract method, please define `location` in subclass.'
end

Instance Method Details

#to_hHash

Returns hash representation of the Template

Returns:

  • (Hash)

    template



59
60
61
62
63
64
65
# File 'lib/bracket_tree/template.rb', line 59

def to_h
  {
    seats: @seats.map { |s| { position: s } },
    starting_seats: @starting_seats,
    matches: @matches
  }
end