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.
219 220 221 222 223 |
# File 'lib/postrunner/PersonalRecords.rb', line 219 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.
217 218 219 |
# File 'lib/postrunner/PersonalRecords.rb', line 217 def all_time @all_time end |
#sport ⇒ Object (readonly)
Returns the value of attribute sport.
217 218 219 |
# File 'lib/postrunner/PersonalRecords.rb', line 217 def sport @sport end |
#yearly ⇒ Object (readonly)
Returns the value of attribute yearly.
217 218 219 |
# File 'lib/postrunner/PersonalRecords.rb', line 217 def yearly @yearly end |
Instance Method Details
#delete_activity(activity) ⇒ Object
237 238 239 240 241 |
# File 'lib/postrunner/PersonalRecords.rb', line 237 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.
252 253 254 255 256 |
# File 'lib/postrunner/PersonalRecords.rb', line 252 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.
244 245 246 247 248 249 |
# File 'lib/postrunner/PersonalRecords.rb', line 244 def empty? return false unless @all_time.empty? @yearly.each_value { |r| return false unless r.empty? } true end |
#register_result(result) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/postrunner/PersonalRecords.rb', line 225 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
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/postrunner/PersonalRecords.rb', line 271 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
258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/postrunner/PersonalRecords.rb', line 258 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 |