Class: PostRunner::PersonalRecords::SportRecords
- Inherits:
-
Object
- Object
- PostRunner::PersonalRecords::SportRecords
- Defined in:
- lib/postrunner/PersonalRecords.rb
Instance Attribute Summary collapse
-
#all_time ⇒ Object
readonly
Returns the value of attribute all_time.
-
#sport ⇒ Object
readonly
Returns the value of attribute sport.
-
#yearly ⇒ Object
readonly
Returns the value of attribute yearly.
Instance Method Summary collapse
- #delete_activity(activity) ⇒ Object
-
#each(&block) ⇒ Object
Iterator for all Record objects that are stored in this data structure.
-
#empty? ⇒ Boolean
Return true if no record is stored in this SportRecords object.
-
#initialize(sport) ⇒ SportRecords
constructor
A new instance of SportRecords.
- #register_result(result) ⇒ Object
- #to_html(doc) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(sport) ⇒ SportRecords
Returns a new instance of SportRecords.
230 231 232 233 234 |
# File 'lib/postrunner/PersonalRecords.rb', line 230 def initialize(sport) @sport = sport @all_time = RecordSet.new(@sport, nil) @yearly = {} end |
Instance Attribute Details
#all_time ⇒ Object (readonly)
Returns the value of attribute all_time.
228 229 230 |
# File 'lib/postrunner/PersonalRecords.rb', line 228 def all_time @all_time end |
#sport ⇒ Object (readonly)
Returns the value of attribute sport.
228 229 230 |
# File 'lib/postrunner/PersonalRecords.rb', line 228 def sport @sport end |
#yearly ⇒ Object (readonly)
Returns the value of attribute yearly.
228 229 230 |
# File 'lib/postrunner/PersonalRecords.rb', line 228 def yearly @yearly end |
Instance Method Details
#delete_activity(activity) ⇒ Object
248 249 250 251 252 |
# File 'lib/postrunner/PersonalRecords.rb', line 248 def delete_activity(activity) ([ @all_time ] + @yearly.values).each do |r| r.delete_activity(activity) end end |
#each(&block) ⇒ Object
Iterator for all Record objects that are stored in this data structure.
263 264 265 266 267 |
# File 'lib/postrunner/PersonalRecords.rb', line 263 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.
255 256 257 258 259 260 |
# File 'lib/postrunner/PersonalRecords.rb', line 255 def empty? return false unless @all_time.empty? @yearly.each_value { |r| return false unless r.empty? } true end |
#register_result(result) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/postrunner/PersonalRecords.rb', line 236 def register_result(result) year = result.start_time.year unless @yearly[year] @yearly[year] = RecordSet.new(@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
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/postrunner/PersonalRecords.rb', line 282 def to_html(doc) return nil if empty? doc.div { doc.h3('All-time records') @all_time.to_html(doc) @yearly.values.sort{ |r1, r2| r2.year <=> r1.year }.each do |record| puts record.year unless record.empty? doc.h3("Records of #{record.year}") record.to_html(doc) end end } end |
#to_s ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/postrunner/PersonalRecords.rb', line 269 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 |