Class: Tempo::Views::ViewRecords::Duration
- Inherits:
-
Object
- Object
- Tempo::Views::ViewRecords::Duration
- Defined in:
- lib/tempo/views/view_records/base.rb
Overview
Specifically for managing a time duration, nested in other view records. This can be used with a start and end time, or used to manage a sum of times.
Total duration is stored in seconds.
Duration records can be further queried for hours and minutes in order to construct a human redable duration. This can be used to construct time as ##hours:##minutes Hours returns the total whole hours, minutes returns the remaining whole minutes after the hours have been removed from the total.
Instance Attribute Summary collapse
-
#total ⇒ Object
Returns the value of attribute total.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #add(seconds) ⇒ Object
- #format(&block) ⇒ Object
- #hours ⇒ Object
-
#initialize(seconds = 0) ⇒ Duration
constructor
A new instance of Duration.
- #minutes ⇒ Object
- #subtract(seconds) ⇒ Object
Constructor Details
#initialize(seconds = 0) ⇒ Duration
Returns a new instance of Duration.
88 89 90 91 |
# File 'lib/tempo/views/view_records/base.rb', line 88 def initialize seconds=0 @type = "duration" @total = seconds end |
Instance Attribute Details
#total ⇒ Object
Returns the value of attribute total.
86 87 88 |
# File 'lib/tempo/views/view_records/base.rb', line 86 def total @total end |
#type ⇒ Object
Returns the value of attribute type.
86 87 88 |
# File 'lib/tempo/views/view_records/base.rb', line 86 def type @type end |
Instance Method Details
#add(seconds) ⇒ Object
100 101 102 |
# File 'lib/tempo/views/view_records/base.rb', line 100 def add(seconds) @total += seconds end |
#format(&block) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/tempo/views/view_records/base.rb', line 93 def format(&block) block ||= lambda do |d| "#{ d.hours.to_s }:#{ d.minutes.to_s.rjust(2, '0') }" end block.call self end |
#hours ⇒ Object
108 109 110 |
# File 'lib/tempo/views/view_records/base.rb', line 108 def hours hours = ( @total / 3600 ).to_i end |
#minutes ⇒ Object
112 113 114 |
# File 'lib/tempo/views/view_records/base.rb', line 112 def minutes minutes = ( @total / 60 - hours * 60 ).to_i end |
#subtract(seconds) ⇒ Object
104 105 106 |
# File 'lib/tempo/views/view_records/base.rb', line 104 def subtract(seconds) @total -= seconds end |