class Readspeed::Tracker
Constants
- COMMANDS
Attributes
file_name[RW]
Public Class Methods
new(title)
click to toggle source
# File lib/readspeed/tracker.rb, line 7 def initialize(title) @pages = 0 @times = [] @title = title @input = nil end
Public Instance Methods
start()
click to toggle source
# File lib/readspeed/tracker.rb, line 14 def start puts "#{help_info}\n Starting recording. Press [ENTER] when you finish reading page." while true do start_time = Time.now case read_command when :quit write_summary_to_file break when :pause @paused = true when :resume @paused = false when :help print_help else record_next_page(start_time, Time.now) end end end
Private Instance Methods
average()
click to toggle source
# File lib/readspeed/tracker.rb, line 112 def average (total / @pages).round(1) end
expanded_commands()
click to toggle source
# File lib/readspeed/tracker.rb, line 43 def expanded_commands COMMANDS.reduce({}) do |sum, (command, inputs)| inputs.each { |i| sum[i] = command } sum end end
help_info()
click to toggle source
# File lib/readspeed/tracker.rb, line 69 def help_info <<~EOS Press [ENTER] without typing anything to start next page or one of the following commands: q, quit - quit the application, write summary to file p, pause - record current page, pause r, resume - resume from pause h, help - show help (this) EOS end
last()
click to toggle source
# File lib/readspeed/tracker.rb, line 108 def last @times.last.round(1) end
read_command()
click to toggle source
# File lib/readspeed/tracker.rb, line 50 def read_command status = @paused ? "(paused)" : "#{@pages}" print "#{status}> " input = STDIN.gets.chomp if @paused && input == '' @paused = false selected_command = :resume else selected_command = expanded_commands[input] end selected_command end
record_next_page(start_time, end_time)
click to toggle source
# File lib/readspeed/tracker.rb, line 63 def record_next_page(start_time, end_time) @pages += 1 @times << end_time - start_time puts summary end
summary()
click to toggle source
# File lib/readspeed/tracker.rb, line 104 def summary "Finished page #{@pages}. Last: #{last} Average: #{average} Total: #{total}" end
total()
click to toggle source
# File lib/readspeed/tracker.rb, line 116 def total @times.reduce(&:+).round(1) end
write_summary_to_file()
click to toggle source
# File lib/readspeed/tracker.rb, line 81 def write_summary_to_file puts "Writing summary to #{file_name}" if File.exist?(file_name) reading_summary = YAML.load(File.read(file_name)) else reading_summary = { title: @title, started_at: Time.now, readings: [] } end reading = { session_finished_at: Time.now, pages: @pages, times: @times, summary: summary } reading_summary[:readings] << reading File.open(file_name, 'w') { |f| f.write(reading_summary.to_yaml) } end