Class: Numeric
- Defined in:
- lib/rext/numeric/time.rb,
lib/rext/numeric/bytes.rb,
lib/rext/numeric/helpers.rb
Instance Method Summary collapse
-
#ago(time = ::Time.now) ⇒ Object
(also: #before)
Time before specified
time
, which defaults to now. - #bytes ⇒ Object (also: #byte)
- #days ⇒ Object (also: #day)
-
#format_currency ⇒ Object
Format a number as human readable currency.
- #gigabytes ⇒ Object (also: #gigabyte)
- #hours ⇒ Object (also: #hour)
- #kilobytes ⇒ Object (also: #kilobyte)
- #megabytes ⇒ Object (also: #megabyte)
- #minutes ⇒ Object (also: #minute)
- #months ⇒ Object (also: #month)
- #seconds ⇒ Object (also: #second)
-
#since(time = ::Time.now) ⇒ Object
(also: #from_now)
Time since specified
time
, which defaults to now. - #terabytes ⇒ Object (also: #terabyte)
- #to_days ⇒ Object
- #to_hours ⇒ Object
- #to_minutes ⇒ Object
- #to_months ⇒ Object
- #to_weeks ⇒ Object
- #to_years ⇒ Object
- #weeks ⇒ Object (also: #week)
- #years ⇒ Object (also: #year)
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 |
#bytes ⇒ Object Also known as: byte
4 |
# File 'lib/rext/numeric/bytes.rb', line 4 def bytes; self end |
#days ⇒ Object Also known as: day
7 |
# File 'lib/rext/numeric/time.rb', line 7 def days; self * 86400 end |
#format_currency ⇒ Object
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 |
#gigabytes ⇒ Object Also known as: gigabyte
7 |
# File 'lib/rext/numeric/bytes.rb', line 7 def gigabytes; self << 30 end |
#hours ⇒ Object Also known as: hour
6 |
# File 'lib/rext/numeric/time.rb', line 6 def hours; self * 3600 end |
#kilobytes ⇒ Object Also known as: kilobyte
5 |
# File 'lib/rext/numeric/bytes.rb', line 5 def kilobytes; self << 10 end |
#megabytes ⇒ Object Also known as: megabyte
6 |
# File 'lib/rext/numeric/bytes.rb', line 6 def megabytes; self << 20 end |
#minutes ⇒ Object Also known as: minute
5 |
# File 'lib/rext/numeric/time.rb', line 5 def minutes; self * 60 end |
#months ⇒ Object Also known as: month
9 |
# File 'lib/rext/numeric/time.rb', line 9 def months; self * 2592000 end |
#seconds ⇒ Object 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 |
#terabytes ⇒ Object Also known as: terabyte
8 |
# File 'lib/rext/numeric/bytes.rb', line 8 def terabytes; self << 40 end |
#to_days ⇒ Object
22 |
# File 'lib/rext/numeric/time.rb', line 22 def to_days; self / 1.day end |
#to_hours ⇒ Object
21 |
# File 'lib/rext/numeric/time.rb', line 21 def to_hours; self / 1.hour end |
#to_minutes ⇒ Object
20 |
# File 'lib/rext/numeric/time.rb', line 20 def to_minutes; self / 1.minute end |
#to_months ⇒ Object
24 |
# File 'lib/rext/numeric/time.rb', line 24 def to_months; self / 1.month end |
#to_weeks ⇒ Object
23 |
# File 'lib/rext/numeric/time.rb', line 23 def to_weeks; self / 1.week end |
#to_years ⇒ Object
25 |
# File 'lib/rext/numeric/time.rb', line 25 def to_years; self / 1.year end |
#weeks ⇒ Object Also known as: week
8 |
# File 'lib/rext/numeric/time.rb', line 8 def weeks; self * 604800 end |
#years ⇒ Object Also known as: year
10 |
# File 'lib/rext/numeric/time.rb', line 10 def years; self * 31471200 end |