Class: Numeric

Inherits:
Object show all
Defined in:
lib/rext/numeric/time.rb,
lib/rext/numeric/bytes.rb,
lib/rext/numeric/helpers.rb

Instance Method Summary collapse

Instance Method Details

#ago(time = ::Time.now) ⇒ Object Also known as: before

Time before specified time, which defaults to now.

Examples

event = 10.days.ago
15.minutes.before event


36
37
38
# File 'lib/rext/numeric/time.rb', line 36

def ago time = ::Time.now
  time - self
end

#bytesObject Also known as: byte



4
# File 'lib/rext/numeric/bytes.rb', line 4

def bytes;     self       end

#daysObject Also known as: day



7
# File 'lib/rext/numeric/time.rb', line 7

def days;    self * 86400    end

#format_currencyObject

Format a number as human readable currency.

Examples

10.format_currency => '10'
10.99.format_currency => '10.99'
10.9999.format_currency => '10.99'
1000.99.format_currency => '1,000.99'
1000000.99.format_currency => '1,000,000.99'


16
17
18
19
20
21
22
# File 'lib/rext/numeric/helpers.rb', line 16

def format_currency
  parts = self.to_s.split '.'
  parts[0].gsub! /(\d)(?=(\d\d\d)+(?!\d))/, '\1,'
  parts.join('.').gsub /(\.\d{2})(\d+)/, '\1'     
rescue 
  self.to_s
end

#gigabytesObject Also known as: gigabyte



7
# File 'lib/rext/numeric/bytes.rb', line 7

def gigabytes; self << 30 end

#hoursObject Also known as: hour



6
# File 'lib/rext/numeric/time.rb', line 6

def hours;   self * 3600     end

#kilobytesObject Also known as: kilobyte



5
# File 'lib/rext/numeric/bytes.rb', line 5

def kilobytes; self << 10 end

#megabytesObject Also known as: megabyte



6
# File 'lib/rext/numeric/bytes.rb', line 6

def megabytes; self << 20 end

#minutesObject Also known as: minute



5
# File 'lib/rext/numeric/time.rb', line 5

def minutes; self * 60       end

#monthsObject Also known as: month



9
# File 'lib/rext/numeric/time.rb', line 9

def months;  self * 2592000  end

#secondsObject Also known as: second



4
# File 'lib/rext/numeric/time.rb', line 4

def seconds; self            end

#since(time = ::Time.now) ⇒ Object Also known as: from_now

Time since specified time, which defaults to now.

Examples

event = 1.year.ago
3.months.since event

4.days.from_now


52
53
54
# File 'lib/rext/numeric/time.rb', line 52

def since time = ::Time.now
  time + self
end

#terabytesObject Also known as: terabyte



8
# File 'lib/rext/numeric/bytes.rb', line 8

def terabytes; self << 40 end

#to_daysObject



22
# File 'lib/rext/numeric/time.rb', line 22

def to_days;    self / 1.day    end

#to_hoursObject



21
# File 'lib/rext/numeric/time.rb', line 21

def to_hours;   self / 1.hour   end

#to_minutesObject



20
# File 'lib/rext/numeric/time.rb', line 20

def to_minutes; self / 1.minute end

#to_monthsObject



24
# File 'lib/rext/numeric/time.rb', line 24

def to_months;  self / 1.month  end

#to_weeksObject



23
# File 'lib/rext/numeric/time.rb', line 23

def to_weeks;   self / 1.week   end

#to_yearsObject



25
# File 'lib/rext/numeric/time.rb', line 25

def to_years;   self / 1.year   end

#weeksObject Also known as: week



8
# File 'lib/rext/numeric/time.rb', line 8

def weeks;   self * 604800   end

#yearsObject Also known as: year



10
# File 'lib/rext/numeric/time.rb', line 10

def years;   self * 31471200 end