Class: Numeric

Inherits:
Object show all
Defined in:
lib/poolparty/core/time.rb

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

Instance Method Details

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



7
8
9
# File 'lib/poolparty/core/time.rb', line 7

def ago(time = Time.now)
  time - self
end

#daysObject Also known as: day



32
33
34
# File 'lib/poolparty/core/time.rb', line 32

def days
  self * 24.hours
end

#hoursObject Also known as: hour



27
28
29
# File 'lib/poolparty/core/time.rb', line 27

def hours
  self * 60.minutes
end

#minutesObject Also known as: minute



22
23
24
# File 'lib/poolparty/core/time.rb', line 22

def minutes
  self * 60
end

#monthsObject Also known as: month



42
43
44
# File 'lib/poolparty/core/time.rb', line 42

def months
  self * 31.days
end

#secondsObject Also known as: second



17
18
19
# File 'lib/poolparty/core/time.rb', line 17

def seconds
  self
end

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



12
13
14
# File 'lib/poolparty/core/time.rb', line 12

def since(time = Time.now)
  time + self
end

#time_agoObject



47
48
49
50
# File 'lib/poolparty/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/poolparty/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

#weeksObject Also known as: week



37
38
39
# File 'lib/poolparty/core/time.rb', line 37

def weeks
  self * 7.days
end