Class: PostRunner::ActivitiesDB
- Inherits:
-
Object
- Object
- PostRunner::ActivitiesDB
- Includes:
- Fit4Ruby::Converters
- Defined in:
- lib/postrunner/ActivitiesDB.rb
Instance Attribute Summary collapse
-
#db_dir ⇒ Object
readonly
Returns the value of attribute db_dir.
-
#fit_dir ⇒ Object
readonly
Returns the value of attribute fit_dir.
Instance Method Summary collapse
- #activity_by_fit_file(fit_file) ⇒ Object
- #add(fit_file) ⇒ Object
- #check ⇒ Object
- #delete(activity) ⇒ Object
- #find(query) ⇒ Object
-
#initialize(db_dir) ⇒ ActivitiesDB
constructor
A new instance of ActivitiesDB.
- #list ⇒ Object
- #map_to_files(query) ⇒ Object
- #ref_by_fit_file(fit_file) ⇒ Object
- #rename(activity, name) ⇒ Object
- #show_records ⇒ Object
Constructor Details
#initialize(db_dir) ⇒ ActivitiesDB
Returns a new instance of ActivitiesDB.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/postrunner/ActivitiesDB.rb', line 17 def initialize(db_dir) @db_dir = db_dir @fit_dir = File.join(@db_dir, 'fit') @archive_file = File.join(@db_dir, 'archive.yml') create_directories begin if File.exists?(@archive_file) @activities = YAML.load_file(@archive_file) else @activities = [] end rescue StandardError Log.fatal "Cannot load archive file '#{@archive_file}': #{$!}" end unless @activities.is_a?(Array) Log.fatal "The archive file '#{@archive_file}' is corrupted" end # The reference to this object is needed inside Activity object but is # not stored in the archive file. We have to retrofit the Activity # instances with this data. @activities.each do |a| a.db = self end @records = PersonalRecords.new(self) end |
Instance Attribute Details
#db_dir ⇒ Object (readonly)
Returns the value of attribute db_dir.
15 16 17 |
# File 'lib/postrunner/ActivitiesDB.rb', line 15 def db_dir @db_dir end |
#fit_dir ⇒ Object (readonly)
Returns the value of attribute fit_dir.
15 16 17 |
# File 'lib/postrunner/ActivitiesDB.rb', line 15 def fit_dir @fit_dir end |
Instance Method Details
#activity_by_fit_file(fit_file) ⇒ Object
110 111 112 |
# File 'lib/postrunner/ActivitiesDB.rb', line 110 def activity_by_fit_file(fit_file) @activities.find { |a| a.fit_file == fit_file } end |
#add(fit_file) ⇒ Object
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 79 80 81 82 83 84 |
# File 'lib/postrunner/ActivitiesDB.rb', line 47 def add(fit_file) base_fit_file = File.basename(fit_file) if @activities.find { |a| a.fit_file == base_fit_file } Log.debug "Activity #{fit_file} is already included in the archive" return false end if File.exists?(File.join(@fit_dir, base_fit_file)) Log.debug "Activity #{fit_file} has been deleted before" return false end begin fit_activity = Fit4Ruby.read(fit_file) rescue Fit4Ruby::Error Log.error $! return false end begin FileUtils.cp(fit_file, @fit_dir) rescue StandardError Log.fatal "Cannot copy #{fit_file} into #{@fit_dir}: #{$!}" end @activities << (activity = Activity.new(self, base_fit_file, fit_activity)) @activities.sort! do |a1, a2| a2. <=> a1. end activity.register_records(@records) sync Log.info "#{fit_file} successfully added to archive" true end |
#check ⇒ Object
96 97 98 |
# File 'lib/postrunner/ActivitiesDB.rb', line 96 def check @activities.each { |a| a.check } end |
#delete(activity) ⇒ Object
86 87 88 89 |
# File 'lib/postrunner/ActivitiesDB.rb', line 86 def delete(activity) @activities.delete(activity) sync end |
#find(query) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/postrunner/ActivitiesDB.rb', line 114 def find(query) case query when /\A-?\d+$\z/ index = query.to_i # The UI counts the activities from 1 to N. Ruby counts from 0 - # (N-1). index -= 1 if index > 0 if (a = @activities[index]) return [ a ] end when /\A-?\d+--?\d+\z/ idxs = query.match(/(?<sidx>-?\d+)-(?<eidx>-?[0-9]+)/) sidx = idxs['sidx'].to_i eidx = idxs['eidx'].to_i # The UI counts the activities from 1 to N. Ruby counts from 0 - # (N-1). sidx -= 1 if sidx > 0 eidx -= 1 if eidx > 0 unless (as = @activities[sidx..eidx]).empty? return as end else Log.error "Invalid activity query: #{query}" end [] end |
#list ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/postrunner/ActivitiesDB.rb', line 174 def list i = 0 t = FlexiTable.new t.head t.row(%w( Ref. Activity Start Distance Duration Pace ), { :halign => :left }) t.set_column_attributes([ { :halign => :right }, {}, {}, { :halign => :right }, { :halign => :right }, { :halign => :right } ]) t.body @activities.each do |a| t.row([ i += 1, a.name[0..19], a..strftime("%a, %Y %b %d %H:%M"), "%.2f" % (a.total_distance / 1000), secsToHMS(a.total_timer_time), speedToPace(a.avg_speed) ]) end puts t.to_s end |
#map_to_files(query) ⇒ Object
142 143 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 |
# File 'lib/postrunner/ActivitiesDB.rb', line 142 def map_to_files(query) case query when /\A-?\d+$\z/ index = query.to_i # The UI counts the activities from 1 to N. Ruby counts from 0 - # (N-1). index -= 1 if index > 0 if (a = @activities[index]) return [ File.join(@fit_dir, a.fit_file) ] end when /\A-?\d+--?\d+\z/ idxs = query.match(/(?<sidx>-?\d+)-(?<eidx>-?[0-9]+)/) sidx = idxs['sidx'].to_i eidx = idxs['eidx'].to_i # The UI counts the activities from 1 to N. Ruby counts from 0 - # (N-1). sidx -= 1 if sidx > 0 eidx -= 1 if eidx > 0 unless (as = @activities[sidx..eidx]).empty? files = [] as.each do |a| files << File.join(@fit_dir, a.fit_file) end return files end else Log.error "Invalid activity query: #{query}" end [] end |
#ref_by_fit_file(fit_file) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/postrunner/ActivitiesDB.rb', line 100 def ref_by_fit_file(fit_file) i = 1 @activities.each do |activity| return i if activity.fit_file == fit_file i += 1 end nil end |
#rename(activity, name) ⇒ Object
91 92 93 94 |
# File 'lib/postrunner/ActivitiesDB.rb', line 91 def rename(activity, name) activity.rename(name) sync end |
#show_records ⇒ Object
200 201 202 |
# File 'lib/postrunner/ActivitiesDB.rb', line 200 def show_records puts @records.to_s end |