Class: PostRunner::PersonalRecords::SportRecords

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

Instance Method Summary collapse

Constructor Details

#initialize(p, sport) ⇒ SportRecords

Returns a new instance of SportRecords.



255
256
257
258
259
260
261
# File 'lib/postrunner/PersonalRecords.rb', line 255

def initialize(p, sport)
  super(p)

  self.sport = sport
  self.all_time = @store.new(RecordSet, sport, nil)
  self.yearly = @store.new(PEROBS::Hash)
end

Instance Method Details

#delete_activity(activity) ⇒ Object



275
276
277
278
279
280
281
282
# File 'lib/postrunner/PersonalRecords.rb', line 275

def delete_activity(activity)
  record_deleted = false
  ([ @all_time ] + @yearly.values).each do |r|
    record_deleted = true if r.delete_activity(activity)
  end

  record_deleted
end

#each(&block) ⇒ Object

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



293
294
295
296
297
# File 'lib/postrunner/PersonalRecords.rb', line 293

def each(&block)
  records = @yearly.values
  records << @all_time if @all_time
  records.each { |r| r.each(&block) }
end

#empty?Boolean

Return true if no record is stored in this SportRecords object.

Returns:

  • (Boolean)


285
286
287
288
289
290
# File 'lib/postrunner/PersonalRecords.rb', line 285

def empty?
  return false unless @all_time.empty?
  @yearly.each_value { |r| return false unless r.empty? }

  true
end

#register_result(result) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/postrunner/PersonalRecords.rb', line 263

def register_result(result)
  year = result.start_time.year.to_s
  unless @yearly[year]
    @yearly[year] = @store.new(RecordSet, @sport, year)
  end

  new_at = @all_time.register_result(result)
  new_yr = @yearly[year].register_result(result)

  new_at || new_yr
end

#to_html(doc) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/postrunner/PersonalRecords.rb', line 312

def to_html(doc)
  return nil if empty?

  doc.div {
    doc.h3('All-time records')
    @all_time.to_html(doc)
    @yearly.values.sort do |r1, r2|
      r2.year.to_i <=> r1.year.to_i
    end.each do |record|
      unless record.empty?
        doc.h3("Records of #{record.year}")
        record.to_html(doc)
      end
    end
  }
end

#to_sObject



299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/postrunner/PersonalRecords.rb', line 299

def to_s
  return '' if empty?

  str = "All-time records:\n\n#{@all_time.to_s}" unless @all_time.empty?
  @yearly.values.sort{ |r1, r2| r2.year <=> r1.year }.each do |record|
    unless record.empty?
      str += "Records of #{record.year}:\n\n#{record.to_s}"
    end
  end

  str
end