Permalink
Browse files

Added distance_of_time, based off an idea presented by DvyJones in #r…

…ubyonrails
  • Loading branch information...
1 parent e0e4473 commit dc63e339e95d4fe7a64fa1db56e283104c1f324d @radar radar committed Dec 12, 2009
Showing with 40 additions and 5 deletions.
  1. +7 −0 README.markdown
  2. +20 −5 lib/dotiw.rb
  3. +13 −0 spec/dotiw_spec.rb
View
@@ -25,6 +25,13 @@ Don't like any format you're given? That's cool too! Here, have an indifferent h
Indiferrent means that you can access all keys by their `String` or `Symbol` version.
+## distance\_of\_time
+
+If you have simply a number of seconds you can get the "stringified" version of this by using `distance_of_time`:
+
+ >> distance_of_time(300)
+ => "5 minutes"
+
### Options
#### :locale
View
@@ -2,11 +2,17 @@ module ActionView
module Helpers
module DateHelper
def distance_of_time_in_words_hash(from_time, to_time, options={})
- output = HashWithIndifferentAccess.new
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance = (from_time - to_time).abs
+ distance_of_time_hash(distance, from_time, to_time, options)
+ end
+
+ def distance_of_time_hash(distance, from_time = nil, to_time = nil, options={})
+ output = HashWithIndifferentAccess.new
+ from_time ||= Time.now
+ to_time ||= from_time + distance.seconds
I18n.with_options :locale => options[:locale] do |locale|
while distance > 0
if distance < 1.minute
@@ -56,9 +62,11 @@ def distance_of_time_in_words_hash(from_time, to_time, options={})
alias_method :old_distance_of_time_in_words, :distance_of_time_in_words
- def distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {})
- return old_distance_of_time_in_words(from_time, to_time, include_seconds, options) if options.delete(:vague)
- hash = distance_of_time_in_words_hash(from_time, to_time, options)
+ def distance_of_time(seconds, options = {})
+ hash = display_time_in_words(distance_of_time_hash(seconds), options)
+ end
+
+ def display_time_in_words(hash, include_seconds = false, options = {})
hash.delete(:seconds) if !include_seconds
I18n.with_options :locale => options[:locale] do |locale|
# Remove all the values that are nil.
@@ -90,8 +98,15 @@ def distance_of_time_in_words(from_time, to_time, include_seconds = false, optio
options.delete(:precision)
end
+
output.to_sentence(options)
- end
+ end
+ end
+
+ def distance_of_time_in_words(from_time, to_time, include_seconds = false, options = {})
+ return old_distance_of_time_in_words(from_time, to_time, include_seconds, options) if options.delete(:vague)
+ hash = distance_of_time_in_words_hash(from_time, to_time, options)
+ display_time_in_words(hash, include_seconds, options)
end
end
end
View
@@ -12,6 +12,19 @@
Time.zone.stub!(:now).and_return(time)
end
+ describe "distance of time" do
+ [
+ [300, "5 minutes"],
+ [600, "10 minutes"],
+ [3600, "1 hour"],
+ [14515200, "5 months and 15 days"]
+ ].each do |number, result|
+ it "#{number} == #{result}" do
+ distance_of_time(number).should eql(result)
+ end
+ end
+ end
+
describe "hash version" do
describe "giving correct numbers of" do

0 comments on commit dc63e33

Please sign in to comment.