class SlackPomodoroTimer::Timer

Constants

DONE

Attributes

interval[RW]
pomodoros[RW]
total[R]

Public Class Methods

new(options={}) click to toggle source

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

pomodoros=(pomodoros) click to toggle source

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
start() { |current_pomodoro| ... } click to toggle source

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

current_pomodoro() click to toggle source

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
current_time() click to toggle source

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
display_countdown() click to toggle source

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
format_countdown(seconds) click to toggle source

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
pomodoro_status() click to toggle source

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
quit_message() click to toggle source

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
start_message() click to toggle source

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
stop() { |DONE| ... } click to toggle source

Calls the given block passing

# File lib/slack_pomodoro_timer/timer.rb, line 55
def stop(&block)
  yield(DONE)
  puts stop_message
end
stop?() click to toggle source

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
stop_message() click to toggle source

Returns the timer stop message

# File lib/slack_pomodoro_timer/timer.rb, line 109
def stop_message
  "Slack Pomodoro Timer stopped!"
end
time_remaining(end_time) click to toggle source

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