lib/usaidwat/ext/time.rb in usaidwat-1.1.1 vs lib/usaidwat/ext/time.rb in usaidwat-1.2.0

- old
+ new

@@ -1,12 +1,63 @@ require 'usaidwat/ext/string' class Time def ago - case minutes_ago + delta.ago + end + + def seconds_ago + delta.seconds_ago + end + + def minutes_ago + delta.minutes_ago + end + + def hours_ago + delta.hours_ago + end + + def days_ago + delta.days_ago + end + + def weeks_ago + delta.weeks_ago + end + + def months_ago + delta.months_ago + end + + def years_ago + delta.years_ago + end + + private + + def delta + Time.now - self + end +end + +class Numeric + def negative? + self < 0 + end + + def positive? + self > 0 + end +end + +class Float + def ago + raise ArgumentError, "Delta is negative: #{self}" if negative? + case minutes_ago.to_i when 0..1 - case seconds_ago + case seconds_ago.to_i when 0..5 then "less than 5 seconds ago" when 6..10 then "less than 10 seconds ago" when 11..20 then "less than 20 seconds ago" when 21..40 then "half a minute ago" when 41..59 then "less than a minute ago" @@ -18,15 +69,15 @@ when 1441..2880 then "a day ago" when 2881..10080 then "about #{days_ago.round} days ago" when 10081..43220 then "about #{weeks_ago.round} #{"week".pluralize(weeks_ago.round)} ago" when 43221..525960 then "about #{months_ago.round} #{"month".pluralize(months_ago.round)} ago" when 525960..1051920 then "about a year ago" - else "over #{years_ago.round} years ago" + else "over #{years_ago.to_i} years ago" end end def seconds_ago - Time.now.to_i - to_i + self end def minutes_ago seconds_ago / 60.0 end