Class: PostRunner::PersonalRecords

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

Defined Under Namespace

Classes: Record

Instance Method Summary collapse

Constructor Details

#initialize(activities) ⇒ PersonalRecords

Returns a new instance of PersonalRecords.



25
26
27
28
29
30
31
32
# File 'lib/postrunner/PersonalRecords.rb', line 25

def initialize(activities)
  @activities = activities
  @db_dir = activities.db_dir
  @records_file = File.join(@db_dir, 'records.yml')
  @records = []

  load_records
end

Instance Method Details

#delete_activity(fit_file) ⇒ Object



80
81
82
# File 'lib/postrunner/PersonalRecords.rb', line 80

def delete_activity(fit_file)
  @records.delete_if { |r| r.fit_file == fit_file }
end

#register_result(distance, duration, start_time, fit_file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/postrunner/PersonalRecords.rb', line 34

def register_result(distance, duration, start_time, fit_file)
  @records.each do |record|
    if record.duration > 0
      if duration > 0
        # This is a speed record for a popular distance.
        if distance == record.distance
          if duration < record.duration
            record.duration = duration
            record.start_time = start_time
            record.fit_file = fit_file
            Log.info "New record for #{distance} m in " +
                     "#{secsToHMS(duration)}"
            return true
          else
            # No new record for this distance.
            return false
          end
        end
      end
    else
      if distance > record.distance
        # This is a new distance record.
        record.distance = distance
        record.duration = 0
        record.start_time = start_time
        record.fit_file = fit_file
        Log.info "New distance record #{distance} m"
        return true
      else
        # No new distance record.
        return false
      end
    end
  end

  # We have not found a record.
  @records << Record.new(distance, duration, start_time, fit_file)
  if duration == 0
    Log.info "New distance record #{distance} m"
  else
    Log.info "New record for #{distance}m in #{secsToHMS(duration)}"
  end

  true
end

#syncObject



84
85
86
# File 'lib/postrunner/PersonalRecords.rb', line 84

def sync
  save_records
end

#to_sObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/postrunner/PersonalRecords.rb', line 88

def to_s
  record_names = { 1000.0 => '1 km', 1609.0 => '1 mi', 5000.0 => '5 km',
                   21097.5 => '1/2 Marathon',  42195.0 => 'Marathon' }
  t = FlexiTable.new
  t.head
  t.row([ 'Record', 'Time/Dist.', 'Avg. Pace', 'Ref.', 'Activity', 'Date' ],
        { :halign => :center })
  t.set_column_attributes([
    {},
    { :halign => :right },
    { :halign => :right },
    { :halign => :right },
    { :halign => :right },
    { :halign => :left }
  ])
  t.body
  @records.sort { |r1, r2| r1.distance <=> r2.distance }.each do |r|
    activity = @activities.activity_by_fit_file(r.fit_file)
    t.row((r.duration == 0 ?
           [ 'Longest Run', '%.1f m' % r.distance, '-' ] :
           [ record_names[r.distance], secsToHMS(r.duration),
            speedToPace(r.distance / r.duration) ]) +
          [ @activities.ref_by_fit_file(r.fit_file),
            activity.name, r.start_time.strftime("%Y-%m-%d") ])
  end
  t.to_s
end