Class: FReCon::MatchNumber

Inherits:
Object show all
Defined in:
lib/frecon/match_number.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ MatchNumber

Returns a new instance of MatchNumber.



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
216
# File 'lib/frecon/match_number.rb', line 113

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

#numberObject (readonly)

Public: The numerical part of the match number

Examples

match_number = MatchNumber.new('qm2')
match_number.number
# => 2


32
33
34
# File 'lib/frecon/match_number.rb', line 32

def number
  @number
end

#roundObject (readonly)

Public: The round part of the match number

Examples

match_number = MatchNumber.new('qf1m2r3')
match_number.round
# => 2


41
42
43
# File 'lib/frecon/match_number.rb', line 41

def round
  @round
end

#typeObject (readonly)

Public: The type of the match.

Examples

match_number = MatchNumber.new('qf1m2r3')
match_number.type
# => :quarterfinal


50
51
52
# File 'lib/frecon/match_number.rb', line 50

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.

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
# File 'lib/frecon/match_number.rb', line 57

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.



95
96
97
98
99
100
101
102
103
104
# File 'lib/frecon/match_number.rb', line 95

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.



74
75
76
77
78
79
80
81
82
83
# File 'lib/frecon/match_number.rb', line 74

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.

Returns:

  • (Boolean)


272
273
274
# File 'lib/frecon/match_number.rb', line 272

def elimination?
	ELIMINATION_TYPES.include?(@type)
end

#final?Boolean

Public: Determine if MatchNumber represents a final match.

Returns:

  • (Boolean)


266
267
268
# File 'lib/frecon/match_number.rb', line 266

def final?
	@type == :final
end

#mongoizeObject

Public: Convert to a storable string representation.

Returns String representing the MatchNumber’s data.



109
110
111
# File 'lib/frecon/match_number.rb', line 109

def mongoize
	to_s
end

#practice?Boolean

Public: Determine if MatchNumber represents a practice match.

Returns:

  • (Boolean)


246
247
248
# File 'lib/frecon/match_number.rb', line 246

def practice?
	@type == :practice
end

#qualification?Boolean

Public: Determine if MatchNumber represents a qualification match.

Returns:

  • (Boolean)


251
252
253
# File 'lib/frecon/match_number.rb', line 251

def qualification?
	@type == :qualification
end

#quarterfinal?Boolean

Public: Determine if MatchNumber represents a quarterfinal match.

Returns:

  • (Boolean)


256
257
258
# File 'lib/frecon/match_number.rb', line 256

def quarterfinal?
	@type == :quarterfinal
end

#replay?Boolean

Public: Determine if MatchNumber represents a replay.

Returns:

  • (Boolean)


241
242
243
# File 'lib/frecon/match_number.rb', line 241

def replay?
	!@replay_number.nil? && @replay_number > 0
end

#semifinal?Boolean

Public: Determine if MatchNumber represents a semifinal match.

Returns:

  • (Boolean)


261
262
263
# File 'lib/frecon/match_number.rb', line 261

def semifinal?
	@type == :semifinal
end

#to_sObject

Public: Convert to a String.

Returns String representing the match number data.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/frecon/match_number.rb', line 221

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