class PostRunner::PersonalRecords::RecordSet

Public Class Methods

new(p, sport, year) click to toggle source
Calls superclass method
# 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

Public Instance Methods

delete_activity(activity) click to toggle source
# 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| ... } click to toggle source

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

# 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?() click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# File lib/postrunner/PersonalRecords.rb, line 216
def to_html(doc)
  generate_table.to_html(doc)
end
to_s() click to toggle source
# File lib/postrunner/PersonalRecords.rb, line 210
def to_s
  return '' if empty?

  generate_table.to_s + "\n"
end

Private Instance Methods

generate_table() click to toggle source
# File lib/postrunner/PersonalRecords.rb, line 222
def generate_table
  t = FlexiTable.new
  t.head
  t.row([ 'Record', 'Time/Dist.',
          @sport == 'running' ? 'Avg. Pace' : 'Avg. Speed',
          'Ref.', 'Activity',
          'Date' ],
        { :halign => :center })
  t.set_column_attributes([
    {},
    { :halign => :right },
    { :halign => :right },
    { :halign => :right },
    { :halign => :left },
    { :halign => :left }
  ])
  t.body

  records = @speed_records.values.delete_if { |r| r.nil? }.
            sort { |r1, r2| r1.distance <=> r2.distance }
  records << @distance_record if @distance_record

  records.each { |r| r.to_table_row(t) }

  t
end