Ravn::

NumericRefinements module

Refinements to Numeric and Time to add convenience methods

Constants

DAYS
HOURS
MINUTES

Approximate Time Constants (in seconds)

MONTHS
WEEKS
YEARS

Public Instance Methods

after( time )

Returns the Time <receiver> number of seconds after the given time. E.g., 10.minutes.after( header.expiration )

# File lib/ravn/mixins.rb, line 533
def after( time )
        return time + self
end
ago()

Returns the Time <receiver> number of seconds ago. (e.g., expiration > 2.hours.ago )

# File lib/ravn/mixins.rb, line 526
def ago
        return self.before( ::Time.now )
end
as_delta()

Return a description of the receiving Time object in relation to the current time.

Example:

“Saved %s ago.” % object.updated_at.as_delta

# File lib/ravn/mixins.rb, line 635
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
before( time )

Returns the Time <receiver> number of seconds before the specified time. E.g., 2.hours.before( header.expiration )

# File lib/ravn/mixins.rb, line 519
def before( time )
        return time - self
end
bytes()

Number of bytes (returns receiver unmodified)

# File lib/ravn/mixins.rb, line 545
def bytes
        return self
end
days()

Returns the number of seconds in <receiver> days

# File lib/ravn/mixins.rb, line 483
def days
        return TimeFunctions.calculate_seconds( self, :day )
end
exabytes()

Return the number of bytes in <receiver> exabytes

# File lib/ravn/mixins.rb, line 587
def exabytes
        return self * 1024 ** 6
end
fortnights()

Returns the number of seconds in <receiver> fortnights

# File lib/ravn/mixins.rb, line 497
def fortnights
        return TimeFunctions.calculate_seconds( self, :fortnights )
end
from_now()

Return a new Time <receiver> number of seconds from now.

# File lib/ravn/mixins.rb, line 539
def from_now
        return self.after( ::Time.now )
end
future?()

Returns true if the receiver is a Time in the future.

# File lib/ravn/mixins.rb, line 618
def future?
        return self > Time.now
end
gigabytes()

Return the number of bytes in <receiver> gigabytes

# File lib/ravn/mixins.rb, line 566
def gigabytes
        return self * 1024 ** 3
end
hours()

Returns the number of seconds in <receiver> hours

# File lib/ravn/mixins.rb, line 476
def hours
        return TimeFunctions.calculate_seconds( self, :hours )
end
kilobytes()

Returns the number of bytes in <receiver> kilobytes

# File lib/ravn/mixins.rb, line 552
def kilobytes
        return self * 1024
end
megabytes()

Return the number of bytes in <receiver> megabytes

# File lib/ravn/mixins.rb, line 559
def megabytes
        return self * 1024 ** 2
end
milliseconds()

Number of seconds (returns receiver unmodified)

# File lib/ravn/mixins.rb, line 457
def milliseconds
        return self * 0.001
end
minutes()

Returns number of seconds in <receiver> minutes

# File lib/ravn/mixins.rb, line 469
def minutes
        return TimeFunctions.calculate_seconds( self, :minutes )
end
months()

Returns the number of seconds in <receiver> months (approximate)

# File lib/ravn/mixins.rb, line 504
def months
        return TimeFunctions.calculate_seconds( self, :months )
end
past?()

Returns true if the receiver is a Time in the past.

# File lib/ravn/mixins.rb, line 624
def past?
        return self < Time.now
end
petabytes()

Return the number of bytes in <receiver> petabytes

# File lib/ravn/mixins.rb, line 580
def petabytes
        return self * 1024 ** 5
end
size_suffix()

Return a human readable file size.

# File lib/ravn/mixins.rb, line 594
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
terabytes()

Return the number of bytes in <receiver> terabytes

# File lib/ravn/mixins.rb, line 573
def terabytes
        return self * 1024 ** 4
end
timeperiod( seconds )

Return a description of seconds as the nearest whole unit of time.

# File lib/ravn/mixins.rb, line 648
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
weeks()

Return the number of seconds in <receiver> weeks

# File lib/ravn/mixins.rb, line 490
def weeks
        return TimeFunctions.calculate_seconds( self, :weeks )
end
years()

Returns the number of seconds in <receiver> years (approximate)

# File lib/ravn/mixins.rb, line 511
def years
        return TimeFunctions.calculate_seconds( self, :years )
end