module ReadTime

Attributes

config[W]

Public Class Methods

config() click to toggle source
# File lib/read-time.rb, line 33
def self.config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source
# File lib/read-time.rb, line 41
def self.configure
  yield(config)
end
reset_config() click to toggle source
# File lib/read-time.rb, line 37
def self.reset_config
  @config = Configuration.new
end

Public Instance Methods

readtime(options = {}) click to toggle source
# File lib/read-time.rb, line 2
def readtime(options = {})
  reading_speed = (options[:reading_speed] || ReadTime.config.reading_speed).to_f
  format = options[:format] || ReadTime.config.format
  word_count = scan(/[\w-]+/).size
  num = ((word_count / reading_speed) * 60).ceil

  minutes = num / 60
  seconds = num - (minutes * 60)

  case format
  when :default
    min = (num / 60.0).ceil
    "#{min} minute read"
  when :long
    if seconds.zero?
      "#{minutes} minute read"
    elsif minutes.zero?
      "#{seconds} second read"
    else
      "#{minutes} minute #{seconds} second read"
    end
  else
    min = (num / 60.0).ceil
    "#{min} minute read"
  end
end