Skip to content

Instantly share code, notes, and snippets.

@thomasklemm
Created September 15, 2012 16:40
Show Gist options
  • Save thomasklemm/3728753 to your computer and use it in GitHub Desktop.
Save thomasklemm/3728753 to your computer and use it in GitHub Desktop.
Speaking Timer from my training
# encoding: utf-8
require 'talks'
class Hash
##
# Cascading Fetch
#
# Tries to fetch provided symbols one after the other
# and return fallback non-symbol value otherwise
def cascading_fetch(*args)
args.each do |field|
if field.instance_of? Symbol
begin
return fetch(field)
rescue
end
else
return field
end
end
end
end
class Timer
def initialize(opts={})
@steps = opts.cascading_fetch(:steps, :step, 8)
@duration = opts.cascading_fetch(:duration_in_seconds, :duration, 120)
@reason = opts.cascading_fetch(:reason, :cause, 'training')
end
def run
start_message
step_messages
end_message
end
def start_message
say "You are starting your #{ @reason } with #{ @steps } steps, #{ @duration.to_s.split('').join(', ') }, seconds each."
end
def step_messages
steps = (1..@steps).map do |i|
if i == 1
"Begin with position 1"
elsif i == @steps
"Proceed to the last position, number #{i}"
else
"Proceed to position #{i}"
end
end
steps.each do |step_message|
say step_message
sleep @duration
end
end
def end_message
say "You have finished your #{ @reason }. Well done! I encourage you to repeat it, as soon as you feel good about it!"
end
def say(message)
Talks.say message
end
end
timer = Timer.new(steps: 8, duration: 180, reason: 'meditation')
timer.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment