added #parent method to Finder objects

This commit is contained in:
2011-03-10 16:26:38 +00:00
parent 53aee885bd
commit 57517983f6
2 changed files with 37 additions and 15 deletions

View File

@@ -69,6 +69,10 @@ module Redistat
all.each_with_index(&block)
end
def parent
@parent ||= self.class.new(options.merge(:label => options[:label].parent)) unless options[:label].nil?
end
def children
build_key.children.map { |key|
self.class.new(options.merge(:label => key.label.to_s))
@@ -88,8 +92,8 @@ module Redistat
end
def label(label)
reset! if !options[:label].nil? && options[:label].to_s != label
options[:label] = Label.new(label)
reset! if options.has_key?(:label) && options[:label].to_s != label.to_s
options[:label] = (!label.nil?) ? Label.new(label) : nil
self
end
@@ -181,6 +185,7 @@ module Redistat
def reset!
@result = nil
@parent = nil
end
def valid_options?

View File

@@ -91,19 +91,36 @@ describe Redistat::Finder do
lambda { Redistat::Finder.find(:from => 3.hours.ago) }.should raise_error(Redistat::InvalidOptions)
end
it "should find children" do
Redistat::Key.new("PageViews", "message/public/die").update_index
Redistat::Key.new("PageViews", "message/public/live").update_index
Redistat::Key.new("PageViews", "message/public/fester").update_index
members = db.smembers("#{@scope}#{Redistat::LABEL_INDEX}message/public") # checking 'message/public'
options = {:scope => "PageViews", :label => "message/public", :from => @two_hours_ago, :till => @one_hour_ago, :depth => :hour, :interval => :hour}
finder = Redistat::Finder.new(options)
finder.children.first.should be_a(Redistat::Finder)
subs = finder.children.map { |f| f.options[:label].me }
subs.should have(3).items
subs.should include('die')
subs.should include('live')
subs.should include('fester')
describe "Grouping" do
before(:each) do
@options = {:scope => "PageViews", :label => "message/public", :from => @two_hours_ago, :till => @one_hour_ago, :depth => :hour, :interval => :hour}
@finder = Redistat::Finder.new(@options)
end
it "should return parent finder" do
@finder.instance_variable_get("@parent").should be_nil
@finder.parent.should be_a(Redistat::Finder)
@finder.instance_variable_get("@parent").should_not be_nil
@finder.parent.options[:label].to_s.should == 'message'
@finder.label('message')
@finder.instance_variable_get("@parent").should be_nil
@finder.parent.should_not be_nil
@finder.parent.options[:label].should be_nil
@finder.parent.parent.should be_nil
end
it "should find children" do
Redistat::Key.new("PageViews", "message/public/die").update_index
Redistat::Key.new("PageViews", "message/public/live").update_index
Redistat::Key.new("PageViews", "message/public/fester").update_index
members = db.smembers("#{@scope}#{Redistat::LABEL_INDEX}message/public") # checking 'message/public'
@finder.children.first.should be_a(Redistat::Finder)
subs = @finder.children.map { |f| f.options[:label].me }
subs.should have(3).items
subs.should include('die')
subs.should include('live')
subs.should include('fester')
end
end
describe "Lazy-Loading" do