lib/timetrap/models.rb in samg-timetrap-1.1.0 vs lib/timetrap/models.rb in samg-timetrap-1.1.1
- old
+ new
@@ -1,14 +1,56 @@
module Timetrap
class Entry < Sequel::Model
plugin :schema
+ class << self
+ # a class level instance variable that controls whether or not all entries
+ # should respond to #start and #end with times rounded to 15 minute
+ # increments.
+ attr_accessor :round
+ end
+
+ def round?
+ !!self.class.round
+ end
+
def start= time
self[:start]= Chronic.parse(time) || time
end
def end= time
self[:end]= Chronic.parse(time) || time
+ end
+
+ def start
+ round? ? rounded_start : self[:start]
+ end
+
+ def end
+ round? ? rounded_end : self[:end]
+ end
+
+ def end_or_now
+ self.end || (round? ? round(Time.now) : Time.now)
+ end
+
+ def rounded_start
+ round(self[:start])
+ end
+
+ def rounded_end
+ round(self[:end])
+ end
+
+ def round time
+ return nil unless time
+ Time.at(
+ if (r = time.to_i % 900) < 450
+ time.to_i - r
+ else
+ time.to_i + (900 - r)
+ end
+ )
end
def self.sheets
map{|e|e.sheet}.uniq.sort
end