Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding approx option to only get minutes/seconds rounded to the neare…

…st minute
  • Loading branch information...
commit 1a7c277337b9c4bb204d7c43ff687c80dde0dba8 1 parent 5b72bff
@jenkoian jenkoian authored
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/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 == "1 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
Please sign in to comment.
Something went wrong with that request. Please try again.