Class: RacePerformanceStatus

Inherits:
Object
  • Object
show all
Defined in:
app/models/race_performance_status.rb

Constant Summary collapse

@@statuses =
[
  RacePerformanceStatus.new(:id => 0,   :name => '?',   :aliases => ['unknown'] ),
  RacePerformanceStatus.new(:id => 10,  :name => 'Ret', :aliases => ['retired', 'r', 'ret']),
  RacePerformanceStatus.new(:id => 20,  :name => 'DNF', :aliases => ['did not finish', 'x', 'dnf']),
  RacePerformanceStatus.new(:id => 30,  :name => 'TO',  :aliases => ['timed out', 't', 'to']),
  RacePerformanceStatus.new(:id => 50,  :name => 'Dsq', :aliases => ['disqualified', 'disq', 'd', 'dsq']),
  RacePerformanceStatus.new(:id => 100, :name => 'Finished')
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RacePerformanceStatus

Returns a new instance of RacePerformanceStatus.



4
5
6
7
8
# File 'app/models/race_performance_status.rb', line 4

def initialize(options = {})
  options = options.symbolize_keys
  @id, @name = options[:id], options[:name]
  @aliases = (options[:aliases] || []).collect {|name| name.to_s.downcase.intern}
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



2
3
4
# File 'app/models/race_performance_status.rb', line 2

def aliases
  @aliases
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'app/models/race_performance_status.rb', line 2

def id
  @id
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'app/models/race_performance_status.rb', line 2

def name
  @name
end

Class Method Details

.[](value = "") ⇒ Object



26
27
28
29
# File 'app/models/race_performance_status.rb', line 26

def self.[](value="")
  value = "?" if value.blank? || value == 0
  @@statuses.find { |status| status.symbol == value.to_s.downcase.intern || status.has_alias?(value) }
end

.find(id = 0) ⇒ Object



31
32
33
# File 'app/models/race_performance_status.rb', line 31

def self.find(id=0)
  @@statuses.find { |status| status.id.to_s == id.to_s }
end

.find_allObject



35
36
37
# File 'app/models/race_performance_status.rb', line 35

def self.find_all
  @@statuses.dup
end

.from_pos_or_time(pos = "", time = "") ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'app/models/race_performance_status.rb', line 39

def self.from_pos_or_time(pos="",time="")
  if self[pos]
    self[pos]
  elsif pos.looks_like_number?
    self["Finished"]
  else
    self.from_time(time)
  end
end

.from_time(time = "") ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/models/race_performance_status.rb', line 49

def self.from_time(time="")
  if time.seconds > 0   # String.seconds returns 0 for a string that doesn't parse. See DurationExtensions for method.
    self["Finished"] 
  elsif self[time]
    self[time]
  else
    self["?"]
  end
end

Instance Method Details

#has_alias?(value) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/models/race_performance_status.rb', line 14

def has_alias?(value)
  @aliases.include?(value.to_s.downcase.intern)
end

#symbolObject



10
11
12
# File 'app/models/race_performance_status.rb', line 10

def symbol
  @name.to_s.downcase.intern
end

#to_iObject



22
23
24
# File 'app/models/race_performance_status.rb', line 22

def to_i
  @id
end

#to_sObject



18
19
20
# File 'app/models/race_performance_status.rb', line 18

def to_s
  @name
end