Ravn::
NumericRefinements
module
Refinements to Numeric and Time to add convenience methods
- DAYS
- HOURS
- MINUTES
Approximate Time Constants
(in seconds)
- MONTHS
- WEEKS
- YEARS
Returns the Time <receiver> number of seconds after the given time
. E.g., 10.minutes.after( header.expiration )
def after( time )
return time + self
end
Returns the Time <receiver> number of seconds ago. (e.g., expiration > 2.hours.ago )
def ago
return self.before( ::Time.now )
end
Return a description of the receiving Time object in relation to the current time.
Example:
“Saved %s ago.” % object.updated_at.as_delta
def as_delta
now = Time.now
if now > self
seconds = now - self
return "%s ago" % [ timeperiod(seconds) ]
else
seconds = self - now
return "%s from now" % [ timeperiod(seconds) ]
end
end
Returns the Time <receiver> number of seconds before the specified time
. E.g., 2.hours.before( header.expiration )
def before( time )
return time - self
end
Number of bytes (returns receiver unmodified)
def bytes
return self
end
Returns the number of seconds in <receiver> days
def days
return TimeFunctions.calculate_seconds( self, :day )
end
Return the number of bytes in <receiver> exabytes
def exabytes
return self * 1024 ** 6
end
Returns the number of seconds in <receiver> fortnights
def fortnights
return TimeFunctions.calculate_seconds( self, :fortnights )
end
Return a new Time <receiver> number of seconds from now.
def from_now
return self.after( ::Time.now )
end
Returns true
if the receiver is a Time in the future.
def future?
return self > Time.now
end
Return the number of bytes in <receiver> gigabytes
def gigabytes
return self * 1024 ** 3
end
Returns the number of seconds in <receiver> hours
def hours
return TimeFunctions.calculate_seconds( self, :hours )
end
Returns the number of bytes in <receiver> kilobytes
def kilobytes
return self * 1024
end
Return the number of bytes in <receiver> megabytes
def megabytes
return self * 1024 ** 2
end
Number of seconds (returns receiver unmodified)
def milliseconds
return self * 0.001
end
Returns number of seconds in <receiver> minutes
def minutes
return TimeFunctions.calculate_seconds( self, :minutes )
end
Returns the number of seconds in <receiver> months (approximate)
def months
return TimeFunctions.calculate_seconds( self, :months )
end
Returns true
if the receiver is a Time in the past.
def past?
return self < Time.now
end
Return the number of bytes in <receiver> petabytes
def petabytes
return self * 1024 ** 5
end
Return a human readable file size.
def size_suffix
bytes = Float( self )
return case
when bytes >= 1.petabyte
"%0.1fP" % [ bytes / 1.petabyte ]
when bytes >= 1.terabyte
"%0.1fT" % [ bytes / 1.terabyte ]
when bytes >= 1.gigabyte
"%0.1fG" % [ bytes / 1.gigabyte ]
when bytes >= 1.megabyte
"%0.1fM" % [ bytes / 1.megabyte ]
when bytes >= 1.kilobyte
"%0.1fK" % [ bytes / 1.kilobyte ]
else
"%db" % [ self ]
end
end
Return the number of bytes in <receiver> terabytes
def terabytes
return self * 1024 ** 4
end
Return a description of seconds
as the nearest whole unit of time.
def timeperiod( seconds )
return case
when seconds < MINUTES - 5
'less than a minute'
when seconds < 50 * MINUTES
if seconds <= 89
"a minute"
else
"%d minutes" % [ (seconds.to_f / MINUTES).ceil ]
end
when seconds < 90 * MINUTES
'about an hour'
when seconds < 18 * HOURS
"%d hours" % [ (seconds.to_f / HOURS).ceil ]
when seconds < 30 * HOURS
'about a day'
when seconds < WEEKS
"%d days" % [ (seconds.to_f / DAYS).ceil ]
when seconds < 2 * WEEKS
'about a week'
when seconds < 3 * MONTHS
"%d weeks" % [ (seconds.to_f / WEEKS).round ]
when seconds < 18 * MONTHS
"%d months" % [ (seconds.to_f / MONTHS).ceil ]
else
"%d years" % [ (seconds.to_f / YEARS).ceil ]
end
end
Return the number of seconds in <receiver> weeks
def weeks
return TimeFunctions.calculate_seconds( self, :weeks )
end
Returns the number of seconds in <receiver> years (approximate)
def years
return TimeFunctions.calculate_seconds( self, :years )
end