Class: TimeTrello::Duration
- Inherits:
-
Object
- Object
- TimeTrello::Duration
- 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
-
#+(other) ⇒ Object
Public: Operator overload.
-
#-(other) ⇒ Object
Public: Operator overload.
-
#hours ⇒ Object
Public: Getter.
-
#initialize(*args) ⇒ Duration
constructor
A new instance of Duration.
-
#inspect ⇒ Object
Public: Let a developer to inspect this class instance.
-
#minutes ⇒ Object
Public: Getter.
-
#raw_minutes ⇒ Object
Public: Getter.
-
#raw_seconds ⇒ Object
Public: Getter.
-
#seconds ⇒ Object
Public: Getter.
-
#to_s ⇒ Object
Public: Converts an instance of this class to a string representation.
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 |
#hours ⇒ Object
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 |
#inspect ⇒ Object
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 |
#minutes ⇒ Object
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_minutes ⇒ Object
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_seconds ⇒ Object
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 |
#seconds ⇒ Object
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_s ⇒ Object
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 |