class Billable

Public Instance Methods

accumulate_hours_from_minutes(hours, minutes) click to toggle source
# File lib/billable.rb, line 29
def accumulate_hours_from_minutes(hours, minutes)
  if minutes >= 60
    return accumulate_hours_from_minutes(hours + 1, minutes - 60)
  end

  [hours, minutes]
end
charge(rate, timestamp) click to toggle source
# File lib/billable.rb, line 2
def charge(rate, timestamp)
  hours, minutes = split_timestamp(timestamp)
  decimal = minutes / 60.0

  rate * (hours + decimal)
end
sum(args={}) click to toggle source
# File lib/billable.rb, line 9
def sum(args={})
  period_x = args.fetch(:period_x, '0:00') || '0:00'
  period_y = args.fetch(:period_y, '0:00') || '0:00'

  hour_x, min_x = split_timestamp(period_x)
  hour_y, min_y = split_timestamp(period_y)

  hours_sum = hour_x + hour_y
  min_sum = min_x + min_y

  hours, minutes = accumulate_hours_from_minutes(hours_sum, min_sum)

  format_timestamp(hours, minutes)
end
sum_multiple(args) click to toggle source
# File lib/billable.rb, line 24
def sum_multiple(args)
  timestamps = args.fetch(:timestamps, [])
  reduce_timestamps(timestamps)
end

Private Instance Methods

format_timestamp(hours, minutes) click to toggle source
# File lib/billable.rb, line 52
def format_timestamp(hours, minutes)
  minutes = minutes.to_s.rjust(2, '0')
  "#{hours}:#{minutes}"
end
map_sum_to(timestamps) click to toggle source
# File lib/billable.rb, line 44
def map_sum_to(timestamps)
  timestamps.each_slice(2).map { |x, y| sum(period_x: x, period_y: y) }
end
reduce_timestamps(timestamps) click to toggle source
# File lib/billable.rb, line 39
def reduce_timestamps(timestamps)
  return sum(period_x: timestamps.first) if timestamps.length == 1
  return reduce_timestamps(map_sum_to(timestamps))
end
split_timestamp(timestamp) click to toggle source
# File lib/billable.rb, line 48
def split_timestamp(timestamp)
  timestamp.split(':').map(&:to_i)
end