class TimeTrello::Duration

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

Attributes

internal_seconds[RW]

Public Class Methods

new(*args) click to toggle source

Public: Initializes this class with hours, minutes and seconds. You can provide two different sets of arguments in order to initialize this class.

hours - Hours as an integer. minutes - Minutes as an integer using sexagesimal representation. seconds - Seconds as an integer, using sexagesimal representation.

or:

a string containing the duration using the format hh:mm.ss

# File lib/time_trello/duration.rb, line 27
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

Public Instance Methods

+(other) click to toggle source

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

other - The other operand

# File lib/time_trello/duration.rb, line 70
def +(other)
  duration = Duration.new(0)
  duration.internal_seconds = @internal_seconds + other.internal_seconds

  duration
end
-(other) click to toggle source

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.

# File lib/time_trello/duration.rb, line 85
def -(other)
  duration = Duration.new(0)
  duration.internal_seconds = (@internal_seconds - other.internal_seconds).abs

  duration
end
hours() click to toggle source

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

# File lib/time_trello/duration.rb, line 41
def hours
  @internal_seconds / 3600
end
inspect() click to toggle source

Public: Let a developer to inspect this class instance

# File lib/time_trello/duration.rb, line 101
def inspect
  self.to_s
end
minutes() click to toggle source

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

# File lib/time_trello/duration.rb, line 46
def minutes
  (@internal_seconds / 60) % 60
end
raw_minutes() click to toggle source

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

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

# File lib/time_trello/duration.rb, line 58
def raw_minutes
  @internal_seconds.to_f / 60.0
end
raw_seconds() click to toggle source

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

# File lib/time_trello/duration.rb, line 63
def raw_seconds
  @internal_seconds
end
seconds() click to toggle source

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

# File lib/time_trello/duration.rb, line 51
def seconds
  @internal_seconds % 60
end
to_s() click to toggle source

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

# File lib/time_trello/duration.rb, line 96
def to_s
  "#{hours}:#{minutes}.#{seconds}"
end