Class: RaceInstance

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/race_instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#checkpoint_timesObject

Returns the value of attribute checkpoint_times.



4
5
6
# File 'app/models/race_instance.rb', line 4

def checkpoint_times
  @checkpoint_times
end

#performances_countObject

Returns the value of attribute performances_count.



4
5
6
# File 'app/models/race_instance.rb', line 4

def performances_count
  @performances_count
end

Instance Method Details

#as_json(options = {}) ⇒ Object



150
151
152
153
154
155
# File 'app/models/race_instance.rb', line 150

def as_json(options={})
  json = {
    :name => full_name,
    :performances => performances.map(&:as_json)
  }
end

#assembled_checkpoint_timesObject



83
84
85
86
87
88
89
90
91
92
# File 'app/models/race_instance.rb', line 83

def assembled_checkpoint_times
  unless @checkpoint_times
    @checkpoint_times = {}
    RaceCheckpointTime.in(self).with_context.each do |cpt|
      @checkpoint_times[cpt.performance.id] ||= {}
      @checkpoint_times[cpt.performance.id][cpt.checkpoint.id] = cpt.elapsed_time
    end
  end
  @checkpoint_times
end

#category_present?(category) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'app/models/race_instance.rb', line 125

def category_present?(category)
  categories.include?(category)
end

#category_top(category, count = 5) ⇒ Object



121
122
123
# File 'app/models/race_instance.rb', line 121

def category_top(category, count=5)
  performances.eligible_for_category(category).top(count) if category_present?(category)
end

#checkpointsObject



74
75
76
77
# File 'app/models/race_instance.rb', line 74

def checkpoints
  # we will need to accommodate races whose checkpoints are different with each instance, but for now:
  race.checkpoints
end

#fastest_checkpoint_legsObject



137
138
139
# File 'app/models/race_instance.rb', line 137

def fastest_checkpoint_legs
  @fastest_legs ||= self.checkpoints.map{|cp| cp.fastest_leg(self) }
end

#fastest_checkpoint_timesObject



133
134
135
# File 'app/models/race_instance.rb', line 133

def fastest_checkpoint_times
  @leading ||= self.checkpoints.map{|cp| cp.fastest_time(self) }
end

#full_nameObject



41
42
43
# File 'app/models/race_instance.rb', line 41

def full_name
  "#{race.name} #{name}"
end

#future?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/race_instance.rb', line 66

def future?
  started_at > Time.now
end

#has_results?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/race_instance.rb', line 70

def has_results?
  performances.any?
end

#median_checkpoint_legsObject



141
142
143
# File 'app/models/race_instance.rb', line 141

def median_checkpoint_legs
  @median_legs ||= self.checkpoints.map{|cp| cp.median_leg(self) }
end

#median_checkpoint_timesObject



145
146
147
148
# File 'app/models/race_instance.rb', line 145

def median_checkpoint_times
  carry = 0
  @medians ||= self.checkpoints.map{|cp| cp.median_time(self) }
end

#nice_start_dateObject



58
59
60
# File 'app/models/race_instance.rb', line 58

def nice_start_date
  started_at.to_datetime.strftime("%B %e %Y") if started_at
end

#nice_start_timeObject



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

def nice_start_time
  return unless started_at
  if started_at.min == 0
    started_at.to_datetime.strftime("%-1I%p").downcase
  else
    started_at.to_datetime.strftime("%-1I:%M%p").downcase
  end
end

#past?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/race_instance.rb', line 62

def past?
  started_at <= Time.now
end

#pathObject



45
46
47
# File 'app/models/race_instance.rb', line 45

def path
  "#{race.slug}/#{slug}"
end

#performance_at(position) ⇒ Object



98
99
100
# File 'app/models/race_instance.rb', line 98

def performance_at(position)
  
end

#performance_by(competitor) ⇒ Object



94
95
96
# File 'app/models/race_instance.rb', line 94

def performance_by(competitor)
  
end

#splits_available?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/race_instance.rb', line 79

def splits_available?
  RaceCheckpointTime.in(self).other_than_finish.any?
end

#to_paramObject



37
38
39
# File 'app/models/race_instance.rb', line 37

def to_param
  slug
end

#top(count = 20) ⇒ Object



117
118
119
# File 'app/models/race_instance.rb', line 117

def top(count=20)
  performances.top(count)
end

#total_runnersObject



129
130
131
# File 'app/models/race_instance.rb', line 129

def total_runners
  @performances_count ||= performances.count
end

#winner(category = nil) ⇒ Object



111
112
113
114
115
# File 'app/models/race_instance.rb', line 111

def winner(category=nil)
  if perf = winning_performance(category)
    perf.competitor
  end
end

#winning_performance(category = nil) ⇒ Object



102
103
104
105
106
107
108
109
# File 'app/models/race_instance.rb', line 102

def winning_performance(category=nil)
  if category
    category = RaceCategory.find_by_name(category) unless category.is_a? RaceCategory
    performances.completed.eligible_for_category(category).first if category_present?(category)
  else
    performances.completed.first
  end
end