Class: FReCon::MatchNumber
Overview
Public: A wrapper to handle converting match numbers and storing them.
Constant Summary collapse
- POSSIBLE_TYPES =
Public: All of the possible match types for a MatchNumber to have.
[:practice, :qualification, :quarterfinal, :semifinal, :final]
- ELIMINATION_TYPES =
Public: All of the elimination types for a MatchNumber to have.
[:quarterfinal, :semifinal, :final]
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Public: The numerical part of the match number.
-
#round ⇒ Object
readonly
Public: The round part of the match number.
-
#type ⇒ Object
readonly
Public: The type of the match.
Class Method Summary collapse
-
.demongoize(object) ⇒ Object
Public: Convert a stored match number to a MatchNumber object.
-
.evolve(object) ⇒ Object
Public: Convert a MatchNumber object to a storable string representation for queries.
-
.mongoize(object) ⇒ Object
Public: Convert a MatchNumber object to a storable string representation.
Instance Method Summary collapse
-
#elimination? ⇒ Boolean
Public: Determine if MatchNumber represents a match of any elimination type.
-
#final? ⇒ Boolean
Public: Determine if MatchNumber represents a final match.
-
#initialize(args) ⇒ MatchNumber
constructor
A new instance of MatchNumber.
-
#mongoize ⇒ Object
Public: Convert to a storable string representation.
-
#practice? ⇒ Boolean
Public: Determine if MatchNumber represents a practice match.
-
#qualification? ⇒ Boolean
Public: Determine if MatchNumber represents a qualification match.
-
#quarterfinal? ⇒ Boolean
Public: Determine if MatchNumber represents a quarterfinal match.
-
#replay? ⇒ Boolean
Public: Determine if MatchNumber represents a replay.
-
#semifinal? ⇒ Boolean
Public: Determine if MatchNumber represents a semifinal match.
-
#to_s ⇒ Object
Public: Convert to a String.
Constructor Details
#initialize(args) ⇒ MatchNumber
Returns a new instance of MatchNumber.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/frecon/match_number.rb', line 112 def initialize(args) if args.is_a?(String) # Match `args' against the regular expression, described below. # # This regular expression matches all values where the first group of # characters is one of either [ 'p', 'q', 'qf', 'sf', 'f' ], which is # parsed as the 'type' of the match. This is followed by an 'm' and a # group of digits, which is parsed as the 'number' of the match. # # In addition, one can specify a 'round number' following the first group # of characters such as in eliminations and finals. Often times, there # are multiple so-called 'rounds' in eliminations, and so the system will # optionally capture that round. # # Also, one can specify a 'replay number' following the match number. # this is done by appending 'r' and a group of digits which is the replay # number. # # Below are listed the match groups and what they are: # # 1: Match type # 2: Round number (optional) # 3: Match number # 4: Replay string (optional) # 5: Replay number (required if #4 is supplied) # # This behavior may change in the future. match_data = args.match(/(p|q|qf|sf|f)([\d]+)?m([\d]+)(r)?([\d]+)?/i) # Whine if we don't have a match (string is incorrectly formatted) raise ArgumentError, 'string is improperly formatted' unless match_data # Check and set required stuff first, everything else later. # Whine if we don't have a match type raise ArgumentError, 'match type must be supplied' unless match_data[1] # Parse the match type string @type = case match_data[1].downcase when 'p' :practice when 'q' :qualification when 'qf' :quarterfinal when 'sf' :semifinal when 'f' :final else raise ArgumentError, 'match type must be in [\'p\', \'q\', \'qf\', \'sf\', \'f\']' end # Whine if we don't have a match number raise ArgumentError, 'match number must be supplied' unless match_data[3] # Parse the match number @number = match_data[3].to_i raise ArgumentError, 'match number must be greater than 0' unless @number > 0 # Parse the round number, if it is present if match_data[2] @round = match_data[2].to_i raise ArgumentError, 'round number must be greater than 0' unless @round > 0 end # Parse replay match group, store replay number if present. @replay_number = match_data[5].to_i if match_data[4] == 'r' elsif args.is_a?(Hash) # type (Symbol or String) # number (Integer) # round (Integer), optional # replay_number (Integer), optional # Convert keys to symbols if needed. args = Hash[args.map { |key, value| [key.to_sym, value] }] raise TypeError, 'type must be a Symbol or String' unless args[:type].is_a?(Symbol) || args[:type].is_a?(String) raise ArgumentError, "type must be in #{POSSIBLE_TYPES.inspect}" unless POSSIBLE_TYPES.include?(args[:type].to_sym) @type = args[:type].to_sym raise TypeError, 'match number must be an Integer' unless args[:number].is_an?(Integer) raise ArgumentError, 'match number must be greater than 0' unless args[:number] > 0 @number = args[:number] if args[:round] raise TypeError, 'round number must be an Integer' unless args[:round].is_an?(Integer) raise ArgumentError, 'round number must be greater than 0' unless args[:round] > 0 @round = args[:round] end if args[:replay_number] raise TypeError, 'replay number must be an Integer' unless args[:replay_number].is_an?(Integer) raise ArgumentError, 'replay number must be greater than 0' unless args[:replay_number] > 0 @replay_number = args[:replay_number] end else raise TypeError, 'argument must be a String or Hash' end end |
Instance Attribute Details
#number ⇒ Object (readonly)
Public: The numerical part of the match number
Examples
match_number = MatchNumber.new('qm2')
match_number.number
# => 2
31 32 33 |
# File 'lib/frecon/match_number.rb', line 31 def number @number end |
#round ⇒ Object (readonly)
Public: The round part of the match number
Examples
match_number = MatchNumber.new('qf1m2r3')
match_number.round
# => 2
40 41 42 |
# File 'lib/frecon/match_number.rb', line 40 def round @round end |
#type ⇒ Object (readonly)
Public: The type of the match.
Examples
match_number = MatchNumber.new('qf1m2r3')
match_number.type
# => :quarterfinal
49 50 51 |
# File 'lib/frecon/match_number.rb', line 49 def type @type end |
Class Method Details
.demongoize(object) ⇒ Object
Public: Convert a stored match number to a MatchNumber object.
object - String representation of a match number (mongoized)
Returns MatchNumber parsed from object.
56 57 58 59 60 61 62 |
# File 'lib/frecon/match_number.rb', line 56 def self.demongoize(object) # `object' should *always* be a string (since MatchNumber#mongoize returns a # String which is what is stored in the database) raise ArgumentError, "`object' must be a String" unless object.is_a?(String) MatchNumber.new(object) end |
.evolve(object) ⇒ Object
Public: Convert a MatchNumber object to a storable string representation for queries.
object - A MatchNumber, String, or Hash. If MatchNumber, run #mongoize on
it. If String, create a new MatchNumber object for it, then run
#mongoize on it. If Hash, convert its keys to symbols, then
pull out the :alliance and :number keys to generate a
MatchNumber.
Returns String containing the mongo-ready value for the representation.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/frecon/match_number.rb', line 94 def self.evolve(object) case object when MatchNumber object.mongoize when String, Hash MatchNumber.new(object).mongoize else object end end |
.mongoize(object) ⇒ Object
Public: Convert a MatchNumber object to a storable string representation.
object - A MatchNumber, String, or Hash. If MatchNumber, run #mongoize on
it. If String, create a new MatchNumber object for it, then run
#mongoize on it. If Hash, convert its keys to symbols, then
pull out the :alliance and :number keys to generate a
MatchNumber.
Returns String containing the mongo-ready value for the representation.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/frecon/match_number.rb', line 73 def self.mongoize(object) case object when MatchNumber object.mongoize when String, Hash MatchNumber.new(object).mongoize else object end end |
Instance Method Details
#elimination? ⇒ Boolean
Public: Determine if MatchNumber represents a match of any elimination type.
271 272 273 |
# File 'lib/frecon/match_number.rb', line 271 def elimination? ELIMINATION_TYPES.include?(@type) end |
#final? ⇒ Boolean
Public: Determine if MatchNumber represents a final match.
265 266 267 |
# File 'lib/frecon/match_number.rb', line 265 def final? @type == :final end |
#mongoize ⇒ Object
Public: Convert to a storable string representation.
Returns String representing the MatchNumber’s data.
108 109 110 |
# File 'lib/frecon/match_number.rb', line 108 def mongoize to_s end |
#practice? ⇒ Boolean
Public: Determine if MatchNumber represents a practice match.
245 246 247 |
# File 'lib/frecon/match_number.rb', line 245 def practice? @type == :practice end |
#qualification? ⇒ Boolean
Public: Determine if MatchNumber represents a qualification match.
250 251 252 |
# File 'lib/frecon/match_number.rb', line 250 def qualification? @type == :qualification end |
#quarterfinal? ⇒ Boolean
Public: Determine if MatchNumber represents a quarterfinal match.
255 256 257 |
# File 'lib/frecon/match_number.rb', line 255 def quarterfinal? @type == :quarterfinal end |
#replay? ⇒ Boolean
Public: Determine if MatchNumber represents a replay.
240 241 242 |
# File 'lib/frecon/match_number.rb', line 240 def replay? !@replay_number.nil? && @replay_number > 0 end |
#semifinal? ⇒ Boolean
Public: Determine if MatchNumber represents a semifinal match.
260 261 262 |
# File 'lib/frecon/match_number.rb', line 260 def semifinal? @type == :semifinal end |
#to_s ⇒ Object
Public: Convert to a String.
Returns String representing the match number data.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/frecon/match_number.rb', line 220 def to_s type_string = case @type when :practice 'p' when :qualification 'q' when :quarterfinal 'qf' when :semifinal 'sf' when :final 'f' end match_string = "m#{@number}" replay_string = "r#{@replay_number}" if replay? "#{type_string}#{@round}#{match_string}#{replay_string}" end |