Sha256: 29ec651a55bdcaf2cdd745f8b4293006d3f5fbb4fb0432e3fe376476734c1d13

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

class Numeric

  def clamp(min, max)
    [[self, min].max, max].min
  end

  def to_period(detailed = true, abbreviated = true)
    total_seconds = self.to_i

    days = total_seconds / 86400
    hours = (total_seconds / 3600) - (days * 24)
    minutes = (total_seconds / 60) - (hours * 60) - (days * 1440)
    seconds = total_seconds % 60

    elements = []
    if days > 0
      suffix = abbreviated ? 'd' : (days == 1 ? ' day' : ' days')
      elements << "#{days}#{suffix}"
    end
    if hours > 0
      suffix = abbreviated ? 'h' : (hours == 1 ? ' hour' : ' hours')
      elements << "#{hours}#{suffix}"
    end
    if minutes > 0 and (detailed or days == 0)
      suffix = abbreviated ? 'm' : (minutes == 1 ? ' minute' : ' minutes')
      elements << "#{minutes}#{suffix}"
    end
    if seconds > 0 and (detailed or (days == 0 and hours == 0))
      suffix = abbreviated ? 's' : (seconds == 1 ? ' second' : ' seconds')
      elements << "#{seconds}#{suffix}"
    end

    elements.join(abbreviated ? ' ' : ', ')
  end

  def to_formatted_time
    return '00:00' if self == 0

    minutes = (self / 60).floor
    seconds = (self % 60).round
    minutes = '0' + minutes.to_s if minutes.to_s.length == 1
    seconds = '0' + seconds.to_s if seconds.to_s.length == 1
    "#{minutes}:#{seconds}"
  end
end

class Range
  def random
    rand * (max - min) + min
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bootstripe-0.2.16 lib/bootstripe/number_additions.rb
bootstripe-0.2.15 lib/bootstripe/number_additions.rb
bootstripe-0.2.14 lib/bootstripe/number_additions.rb