Class: Time
Instance Method Summary collapse
-
#in_words_since(time = Time.now) ⇒ Object
(also: #in_words_since_now)
Time in words since
time
or now. -
#to_time ⇒ Object
:nodoc:.
Instance Method Details
#in_words_since(time = Time.now) ⇒ Object Also known as: in_words_since_now
Time in words since time
or now.
Examples
5.seconds.ago.in_words_since_now # => less than one minute
5.days.ago.in_words_since_now # => 5 days
1.month.ago.in_words_since_now # => 1 month
101.years.ago.in_words_since_now # => nil
"the article was published #{article.created_at.in_words_since_now || 'hundreds of years' } ago"
# => the article was published 15 minutes ago
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rext/time/helpers.rb', line 22 def in_words_since time = Time.now time = time.to_time return if self > time seconds = (time - self).to_i # TODO: abstract this out pluralize = lambda do |type| n = seconds.send(:"to_#{type}s") n == 1 ? "one #{type}" : "#{n} #{type}s" end case seconds when 0..60 ; 'less than one minute' when 1.minute..60.minutes ; pluralize[:minute] when 1.hour..24.hours ; pluralize[:hour] when 1.day..7.days ; pluralize[:day] when 1.week..4.weeks ; pluralize[:week] when 1.month..12.months ; pluralize[:month] when 1.year..99.years ; pluralize[:year] end end |
#to_time ⇒ Object
:nodoc:
4 5 6 |
# File 'lib/rext/time/helpers.rb', line 4 def to_time #:nodoc: self end |