Skip to content
Browse files

Merge pull request #2 from jenkoian/master

Add an 'approx' option to only get minutes/seconds rounded to the nearest minute
  • Loading branch information...
2 parents 5b72bff + 4c97185 commit 66525383920007d7290ad62d1cfb9098e3381c7a @garethrees committed Feb 2, 2013
Showing with 17 additions and 0 deletions.
  1. +8 −0 lib/readingtime.rb
  2. +2 −0 lib/readingtime/core_ext.rb
  3. +7 −0 spec/readingtime_spec.rb
View
8 lib/readingtime.rb
@@ -20,4 +20,12 @@ def self.format_words(seconds)
'%d minutes and %d seconds' % seconds.divmod(60)
end
+ def self.format_approx(seconds)
+ if seconds > 59
+ '%d minutes' % (seconds.to_f/60).round
+ else
+ '%d seconds' % seconds
+ end
+ end
+
end
View
2 lib/readingtime/core_ext.rb
@@ -11,6 +11,8 @@ def reading_time(options = {})
Readingtime.format_seconds((minutes + seconds))
when :long
Readingtime.format_words((minutes + seconds))
+ when :approx
+ Readingtime.format_approx((minutes + seconds))
end
end
View
7 spec/readingtime_spec.rb
@@ -7,6 +7,7 @@
describe Readingtime do
let(:two_hundred_words) { ("Lorem " * 200) }
let(:three_hundred_words) { ("Lorem " * 300) }
+ let(:ten_words) { ("Lorem " * 10) }
it "should calculate the reading time of some text" do
two_hundred_words.reading_time.should == "01:00"
@@ -37,7 +38,13 @@
it "should accept an options hash to format the output" do
two_hundred_words.reading_time(:format => :basic).should == "01:00"
two_hundred_words.reading_time(:format => :long).should == "1 minutes and 0 seconds"
+ two_hundred_words.reading_time(:format => :approx).should == "1 minutes"
three_hundred_words.reading_time(:format => :long).should == "1 minutes and 33 seconds"
+ three_hundred_words.reading_time(:format => :approx).should == "2 minutes"
+ end
+
+ it "should return a time in seconds when using approx with time less than 1 minute" do
+ ten_words.reading_time(:format => :approx).should == "3 seconds"
end
end

0 comments on commit 6652538

Please sign in to comment.
Something went wrong with that request. Please try again.