From e01ae94a5e8bc3104701ed16a5cea06a9e5066ab Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 19 Jul 2010 11:18:25 +0300 Subject: [PATCH] restructured Redistat::Date a bit --- lib/redistat/date.rb | 43 +++++++++++++++++++++++-------------------- spec/date_spec.rb | 4 ++-- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/redistat/date.rb b/lib/redistat/date.rb index e741724..788ca25 100644 --- a/lib/redistat/date.rb +++ b/lib/redistat/date.rb @@ -18,33 +18,14 @@ module Redistat end end - def from_time(input) - [:year, :month, :day, :hour, :min, :sec].each do |k| - send("#{k}=", input.send(k)) - end - end - def to_time ::Time.local(@year, @month, @day, @hour, @min, @sec) end - def from_date(input) - [:year, :month, :day].each do |k| - send("#{k}=", input.send(k)) - end - [:hour, :min, :sec].each do |k| - send("#{k}=", 0) - end - end - def to_date ::Date.civil(@year, @month, @day) end - def from_string(input) - from_time(::Time.parse(input)) - end - def to_string(depth = :sec) output = "" [:year, :month, :day, :hour, :min, :sec].each_with_index do |current, i| @@ -55,9 +36,31 @@ module Redistat output end - alias :to_s :to_string alias :to_t :to_time alias :to_d :to_date + alias :to_s :to_string + + + private + + def from_time(input) + [:year, :month, :day, :hour, :min, :sec].each do |k| + send("#{k}=", input.send(k)) + end + end + + def from_date(input) + [:year, :month, :day].each do |k| + send("#{k}=", input.send(k)) + end + [:hour, :min, :sec].each do |k| + send("#{k}=", 0) + end + end + + def from_string(input) + from_time(::Time.parse(input)) + end end end diff --git a/spec/date_spec.rb b/spec/date_spec.rb index 1a3f56d..43dd331 100644 --- a/spec/date_spec.rb +++ b/spec/date_spec.rb @@ -4,14 +4,14 @@ describe Redistat::Date do it "should initialize from Time object" do now = Time.now - [Redistat::Date.new(now), now.to_redistat].each do |rdate| + [Redistat::Date.new(now), now.to_rs].each do |rdate| [:year, :month, :day, :hour, :min, :sec].each { |k| rdate.send(k).should == now.send(k) } end end it "should initialize from Date object" do today = Date.today - [Redistat::Date.new(today), today.to_redistat].each do |rdate| + [Redistat::Date.new(today), today.to_rs].each do |rdate| [:year, :month, :day].each { |k| rdate.send(k).should == today.send(k) } [:hour, :min, :sec].each { |k| rdate.send(k).should == 0 } end