Sha256: 17ff377ad110d619666f64cbe146b367a0a4b4cbd1afd7539ad7144417a43375

Contents?: true

Size: 970 Bytes

Versions: 91

Compression:

Stored size: 970 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

Version data entries

91 entries across 91 versions & 3 rubygems

Version Path
auser-poolparty-0.2.15 lib/poolparty/core/time.rb
auser-poolparty-0.2.16 lib/poolparty/core/time.rb
auser-poolparty-0.2.2 lib/poolparty/core/time.rb
auser-poolparty-0.2.20 lib/poolparty/core/time.rb
auser-poolparty-0.2.21 lib/poolparty/core/time.rb
auser-poolparty-0.2.22 lib/poolparty/core/time.rb
auser-poolparty-0.2.23 lib/poolparty/core/time.rb
auser-poolparty-0.2.24 lib/poolparty/core/time.rb
auser-poolparty-0.2.25 lib/poolparty/core/time.rb
auser-poolparty-0.2.26 lib/poolparty/core/time.rb
auser-poolparty-0.2.3 lib/poolparty/core/time.rb
auser-poolparty-0.2.35 lib/poolparty/core/time.rb
auser-poolparty-0.2.36 lib/poolparty/core/time.rb
auser-poolparty-0.2.37 lib/poolparty/core/time.rb
auser-poolparty-0.2.38 lib/poolparty/core/time.rb
auser-poolparty-0.2.39 lib/poolparty/core/time.rb
auser-poolparty-0.2.4 lib/poolparty/core/time.rb
auser-poolparty-0.2.40 lib/poolparty/core/time.rb
auser-poolparty-0.2.41 lib/poolparty/core/time.rb
auser-poolparty-0.2.42 lib/poolparty/core/time.rb