Finder's options methods now set the option when an argument is supplied and returns self for method chaining. When no argument is supplied it returns the option value itself.

Example:

    finder = Redistat::Finder.new
    finder.scope("Foo") #=> Finder object
    finder.scope        #=> Scope object
    finder.scope.to_s   #=> "Foo"
This commit is contained in:
2011-03-13 19:46:52 +00:00
parent 9920c0dc2f
commit 108b6ab02e
2 changed files with 33 additions and 21 deletions

View File

@@ -81,49 +81,56 @@ module Redistat
} }
end end
def connection_ref(ref) def connection_ref(ref = nil)
return options[:connection_ref] if ref.nil?
reset! if options[:connection_ref] != ref reset! if options[:connection_ref] != ref
options[:connection_ref] = ref options[:connection_ref] = ref
self self
end end
def scope(scope) def scope(input = nil)
reset! if !options[:scope].nil? && options[:scope].to_s != scope return options[:scope] if input.nil?
options[:scope] = Scope.new(scope) reset! if !options[:scope].nil? && options[:scope].to_s != input.to_s
options[:scope] = Scope.new(input)
self self
end end
def label(label) def label(input = nil)
reset! if options.has_key?(:label) && options[:label].to_s != label.to_s return options[:label] if input.nil?
options[:label] = (!label.nil?) ? Label.new(label) : nil reset! if options.has_key?(:label) && options[:label].to_s != input.to_s
options[:label] = (!input.nil?) ? Label.new(input) : nil
self self
end end
def dates(from, till) def dates(start, finish)
from(from).till(till) from(start).till(finish)
end end
alias :date :dates alias :date :dates
def from(date) def from(date = nil)
return options[:from] if date.nil?
reset! if options[:from] != date reset! if options[:from] != date
options[:from] = date options[:from] = date
self self
end end
def till(date) def till(date = nil)
return options[:till] if date.nil?
reset! if options[:till] != date reset! if options[:till] != date
options[:till] = date options[:till] = date
self self
end end
alias :until :till alias :until :till
def depth(unit) def depth(unit = nil)
return options[:depth] if unit.nil?
reset! if options[:depth] != unit reset! if options[:depth] != unit
options[:depth] = unit options[:depth] = unit
self self
end end
def interval(unit) def interval(unit = nil)
return options[:interval] if unit.nil?
reset! if options[:interval] != unit reset! if options[:interval] != unit
options[:interval] = unit options[:interval] = unit
self self

View File

@@ -25,31 +25,36 @@ describe Redistat::Finder do
finder.options[:label].to_s.should == options[:label] finder.options[:label].to_s.should == options[:label]
finder.options.should == options.merge(:scope => finder.options[:scope], :label => finder.options[:label]) finder.options.should == options.merge(:scope => finder.options[:scope], :label => finder.options[:label])
finder = Redistat::Finder.scope("hello")
finder.options[:scope].to_s.should == "hello"
finder.scope.to_s.should == "hello"
finder = Redistat::Finder.label("hello")
finder.options[:label].to_s.should == "hello"
finder.label.to_s.shold == "hello"
finder = Redistat::Finder.dates(@two_hours_ago, @one_hour_ago) finder = Redistat::Finder.dates(@two_hours_ago, @one_hour_ago)
finder.options[:from].should == @two_hours_ago finder.options[:from].should == @two_hours_ago
finder.options[:till].should == @one_hour_ago finder.options[:till].should == @one_hour_ago
finder = Redistat::Finder.scope("hello")
finder.options[:scope].to_s.should == "hello"
finder = Redistat::Finder.label("hello")
finder.options[:label].to_s.should == "hello"
finder = Redistat::Finder.from(@two_hours_ago) finder = Redistat::Finder.from(@two_hours_ago)
finder.options[:from].should == @two_hours_ago finder.options[:from].should == @two_hours_ago
finder.from.should == @two_hours_ago
finder = Redistat::Finder.till(@one_hour_ago) finder = Redistat::Finder.till(@one_hour_ago)
finder.options[:till].should == @one_hour_ago finder.options[:till].should == @one_hour_ago
finder.till.should == @one_hour_ago
finder = Redistat::Finder.depth(:hour) finder = Redistat::Finder.depth(:hour)
finder.options[:depth].should == :hour finder.options[:depth].should == :hour
finder.depth.should == :hour
finder = Redistat::Finder.interval(true) finder = Redistat::Finder.interval(true)
finder.options[:interval].should be_true finder.options[:interval].should be_true
finder.interval.should be_true
finder = Redistat::Finder.interval(false) finder = Redistat::Finder.interval(false)
finder.options[:interval].should be_false finder.options[:interval].should be_false
finder.interval.should be_false
end end
it "should fetch stats properly" do it "should fetch stats properly" do