From 0d3d5469c0ac61f2a8da9ce406f4cf2ed2d5be20 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 24 Jul 2010 21:39:13 +0300 Subject: [PATCH] moved DEPTHS constant from Summary class to the Date class and using it (mostly) everywhere --- lib/redistat/date.rb | 6 ++++-- lib/redistat/summary.rb | 4 +--- spec/date_spec.rb | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/redistat/date.rb b/lib/redistat/date.rb index 4cd91e0..ca19c9b 100644 --- a/lib/redistat/date.rb +++ b/lib/redistat/date.rb @@ -9,6 +9,8 @@ module Redistat attr_accessor :sec attr_accessor :usec + DEPTHS = [:year, :month, :day, :hour, :min, :sec, :usec] + def initialize(input) if input.is_a?(::Time) from_time(input) @@ -36,7 +38,7 @@ module Redistat def to_string(depth = nil) depth ||= :sec output = "" - [:year, :month, :day, :hour, :min, :sec, :usec].each_with_index do |current, i| + DEPTHS.each_with_index do |current, i| break if self.send(current).nil? if current != :usec output << self.send(current).to_s.rjust((i <= 0) ? 4 : 2, '0') @@ -57,7 +59,7 @@ module Redistat private def from_time(input) - [:year, :month, :day, :hour, :min, :sec, :usec].each do |k| + DEPTHS.each do |k| send("#{k}=", input.send(k)) end end diff --git a/lib/redistat/summary.rb b/lib/redistat/summary.rb index 9ed4e05..fe788f8 100644 --- a/lib/redistat/summary.rb +++ b/lib/redistat/summary.rb @@ -3,13 +3,11 @@ module Redistat include Database extend Database - DEPTHS = [:year, :month, :day, :hour, :min, :sec, :usec] - def self.update_all(key, stats = {}, depth_limit = nil) stats ||= {} depth_limit ||= key.depth return nil if stats.size == 0 - DEPTHS.each do |depth| + Date::DEPTHS.each do |depth| update(key, stats, depth) break if depth == depth_limit end diff --git a/spec/date_spec.rb b/spec/date_spec.rb index fbd3d3d..1367d26 100644 --- a/spec/date_spec.rb +++ b/spec/date_spec.rb @@ -5,7 +5,7 @@ describe Redistat::Date do it "should initialize from Time object" do now = Time.now [Redistat::Date.new(now), now.to_rs].each do |rdate| - [:year, :month, :day, :hour, :min, :sec, :usec].each { |k| rdate.send(k).should == now.send(k) } + Redistat::Date::DEPTHS.each { |k| rdate.send(k).should == now.send(k) } end end