Permalink
Browse files

Making sure periods begin on specific days/times (midnight today, Mon…

…day of this week, 1st of the month)
  • Loading branch information...
1 parent 9a5ce70 commit 3a55b674393b5670e353a011b1ae7b216c438787 @winton committed Jun 15, 2010
Showing with 39 additions and 10 deletions.
  1. +31 −2 lib/periodic_counter.rb
  2. +8 −8 spec/periodic_counter_spec.rb
View
@@ -48,12 +48,20 @@ def initialize(environment, root)
computed_at = data["#{col}_at"] || Time.now.utc
duration = column_to_period_integer(col)
time_since_compute = Time.now.utc - computed_at
+ last_day =
+ if col.include?('day')
+ self.class.today
+ elsif col.include?('week')
+ self.class.last_monday
+ elsif col.include?('month')
+ self.class.first_of_the_month
+ end
if (time_since_compute - duration) >= 0
data[col] = count
- data["#{col}_at"] = Time.now.utc
+ data["#{col}_at"] = last_day
else
data[col] ||= count
- data["#{col}_at"] ||= Time.now.utc
+ data["#{col}_at"] ||= last_day
end
record[col] = count - data[col].to_i
end
@@ -80,4 +88,25 @@ def column_to_period_integer(column)
end
eval(column.join('.'))
end
+
+ class <<self
+
+ def first_of_the_month(now=Time.now.utc.to_date)
+ Date.new(now.year, now.month, 1).to_time(:utc)
+ end
+
+ def last_monday(now=Time.now.utc.to_date)
+ wday = now.wday
+ if wday == 0
+ -6
+ else
+ diff = 1 - wday
+ end
+ Date.new(now.year, now.month, now.day + diff).to_time(:utc)
+ end
+
+ def today(now=Time.now.utc.to_date)
+ Date.new(now.year, now.month, now.day).to_time(:utc)
+ end
+ end
end
@@ -13,8 +13,8 @@
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
- data.delete('counter_last_day_at').to_s.should == Time.now.utc.to_s
- data.delete('counter_last_2_days_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_last_2_days_at').to_s.should == PeriodicCounter.today.to_s
data.should == {
"counter_last_day"=>1,
"counter_last_2_days"=>1
@@ -32,8 +32,8 @@
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
- data.delete('counter_last_day_at').to_s.should == Time.now.utc.to_s
- data.delete('counter_last_2_days_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_last_2_days_at').to_s.should == PeriodicCounter.today.to_s
data.should == {
"counter_last_day"=>1,
"counter_last_2_days"=>1
@@ -52,8 +52,8 @@
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
- data.delete('counter_last_day_at').to_s.should == Time.now.utc.to_s
- data.delete('counter_last_2_days_at').to_s.should == (Time.now - 1.day).utc.to_s
+ data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_last_2_days_at').to_s.should == (PeriodicCounter.today - 1.day).to_s
data.should == {
"counter_last_day"=>3,
"counter_last_2_days"=>1
@@ -72,8 +72,8 @@
start
attributes = Counter.last.attributes
data = attributes.delete('counter_data')
- data.delete('counter_last_day_at').to_s.should == Time.now.utc.to_s
- data.delete('counter_last_2_days_at').to_s.should == Time.now.utc.to_s
+ data.delete('counter_last_day_at').to_s.should == PeriodicCounter.today.to_s
+ data.delete('counter_last_2_days_at').to_s.should == PeriodicCounter.today.to_s
data.should == {
"counter_last_day"=>4,
"counter_last_2_days"=>4

0 comments on commit 3a55b67

Please sign in to comment.