class Motion::Duration

Constants

MULTIPLES
UNITS

Attributes

days[R]
hours[R]
minutes[R]
seconds[R]
to_i[R]
total[R]
weeks[R]

Public Class Methods

new(args = 0) click to toggle source

Initialize a duration. ‘args’ can be a hash or anything else. If a hash is passed, it will be scanned for a key=>value pair of time units such as those listed in the Duration::UNITS array or Duration::MULTIPLES hash.

If anything else except a hash is passed, to_i is invoked on that object and expects that it return the number of seconds desired for the duration.

# File lib/project/motion-duration.rb, line 24
def initialize(args = 0)
  if args.kind_of?(Hash)
    @seconds = 0
    MULTIPLES.each do |unit, multiple|
      unit = unit.to_sym
      @seconds += args[unit].to_i * multiple if args.key?(unit)
    end
  # elsif args.kind_of?(String) and args[0] == 'P'
  #   @seconds = ISO8601::Duration.new(args).to_seconds
  else
    @seconds = args.to_i
  end

  calculate!
end

Public Instance Methods

%(other) click to toggle source
# File lib/project/motion-duration.rb, line 62
def %(other)
  Duration.new(@total % other.to_i)
end
*(other) click to toggle source
# File lib/project/motion-duration.rb, line 54
def *(other)
  Duration.new(@total * other.to_i)
end
+(other) click to toggle source
# File lib/project/motion-duration.rb, line 46
def +(other)
  Duration.new(@total + other.to_i)
end
-(other) click to toggle source
# File lib/project/motion-duration.rb, line 50
def -(other)
  Duration.new(@total - other.to_i)
end
/(other) click to toggle source
# File lib/project/motion-duration.rb, line 58
def /(other)
  Duration.new(@total / other.to_i)
end
<=>(other) click to toggle source

Compare this duration to another (or objects that respond to to_i)

# File lib/project/motion-duration.rb, line 41
def <=>(other)
  return false unless other.is_a?(Duration)
  (@total <=> other.to_i).duration_comparable_bool
end
blank?() click to toggle source

@return true if total is 0

# File lib/project/motion-duration.rb, line 89
def blank?
  @total == 0
end
negative?() click to toggle source
# File lib/project/motion-duration.rb, line 98
def negative?
  @negative
end
present?() click to toggle source

@return true if total different than 0

# File lib/project/motion-duration.rb, line 94
def present?
  !blank?
end
total_days() click to toggle source
# File lib/project/motion-duration.rb, line 82
def total_days
  @total / MULTIPLES[:days]
end
total_hours() click to toggle source
# File lib/project/motion-duration.rb, line 78
def total_hours
  @total / MULTIPLES[:hours]
end
total_minutes() click to toggle source
if/when Android ever gets define_method, we can restore this logic and dump
the following 3 methods
http://hipbyte.myjetbrains.com/youtrack/issue/RM-806

%w(minutes hours days).each do |meth|

define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }

end

# File lib/project/motion-duration.rb, line 74
def total_minutes
  @total / MULTIPLES[:minutes]
end

Private Instance Methods

calculate!() click to toggle source

Calculates the duration from seconds and figures out what the actual durations are in specific units. This method is called internally, and does not need to be called by user code.

# File lib/project/motion-duration.rb, line 175
def calculate!
  multiples = [MULTIPLES[:weeks], MULTIPLES[:days], MULTIPLES[:hours], MULTIPLES[:minutes], MULTIPLES[:seconds]]
  units     = []
  @negative = @seconds < 0
  @total    = @seconds.abs.to_f.round
  multiples.inject(@total) do |total, multiple|
    # Divide into largest unit
    units << total / multiple
    total % multiple # The remainder will be divided as the next largest
  end

  # Gather the divided units
  @weeks, @days, @hours, @minutes, @seconds = units
end