Class: TimeTrello::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/time_trello/duration.rb

Overview

Public: Describes a duration in terms of hours and minutes, representing data internaly as seconds.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Duration

Returns a new instance of Duration.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/time_trello/duration.rb', line 28

def initialize(*args)
  @internal_seconds = 0 
  time_components = args
  if args.size == 1 && args[0].kind_of?(String)
    time_components = args[0].split(/[:.]/)
  end
  factor = 3600
  time_components.each do |component|
    @internal_seconds += factor * component.to_i
    factor /= 60
  end
end

Instance Method Details

#+(other) ⇒ Object

Public: Operator overload. Sums up two different instances of Duration

other - The other operand



71
72
73
74
75
76
# File 'lib/time_trello/duration.rb', line 71

def +(other)
  duration = Duration.new(0)
  duration.internal_seconds = @internal_seconds + other.internal_seconds

  duration
end

#-(other) ⇒ Object

Public: Operator overload. Subtracts two different instances of Duration.

other - The other operand

Important: The resultant duration will have its components described always as positive numbers, even if other is greater than this instance. It is because the way ruby operates over negative numbers in an integer division.



86
87
88
89
90
91
# File 'lib/time_trello/duration.rb', line 86

def -(other)
  duration = Duration.new(0)
  duration.internal_seconds = (@internal_seconds - other.internal_seconds).abs

  duration
end

#hoursObject

Public: Getter. Returns the number of hours from a given duration



42
43
44
# File 'lib/time_trello/duration.rb', line 42

def hours
  @internal_seconds / 3600
end

#inspectObject

Public: Let a developer to inspect this class instance



102
103
104
# File 'lib/time_trello/duration.rb', line 102

def inspect
  self.to_s
end

#minutesObject

Public: Getter. Returns the number of minutes from the internal representation



47
48
49
# File 'lib/time_trello/duration.rb', line 47

def minutes
  (@internal_seconds / 60) % 60
end

#raw_minutesObject

Public: Getter. Returns the number of raw minutes of a given duration

Important: This is a float value, since it is a raw value



59
60
61
# File 'lib/time_trello/duration.rb', line 59

def raw_minutes
  @internal_seconds.to_f / 60.0
end

#raw_secondsObject

Public: Getter. Returns the raw seconds for a given duration.



64
65
66
# File 'lib/time_trello/duration.rb', line 64

def raw_seconds
  @internal_seconds
end

#secondsObject

Public: Getter. Returns the number of seconds of a given duration



52
53
54
# File 'lib/time_trello/duration.rb', line 52

def seconds
  @internal_seconds % 60
end

#to_sObject

Public: Converts an instance of this class to a string representation. This is a simple facility for fast representation on-screen.



97
98
99
# File 'lib/time_trello/duration.rb', line 97

def to_s
  "#{hours}:#{minutes}.#{seconds}"
end