Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Formatting options

  • Loading branch information...
commit da881a524daa905329d3ada8fc9ebf6b7e8490be 1 parent 55b4223
Gareth Rees authored
View
4 lib/readingtime.rb
@@ -16,4 +16,8 @@ def self.format_seconds(seconds)
'%02d:%02d' % seconds.divmod(60)
end
+ def self.format_words(seconds)
+ '%d minutes and %d seconds' % seconds.divmod(60)
+ end
+
end
View
12 lib/readingtime/core_ext.rb
@@ -1,11 +1,17 @@
String.class_eval do
- def reading_time
+ def reading_time(options = {})
+ format_options = options[:format] || :basic
+
word_size = self.calculate_size
-
minutes = Readingtime.minutes_in_seconds(word_size)
seconds = Readingtime.seconds(word_size)
- return Readingtime.format_seconds((minutes + seconds))
+ case format_options
+ when :basic
+ Readingtime.format_seconds((minutes + seconds))
+ when :long
+ Readingtime.format_words((minutes + seconds))
+ end
end
View
12 spec/readingtime_spec.rb
@@ -6,9 +6,11 @@
describe Readingtime do
let(:two_hundred_words) { ("Lorem " * 200) }
+ let(:three_hundred_words) { ("Lorem " * 300) }
it "should calculate the reading time of some text" do
two_hundred_words.reading_time.should == "01:00"
+ three_hundred_words.reading_time.should == "01:33"
end
it "should calculate the length of the string input" do
@@ -32,10 +34,10 @@
Readingtime.format_seconds(1).should == "00:01"
end
- # it "should accept an options hash to format the output" do
- # short_text.reading_time(:format => :basic).should == "0:23"
- # short_text.reading_time(:format => :short).should == "23 seconds"
- # short_text.reading_time(:format => :long).should == "0 minutes and 23 seconds"
- # end
+ 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"
+ three_hundred_words.reading_time(:format => :long).should == "1 minutes and 33 seconds"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.