class SlackPomodoroTimer::Timer
Constants
- DONE
Attributes
Public Class Methods
Accepts options for pomodoros and a time interval in seconds
# File lib/slack_pomodoro_timer/timer.rb, line 14 def initialize(options={}) @pomodoros = options[:pomodoros] @interval = options[:interval] @total = options[:pomodoros] end
Public Instance Methods
Sets the number of pomodoros and the resets total
# File lib/slack_pomodoro_timer/timer.rb, line 43 def pomodoros=(pomodoros) @total = pomodoros @pomodoros = pomodoros end
Starts the timer calls the passed block for each pomodoro displays countdown until next interval
# File lib/slack_pomodoro_timer/timer.rb, line 24 def start(&block) puts start_message if @pomodoros == @total begin yield(current_pomodoro) if block_given? display_countdown @pomodoros -= 1 if stop? stop(&block) else start(&block) end rescue SystemExit, Interrupt puts quit_message end end
Private Instance Methods
Returns the number of the current pomodoro in ascending increments
# File lib/slack_pomodoro_timer/timer.rb, line 130 def current_pomodoro total - pomodoros + 1 end
Get the current time formatted as an AM/PM digital clock
# File lib/slack_pomodoro_timer/timer.rb, line 96 def current_time Time.now.strftime("%l:%M %p") end
Displays the timer countdown to next pomodoro in the console
# File lib/slack_pomodoro_timer/timer.rb, line 62 def display_countdown end_time = Time.now + @interval until Time.now > end_time print "#{time_remaining(end_time)}\r" sleep 1 end puts "#{pomodoro_status} -- POSTED at #{current_time}" end
Formats the countdown timer to display like a digital clock and returns the string
# File lib/slack_pomodoro_timer/timer.rb, line 83 def format_countdown(seconds) minutes = (seconds / 60).to_s.rjust(2,"0") while seconds >= 60 seconds -= 60 end seconds = seconds.to_s.rjust(2,"0") "#{pomodoro_status} -- #{minutes}:#{seconds}" end
Returns the number of the current pomodoro fired in the total
# File lib/slack_pomodoro_timer/timer.rb, line 123 def pomodoro_status "Pomodoro #{current_pomodoro} of #{total}" end
Returns the timer quit message
# File lib/slack_pomodoro_timer/timer.rb, line 115 def quit_message "\nRemaining pomodoros will not be posted." + "\nQuitting slack_pomodoro_timer..." end
Returns the timer start message
# File lib/slack_pomodoro_timer/timer.rb, line 102 def start_message "Slack Pomodoro Timer started!" + "\nStop the timer at any time with CTRL-C" end
Calls the given block passing
# File lib/slack_pomodoro_timer/timer.rb, line 55 def stop(&block) yield(DONE) puts stop_message end
Returns true if stopped manually or if no pomodoros left
# File lib/slack_pomodoro_timer/timer.rb, line 137 def stop? if @pomodoros <= 0 @pomodoros = total true else false end end
Returns the timer stop message
# File lib/slack_pomodoro_timer/timer.rb, line 109 def stop_message "Slack Pomodoro Timer stopped!" end
Returns the time remaining until the next timer fire
# File lib/slack_pomodoro_timer/timer.rb, line 74 def time_remaining(end_time) remaining = (end_time - Time.now).ceil format_countdown(remaining) end