Module: ReadTimeEstimator::String

Included in:
String
Defined in:
lib/read_time_estimator.rb

Instance Method Summary collapse

Instance Method Details

#hours_in_words(hours) ⇒ Object



36
37
38
# File 'lib/read_time_estimator.rb', line 36

def hours_in_words(hours)
  hours == 1 ? "#{hours} hour" : "#{hours} hours"
end

#minutes_in_words(minutes) ⇒ Object



45
46
47
# File 'lib/read_time_estimator.rb', line 45

def minutes_in_words(minutes)
  minutes == 1 ? "#{minutes} minute" : "#{minutes} minutes"
end

#minutes_to_readObject



5
6
7
# File 'lib/read_time_estimator.rb', line 5

def minutes_to_read
  self.split(' ').count/250.0
end

#read_time_hours(time) ⇒ Object



31
32
33
34
# File 'lib/read_time_estimator.rb', line 31

def read_time_hours(time)
  hours = (time/60).to_i
  hours >= 1 ? hours_in_words(hours) : nil
end

#read_time_minutes(time) ⇒ Object



40
41
42
43
# File 'lib/read_time_estimator.rb', line 40

def read_time_minutes(time)
  minutes = time.to_i
  minutes > 1 ? minutes_in_words(minutes) : nil
end

#read_time_seconds(time) ⇒ Object



49
50
51
52
# File 'lib/read_time_estimator.rb', line 49

def read_time_seconds(time)
  seconds = (time * 60).to_i
  seconds > 1 ? seconds_in_words(seconds) : nil
end

#read_time_wordsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/read_time_estimator.rb', line 9

def read_time_words
  time = minutes_to_read
  words = []
  if read_time_hours(time)
    words << read_time_hours(time)
    time = time % 60
  end
  if read_time_minutes(time)
    words << read_time_minutes(time)
    time = time % 1
  end
  if read_time_seconds(time)
    words << read_time_seconds(time)
  end
  words << "1 second" if words.empty?
  words = words.reverse
  words.insert(2, ", ") if words[2]
  words.insert(1, " and ") if words[1]
  words = words.reverse
  words.join + " to read"
end

#seconds_in_words(seconds) ⇒ Object



54
55
56
# File 'lib/read_time_estimator.rb', line 54

def seconds_in_words(seconds)
  seconds == 1 ? "#{seconds} second" : "#{seconds} seconds"
end