Class: PostRunner::PersonalRecords::RecordSet

Inherits:
PEROBS::Object
  • Object
show all
Includes:
Fit4Ruby::Converters
Defined in:
lib/postrunner/PersonalRecords.rb

Instance Method Summary collapse

Constructor Details

#initialize(p, sport, year) ⇒ RecordSet

Returns a new instance of RecordSet.



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/postrunner/PersonalRecords.rb', line 130

def initialize(p, sport, year)
  super(p)

  self.sport = sport
  self.year = year
  self.distance_record = nil
  self.speed_records = @store.new(PEROBS::Hash)
  if sport
    PersonalRecords::SpeedRecordDistances[sport].each_key do |dist|
      @speed_records[dist.to_s] = nil
    end
  end
end

Instance Method Details

#delete_activity(activity) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/postrunner/PersonalRecords.rb', line 177

def delete_activity(activity)
  record_deleted = false
  if @distance_record && @distance_record.activity == activity
    self.distance_record = nil
    record_deleted = true
  end
  PersonalRecords::SpeedRecordDistances[@sport].each_key do |dist|
    dist = dist.to_s
    if @speed_records[dist] && @speed_records[dist].activity == activity
      @speed_records[dist] = nil
      record_deleted = true
    end
  end

  record_deleted
end

#each {|@distance_record| ... } ⇒ Object

Iterator for all Record objects that are stored in this data structure.

Yields:

  • (@distance_record)


203
204
205
206
207
208
# File 'lib/postrunner/PersonalRecords.rb', line 203

def each(&block)
  yield(@distance_record) if @distance_record
  @speed_records.each_value do |record|
    yield(record) if record
  end
end

#empty?Boolean

Return true if no Record is stored in this RecordSet object.

Returns:

  • (Boolean)


195
196
197
198
199
200
# File 'lib/postrunner/PersonalRecords.rb', line 195

def empty?
  return false if @distance_record
  @speed_records.each_value { |r| return false if r }

  true
end

#register_result(result) ⇒ Object



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
# File 'lib/postrunner/PersonalRecords.rb', line 144

def register_result(result)
  if result.duration
    # We have a potential speed record for a known distance.
    unless PersonalRecords::SpeedRecordDistances[@sport].
           include?(result.distance)
      Log.fatal "Unknown record distance #{result.distance}"
    end

    old_record = @speed_records[result.distance.to_s]
    if old_record.nil? || old_record.duration > result.duration
      @speed_records[result.distance.to_s] = @store.new(Record, result)
      Log.info "New #{@year ? @year.to_s : 'all-time'} " +
               "#{result.sport} speed record for " +
               "#{PersonalRecords::SpeedRecordDistances[@sport][
                  result.distance]}: " +
               "#{secsToHMS(result.duration)}"
      return true
    end
  else
    # We have a potential distance record.
    if @distance_record.nil? ||
       @distance_record.distance < result.distance
      self.distance_record = @store.new(Record, result)
      raise RuntimeError if @distance_record.is_a?(String)
      Log.info "New #{@year ? @year.to_s : 'all-time'} " +
               "#{result.sport} distance record: #{result.distance} m"
      return true
    end
  end

  false
end

#to_html(doc) ⇒ Object



216
217
218
# File 'lib/postrunner/PersonalRecords.rb', line 216

def to_html(doc)
  generate_table.to_html(doc)
end

#to_sObject



210
211
212
213
214
# File 'lib/postrunner/PersonalRecords.rb', line 210

def to_s
  return '' if empty?

  generate_table.to_s + "\n"
end