diff --git a/lib/redistat/model.rb b/lib/redistat/model.rb index 8d9059b..1f6c90d 100644 --- a/lib/redistat/model.rb +++ b/lib/redistat/model.rb @@ -6,31 +6,36 @@ module Redistat base.extend(self) end + # + # statistics store/fetch methods + # + def store(label, stats = {}, date = nil, meta = {}, opts = {}) Event.new(name, label, date, stats, options.merge(opts), meta).save end alias :event :store + + def fetch(label, from, till, opts = {}) + find(label, from, till, opts).all + end + alias :lookup :fetch + + def find(label, from, till, opts = {}) + Finder.new( { :scope => name, + :label => label, + :from => from, + :till => till }.merge(options.merge(opts)) ) + end + + # + # options methods + # def connect_to(opts = {}) Connection.create(opts.merge(:ref => name)) options[:connection_ref] = name end - def connection - db(options[:connection_ref]) - end - alias :redis :connection - - def fetch(label, from, till, opts = {}) - Finder.find({ - :scope => name, - :label => label, - :from => from, - :till => till - }.merge(options.merge(opts))) - end - alias :lookup :fetch - def hashed_label(boolean = nil) if !boolean.nil? options[:hashed_label] = boolean @@ -64,15 +69,22 @@ module Redistat end end + # + # resource access methods + # + + def connection + db(options[:connection_ref]) + end + alias :redis :connection + def options @options ||= {} end - private - def name options[:class_name] || (@name ||= self.to_s) end - + end end \ No newline at end of file diff --git a/spec/model_spec.rb b/spec/model_spec.rb index 19755bd..3398bdf 100644 --- a/spec/model_spec.rb +++ b/spec/model_spec.rb @@ -17,6 +17,17 @@ describe Redistat::Model do ModelHelper2.send(:name).should == "ModelHelper2" end + it "should return a Finder" do + two_hours_ago = 2.hours.ago + one_hour_ago = 1.hour.ago + finder = ModelHelper1.find('label', two_hours_ago, one_hour_ago) + finder.should be_a(Redistat::Finder) + finder.options[:scope].should == 'ModelHelper1' + finder.options[:label].should == 'label' + finder.options[:from].should == two_hours_ago + finder.options[:till].should == one_hour_ago + end + it "should listen to model-defined options" do ModelHelper2.depth.should == :day ModelHelper2.store_event.should == true