updated Model to take advantage of new lazy-loading features

This commit is contained in:
2011-03-09 01:31:20 +00:00
parent 33e9477552
commit e0eac61a59
2 changed files with 41 additions and 18 deletions

View File

@@ -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

View File

@@ -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