mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
updated Model to take advantage of new lazy-loading features
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user