Sha256: bf01f1ff1cf89c4d3383cfd14ecf01607356bd9d3d650f09a3294d91c13bff23

Contents?: true

Size: 1.19 KB

Versions: 18

Compression:

Stored size: 1.19 KB

Contents

class Time
  
  def to_time #:nodoc:
    self
  end
  
  ##
  # 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  # => hundreds of years
  #
  #   "the article was published #{article.created_at.in_words_since_now} ago"  
  #   # => the article was published 15 minutes ago
  #

  def in_words_since time = Time.now
    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..59                ; 'less than one minute'
    when 1.minute..59.minutes ; pluralize[:minute]
    when 1.hour..23.hours     ; pluralize[:hour]
    when 1.day..6.days        ; pluralize[:day]
    when 1.week..3.weeks      ; pluralize[:week]
    when 1.month..11.months   ; pluralize[:month]
    when 1.year..99.years     ; pluralize[:year]
    else 'hundreds of years'
    end
  end
  alias :in_words_since_now :in_words_since
  
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
visionmedia-rext-0.0.2 lib/rext/time/helpers.rb
visionmedia-rext-0.0.3 lib/rext/time/helpers.rb
visionmedia-rext-0.0.4 lib/rext/time/helpers.rb
visionmedia-rext-0.0.5 lib/rext/time/helpers.rb
visionmedia-rext-0.0.6 lib/rext/time/helpers.rb
visionmedia-rext-0.0.7 lib/rext/time/helpers.rb
visionmedia-rext-0.0.8 lib/rext/time/helpers.rb
visionmedia-rext-0.1.0 lib/rext/time/helpers.rb
visionmedia-rext-0.2.1 lib/rext/time/helpers.rb
visionmedia-rext-0.2.2 lib/rext/time/helpers.rb
visionmedia-rext-0.3.0 lib/rext/time/helpers.rb
visionmedia-rext-0.3.1 lib/rext/time/helpers.rb
rext-0.6.0 lib/rext/time/helpers.rb
rext-0.5.0 lib/rext/time/helpers.rb
rext-0.4.1 lib/rext/time/helpers.rb
rext-0.4.0 lib/rext/time/helpers.rb
rext-0.3.5 lib/rext/time/helpers.rb
rext-0.3.4 lib/rext/time/helpers.rb