Browse files

Merge branch 'options'

  • Loading branch information...
2 parents f1281c2 + aefc80a commit d7a2e989a07846aaaaebb5e4843fc130c28b6ce7 Gareth Rees committed Sep 30, 2011
Showing with 24 additions and 16 deletions.
  1. +4 −3 lib/readingtime.rb
  2. +1 −1 lib/readingtime/core_ext.rb
  3. +19 −12 spec/readingtime_spec.rb
View
7 lib/readingtime.rb
@@ -3,16 +3,17 @@
module Readingtime
- def self.minutes(words)
- (words / 200).floor
+ def self.minutes_in_seconds(words)
+ (words / 200).floor * 60
end
def self.seconds(words)
(words % 200 / (200 / 60)).floor
end
+ # TODO: Account for HH:MM:00
def self.format_seconds(seconds)
- '%d:%02d' % seconds.divmod(60)
+ '%02d:%02d' % seconds.divmod(60)
end
end
View
2 lib/readingtime/core_ext.rb
@@ -2,7 +2,7 @@
def reading_time
word_size = self.calculate_size
- minutes = Readingtime.minutes(word_size)
+ minutes = Readingtime.minutes_in_seconds(word_size)
seconds = Readingtime.seconds(word_size)
return Readingtime.format_seconds((minutes + seconds))
View
31 spec/readingtime_spec.rb
@@ -5,30 +5,37 @@
describe Readingtime do
- let(:short_text) { "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." }
- let(:long_text) { ("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." * 100) }
+ let(:two_hundred_words) { ("Lorem " * 200) }
+ it "should calculate the reading time of some text" do
+ two_hundred_words.reading_time.should == "01:00"
+ end
it "should calculate the length of the string input" do
- short_text.calculate_size.should == short_text.scan(/(\w|-)+/).size
+ two_hundred_words.calculate_size.should == 200
end
-
it "should calculate the number of minutes the reading should take" do
- words = short_text.calculate_size
- Readingtime.minutes(words).should == (words / 200).floor
+ words = two_hundred_words.calculate_size
+ Readingtime.minutes_in_seconds(words).should == 60
end
it "should calculate the remaining seconds the reading should take" do
- words = short_text.calculate_size
- Readingtime.seconds(words).should == (words % 200 / (200 / 60)).floor
+ words = two_hundred_words.calculate_size
+ Readingtime.seconds(words).should == 0
end
- it "should format the reading time in an HH:MM format" do
+ it "should format the reading time in an MM:SS format" do
Readingtime.format_seconds(3600).should == "60:00"
- Readingtime.format_seconds(60).should == "1:00"
- Readingtime.format_seconds(10).should == "0:10"
- Readingtime.format_seconds(1).should == "0:01"
+ Readingtime.format_seconds(60).should == "01:00"
+ Readingtime.format_seconds(10).should == "00: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
+
end

0 comments on commit d7a2e98

Please sign in to comment.