Class: FReCon::MatchNumber

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

Constant Summary collapse

POSSIBLE_TYPES =
[:practice, :qualification, :quarterfinal, :semifinal, :final]
ELIMINATION_TYPES =
[: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.



54
55
56
57
58
59
60
61
62
63
64
65
66
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
# File 'lib/frecon/match_number.rb', line 54

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)

Returns the value of attribute number.



17
18
19
# File 'lib/frecon/match_number.rb', line 17

def number
  @number
end

#roundObject (readonly)

Returns the value of attribute round.



17
18
19
# File 'lib/frecon/match_number.rb', line 17

def round
  @round
end

Class Method Details

.demongoize(object) ⇒ Object

MongoDB compatibility methods.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
# File 'lib/frecon/match_number.rb', line 20

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/frecon/match_number.rb', line 39

def self.evolve(object)
  case object
  when MatchNumber
    object.mongoize
  when String, Hash
    MatchNumber.new(object).mongoize
  else
    object
  end
end

.mongoize(object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/frecon/match_number.rb', line 28

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

Returns:

  • (Boolean)


202
203
204
# File 'lib/frecon/match_number.rb', line 202

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

#final?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/frecon/match_number.rb', line 198

def final?
  @type == :final
end

#mongoizeObject



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

def mongoize
  to_s
end

#practice?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/frecon/match_number.rb', line 182

def practice?
  @type == :practice
end

#qualification?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/frecon/match_number.rb', line 186

def qualification?
  @type == :qualification
end

#quarterfinal?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/frecon/match_number.rb', line 190

def quarterfinal?
  @type == :quarterfinal
end

#replay?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/frecon/match_number.rb', line 178

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

#semifinal?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/frecon/match_number.rb', line 194

def semifinal?
  @type == :semifinal
end

#to_sObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/frecon/match_number.rb', line 159

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