From e15f6376032d9ba327199abce86d16c9ecda812d Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 18 Apr 2012 15:08:25 +0100 Subject: [PATCH] Make Model.expire act like the other options, and add tests for it --- lib/redistat/model.rb | 8 ++++++-- spec/model_helper.rb | 1 + spec/model_spec.rb | 2 ++ spec/summary_spec.rb | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/redistat/model.rb b/lib/redistat/model.rb index 5b2e0d4..f494959 100644 --- a/lib/redistat/model.rb +++ b/lib/redistat/model.rb @@ -46,8 +46,12 @@ module Redistat alias :class_name :scope - def expire(exp) - options[:expire] = exp.is_a?(Hash) ? exp : Hash.new(exp) + def expire(exp = nil) + if !exp.nil? + options[:expire] = exp.is_a?(Hash) ? exp : Hash.new(exp) + else + options[:expire] + end end def connect_to(opts = {}) diff --git a/spec/model_helper.rb b/spec/model_helper.rb index f2dbf93..def5046 100644 --- a/spec/model_helper.rb +++ b/spec/model_helper.rb @@ -26,5 +26,6 @@ class ModelHelper4 include Redistat::Model scope "FancyHelper" + expire :hour => 24*3600 end diff --git a/spec/model_spec.rb b/spec/model_spec.rb index cc5fbfb..0c88b27 100644 --- a/spec/model_spec.rb +++ b/spec/model_spec.rb @@ -38,6 +38,7 @@ describe Redistat::Model do ModelHelper2.store_event.should == true ModelHelper2.hashed_label.should == true ModelHelper2.scope.should be_nil + ModelHelper2.expire.should be_nil ModelHelper1.depth.should == nil ModelHelper1.store_event.should == nil @@ -57,6 +58,7 @@ describe Redistat::Model do ModelHelper4.scope.should == "FancyHelper" ModelHelper4.send(:name).should == "FancyHelper" + ModelHelper4.expire.should == {:hour => 24*3600} end it "should store and fetch stats" do diff --git a/spec/summary_spec.rb b/spec/summary_spec.rb index 2f92910..c0e5106 100644 --- a/spec/summary_spec.rb +++ b/spec/summary_spec.rb @@ -10,6 +10,7 @@ describe Redistat::Summary do @date = Time.now @key = Redistat::Key.new(@scope, @label, @date, {:depth => :day}) @stats = {"views" => 3, "visitors" => 2} + @expire = {:hour => 24*3600} end it "should update a single summary properly" do @@ -32,6 +33,20 @@ describe Redistat::Summary do summary["visitors"].should == "1" end + it "should set key expiry properly" do + Redistat::Summary.update_all(@key, @stats, :hour,{:expire => @expire}) + ((24*3600)-1..(24*3600)+1).should include(db.ttl(@key.to_s(:hour))) + [:day, :month, :year].each do |depth| + db.ttl(@key.to_s(depth)).should == -1 + end + + db.flushdb + Redistat::Summary.update_all(@key, @stats, :hour, {:expire => {}}) + [:hour, :day, :month, :year].each do |depth| + db.ttl(@key.to_s(depth)).should == -1 + end + end + it "should update all summaries properly" do Redistat::Summary.update_all(@key, @stats, :sec) [:year, :month, :day, :hour, :min, :sec, :usec].each do |depth|