From dce49f637c1af4d9f862c4f59bb2cf7b1fd62919 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 18 Oct 2010 23:40:09 +0100 Subject: [PATCH] added depth attribute to Redistat::Date --- lib/redistat/date.rb | 10 ++++++---- spec/date_spec.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/redistat/date.rb b/lib/redistat/date.rb index 8dc78d2..e4f7a9b 100644 --- a/lib/redistat/date.rb +++ b/lib/redistat/date.rb @@ -8,10 +8,12 @@ module Redistat attr_accessor :min attr_accessor :sec attr_accessor :usec + attr_accessor :depth DEPTHS = [:year, :month, :day, :hour, :min, :sec, :usec] - def initialize(input) + def initialize(input, depth = nil) + @depth = depth if input.is_a?(::Time) from_time(input) elsif input.is_a?(::Date) @@ -39,7 +41,7 @@ module Redistat alias :to_integer :to_i def to_s(depth = nil) - depth ||= :sec + depth ||= @depth ||= :sec output = "" DEPTHS.each_with_index do |current, i| break if self.send(current).nil? @@ -82,8 +84,8 @@ module Redistat end module DateHelper - def to_redistat - Redistat::Date.new(self) + def to_redistat(depth = nil) + Redistat::Date.new(self, depth) end alias :to_rs :to_redistat end diff --git a/spec/date_spec.rb b/spec/date_spec.rb index 4fc5d07..9c5955f 100644 --- a/spec/date_spec.rb +++ b/spec/date_spec.rb @@ -70,4 +70,18 @@ describe Redistat::Date do Date.today.to_rs.to_date.should == Date.today end + it "should have a depth property" do + now = Time.now + + date = Redistat::Date.new(now) + date.depth.should be_nil + date.to_s.should == now.to_rs(:sec).to_s + date.to_s.should == now.to_rs.to_s(:sec) + + date = Redistat::Date.new(now, :hour) + date.depth.should == :hour + date.to_s.should == now.to_rs(:hour).to_s + date.to_s.should == now.to_rs.to_s(:hour) + end + end \ No newline at end of file