From 7830b0c5be7a8bcbd7ef3638f72af328c389e51d Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 23 Jul 2010 22:41:10 +0300 Subject: [PATCH] added default depth option to Key object --- lib/redistat/key.rb | 12 ++++++++++-- spec/key_spec.rb | 17 +++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/redistat/key.rb b/lib/redistat/key.rb index c4bc4dd..72112d9 100644 --- a/lib/redistat/key.rb +++ b/lib/redistat/key.rb @@ -9,13 +9,21 @@ module Redistat @scope = scope self.label = label if !label.nil? self.date = date ||= Time.now - self.options = options + @options = default_options.merge(options ||= {}) + end + + def default_options + { :depth => :day } end def date=(input) @date = (input.instance_of?(Redistat::Date)) ? input : Date.new(input) # Redistat::Date, not ::Date end + def depth + @options[:depth] + end + def label @label.name end @@ -29,7 +37,7 @@ module Redistat end def to_s(depth = nil) - depth ||= @options[:depth] if !@options[:depth].nil? + depth ||= @options[:depth] key = "#{@scope}" key << "/" + ((@options[:label_hash].nil? || @options[:label_hash] == true) ? @label.hash : @label.name) if !label.nil? key << ":#{@date.to_s(depth)}" diff --git a/spec/key_spec.rb b/spec/key_spec.rb index 663d953..319d9f8 100644 --- a/spec/key_spec.rb +++ b/spec/key_spec.rb @@ -7,7 +7,7 @@ describe Redistat::Key do @label = "about_us" @label_hash = Digest::SHA1.hexdigest(@label) @date = Time.now - @key = Redistat::Key.new(@scope, @label, @date, {:depth => :day}) + @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour}) end it "should initialize properly" do @@ -19,7 +19,7 @@ describe Redistat::Key do end it "should convert to string properly" do - @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:day)}" + @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}" props = [:year, :month, :day, :hour, :min, :sec] props.each do @key.to_s(props.last).should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(props.last)}" @@ -28,10 +28,15 @@ describe Redistat::Key do end it "should abide to hash_label option" do - @key = Redistat::Key.new(@scope, @label, @date, {:depth => :day, :label_hash => true}) - @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:day)}" - @key = Redistat::Key.new(@scope, @label, @date, {:depth => :day, :label_hash => false}) - @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:day)}" + @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => true}) + @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}" + @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => false}) + @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}" + end + + it "should have default depth option" do + @key = Redistat::Key.new(@scope, @label, @date) + @key.depth.should == :day end it "should allow changing attributes" do