Class: Beans::Stopwatch
- Inherits:
-
Object
- Object
- Beans::Stopwatch
- Defined in:
- lib/beans/stopwatch.rb
Instance Method Summary collapse
- #days_elapsed ⇒ Object
- #dollars_elapsed ⇒ Object
- #hours_elapsed ⇒ Object
-
#initialize ⇒ Stopwatch
constructor
A new instance of Stopwatch.
- #minutes_elapsed ⇒ Object
- #next_notification_at ⇒ Object
- #nice_elapsed_time ⇒ Object
- #notify ⇒ Object
- #query(unit) ⇒ Object
- #reset ⇒ Object
- #running? ⇒ Boolean
- #seconds_elapsed ⇒ Object
- #seconds_to_next_notification ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize ⇒ Stopwatch
4 5 6 |
# File 'lib/beans/stopwatch.rb', line 4 def initialize self.reset end |
Instance Method Details
#days_elapsed ⇒ Object
70 71 72 |
# File 'lib/beans/stopwatch.rb', line 70 def days_elapsed hours_elapsed/24.0 end |
#dollars_elapsed ⇒ Object
74 75 76 |
# File 'lib/beans/stopwatch.rb', line 74 def dollars_elapsed seconds_elapsed * Config.dollars_per_second end |
#hours_elapsed ⇒ Object
66 67 68 |
# File 'lib/beans/stopwatch.rb', line 66 def hours_elapsed minutes_elapsed / 60.0 end |
#minutes_elapsed ⇒ Object
62 63 64 |
# File 'lib/beans/stopwatch.rb', line 62 def minutes_elapsed seconds_elapsed / 60.0 end |
#next_notification_at ⇒ Object
82 83 84 |
# File 'lib/beans/stopwatch.rb', line 82 def next_notification_at @last_notification_at + Config.seconds_between_notifications end |
#nice_elapsed_time ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/beans/stopwatch.rb', line 90 def nice_elapsed_time if (s=seconds_elapsed) < 60 "#{sprintf("%i",s)} seconds" elsif (m=minutes_elapsed) < 60 "#{sprintf("%i",m)} minutes" elsif (h=hours_elapsed) < 24 "#{sprintf("%.1f",h)} hours" else "#{sprintf("%.1f",days_elapsed)} days" end end |
#notify ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/beans/stopwatch.rb', line 103 def notify @last_notification_at = Time.now return false if @first_started_at.nil? Beans::Notification.new( sprintf( "$%.02f", dollars_elapsed), "over #{nice_elapsed_time} since #{@first_started_at.strftime( "%-I:%M %p")}." ) end |
#query(unit) ⇒ Object
78 79 80 |
# File 'lib/beans/stopwatch.rb', line 78 def query(unit) self.send( "#{unit.strip}_elapsed" ) end |
#reset ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/beans/stopwatch.rb', line 8 def reset t = Time.now @first_started_at = nil @last_notification_at = t @previously_elapsed_time = 0.0 @last_stopped_at = t @last_started_at = nil @running = false end |
#running? ⇒ Boolean
48 49 50 |
# File 'lib/beans/stopwatch.rb', line 48 def running? @running end |
#seconds_elapsed ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/beans/stopwatch.rb', line 52 def seconds_elapsed if @last_started_at.nil? 0.0 else s = @previously_elapsed_time s += (Time.now-@last_started_at).to_f unless stopped? s end end |
#seconds_to_next_notification ⇒ Object
86 87 88 |
# File 'lib/beans/stopwatch.rb', line 86 def seconds_to_next_notification next_notification_at - Time.now end |
#start ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/beans/stopwatch.rb', line 18 def start if stopped? t = Time.now @first_started_at ||= t @last_started_at = t @running = true end end |
#stop ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/beans/stopwatch.rb', line 30 def stop if running? t = Time.now @previously_elapsed_time ||= 0 @previously_elapsed_time += (t-@last_started_at).to_f @last_stopped_at = t @running = false end end |
#stopped? ⇒ Boolean
44 45 46 |
# File 'lib/beans/stopwatch.rb', line 44 def stopped? !@running end |