Class: Tempo::Views::ViewRecords::Duration

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(seconds = 0) ⇒ Duration

Returns a new instance of Duration.



61
62
63
64
# File 'lib/tempo/views/view_records/base.rb', line 61

def initialize seconds=0
  @type = "duration"
  @total = seconds
end

Instance Attribute Details

#totalObject

Returns the value of attribute total.



59
60
61
# File 'lib/tempo/views/view_records/base.rb', line 59

def total
  @total
end

#typeObject

Returns the value of attribute type.



59
60
61
# File 'lib/tempo/views/view_records/base.rb', line 59

def type
  @type
end

Instance Method Details

#add(seconds) ⇒ Object



73
74
75
# File 'lib/tempo/views/view_records/base.rb', line 73

def add(seconds)
  @total += seconds
end

#format(&block) ⇒ Object



66
67
68
69
70
71
# File 'lib/tempo/views/view_records/base.rb', line 66

def format(&block)
  block ||= lambda do |d|
    "#{ d.hours.to_s }:#{ d.minutes.to_s.rjust(2, '0') }"
  end
  block.call self
end

#hoursObject



81
82
83
# File 'lib/tempo/views/view_records/base.rb', line 81

def hours
  hours = ( @total / 3600 ).to_i
end

#minutesObject



85
86
87
# File 'lib/tempo/views/view_records/base.rb', line 85

def minutes
  minutes = ( @total / 60 - hours * 60 ).to_i
end

#subtract(seconds) ⇒ Object



77
78
79
# File 'lib/tempo/views/view_records/base.rb', line 77

def subtract(seconds)
  @total -= seconds
end