diff --git a/lib/redistat/finder.rb b/lib/redistat/finder.rb index 63c947c..289f0ce 100644 --- a/lib/redistat/finder.rb +++ b/lib/redistat/finder.rb @@ -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? diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index 0d8df60..bf60a29 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -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