Browse files

Add thread safety to Project#<<

  • Loading branch information...
1 parent 1716a8b commit 22207384e0d773b6a4ea8e6d5afcd45d83ae3c9c @elia committed Dec 6, 2012
Showing with 15 additions and 13 deletions.
  1. +15 −13 lib/time_tap/project.rb
View
28 lib/time_tap/project.rb
@@ -1,10 +1,9 @@
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/numeric/time'
+require 'monitor'
class TimeTap::Project
- # CONFIG
-
class << self
attr_accessor :pause_limit
def load_projects
@@ -105,6 +104,7 @@ def folders_regexp
def initialize mid_path, name
+ extend MonitorMixin
@name = name
@path = File.expand_path("~/#{mid_path}/#{name}/")
@pinches = []
@@ -157,18 +157,20 @@ def humanized_duration
end
def << time
- time = Time.at time
- last_pinch = pinches.last
- if pinches.empty?
- pinches << Pinch.new(time)
- else
- last_time = last_pinch.end_time
- return unless time > last_time
-
- if (time - last_time) < self.class.pause_limit
- last_pinch.end_time = time
- else
+ synchronize do
+ time = Time.at time
+ last_pinch = pinches.last
+ if pinches.empty?
pinches << Pinch.new(time)
+ else
+ last_time = last_pinch.end_time
+ return unless time > last_time
+
+ if (time - last_time) < self.class.pause_limit
+ last_pinch.end_time = time
+ else
+ pinches << Pinch.new(time)
+ end
end
end
end

0 comments on commit 2220738

Please sign in to comment.