# Helper class for time of day storage in database as integer value
class DayTimeMinutes
  include DayTimeBase

  def to_s
    return '' unless number
    hours, minutes_part = number.divmod(60)
    [hours, minutes_part.round].map{|v| v.to_s.rjust(2, '0')}.join(':')
  end

  def seconds
    number.to_i * 60
  end

  def minutes
    number.to_i
  end

  def hours
    number.to_f / 60.0
  end

  class << self
    def string_to_number(string)
      return nil unless string.present?
      hours, minutes = string.split(':')
      hours.to_i * 60 + minutes.to_i
    end
  end
end
