Sha256: 9f8d86d251dfc840a1cc6aea49a0a6fbf7e1415180f62a5a7c332d387a52c0f9

Contents?: true

Size: 1015 Bytes

Versions: 33

Compression:

Stored size: 1015 Bytes

Contents

=begin rdoc
  Based off the rails Numeric class.
  Gives us the ability to use nice phrases such as
  30.seconds, 5.days, etc.
=end
class Numeric
  def ago(time = Time.now)
    time - self
  end
  alias :until :ago

  def since(time = Time.now)
    time + self
  end
  alias :from_now :since
  
  def seconds
    self
  end
  alias :second :seconds

  def minutes
    self * 60
  end
  alias :minute :minutes  

  def hours
    self * 60.minutes
  end
  alias :hour :hours

  def days
    self * 24.hours
  end
  alias :day :days

  def weeks
    self * 7.days
  end
  alias :week :weeks
  
  def months
    self * 31.days
  end
  alias :month :months
  
  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

  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
end

class Time
  def to_time
    self
  end
end

Version data entries

33 entries across 33 versions & 3 rubygems

Version Path
auser-poolparty-1.2.10 lib/poolparty/core/time.rb
auser-poolparty-1.2.11 lib/poolparty/core/time.rb
auser-poolparty-1.2.12 lib/poolparty/core/time.rb
auser-poolparty-1.2.9 lib/poolparty/core/time.rb
auser-poolparty-1.3.0 lib/core/time.rb
auser-poolparty-1.3.1 lib/core/time.rb
auser-poolparty-1.3.10 lib/core/time.rb
auser-poolparty-1.3.11 lib/core/time.rb
auser-poolparty-1.3.12 lib/core/time.rb
auser-poolparty-1.3.13 lib/core/time.rb
auser-poolparty-1.3.14 lib/core/time.rb
auser-poolparty-1.3.15 lib/core/time.rb
auser-poolparty-1.3.16 lib/core/time.rb
auser-poolparty-1.3.17 lib/core/time.rb
auser-poolparty-1.3.2 lib/core/time.rb
auser-poolparty-1.3.3 lib/core/time.rb
auser-poolparty-1.3.4 lib/core/time.rb
auser-poolparty-1.3.5 lib/core/time.rb
auser-poolparty-1.3.6 lib/core/time.rb
auser-poolparty-1.3.7 lib/core/time.rb