Class: Numeric
Overview
Based off the rails Numeric class.
Gives us the ability to use nice phrases such as
30.seconds, 5.days, etc.
Instance Method Summary collapse
- #ago(time = Time.now) ⇒ Object (also: #until)
- #days ⇒ Object (also: #day)
- #hours ⇒ Object (also: #hour)
- #minutes ⇒ Object (also: #minute)
- #months ⇒ Object (also: #month)
- #seconds ⇒ Object (also: #second)
- #since(time = Time.now) ⇒ Object (also: #from_now)
- #time_ago ⇒ Object
- #units_ago(unit, seconds) ⇒ Object
- #weeks ⇒ Object (also: #week)
Instance Method Details
#ago(time = Time.now) ⇒ Object Also known as: until
7 8 9 |
# File 'lib/core/time.rb', line 7 def ago(time = Time.now) time - self end |
#days ⇒ Object Also known as: day
32 33 34 |
# File 'lib/core/time.rb', line 32 def days self * 24.hours end |
#hours ⇒ Object Also known as: hour
27 28 29 |
# File 'lib/core/time.rb', line 27 def hours self * 60.minutes end |
#minutes ⇒ Object Also known as: minute
22 23 24 |
# File 'lib/core/time.rb', line 22 def minutes self * 60 end |
#months ⇒ Object Also known as: month
42 43 44 |
# File 'lib/core/time.rb', line 42 def months self * 31.days end |
#seconds ⇒ Object Also known as: second
17 18 19 |
# File 'lib/core/time.rb', line 17 def seconds self end |
#since(time = Time.now) ⇒ Object Also known as: from_now
12 13 14 |
# File 'lib/core/time.rb', line 12 def since(time = Time.now) time + self end |
#time_ago ⇒ Object
47 48 49 50 |
# File 'lib/core/time.rb', line 47 def time_ago out = %w(year month week day hour minute).detect {|unit| self > 1.send(unit) } units_ago(out, self) rescue "Less than a minute ago" end |
#units_ago(unit, seconds) ⇒ Object
52 53 54 55 |
# File 'lib/core/time.rb', line 52 def units_ago(unit,seconds) in_units = (seconds / 1.send(unit)) "#{in_units.to_i} #{in_units != 1 ? unit.to_s.pluralize : unit} ago" end |
#weeks ⇒ Object Also known as: week
37 38 39 |
# File 'lib/core/time.rb', line 37 def weeks self * 7.days end |