Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed a bug to do with specifying to as a Fixnum

  • Loading branch information...
commit 2068b589fda00d0eb4bb707d853d61fea1d4813b 1 parent 4a9e9ef
Siddharth Sharma authored
Showing with 46 additions and 16 deletions.
  1. +1 −0  Rakefile
  2. +9 −4 lib/datevector.rb
  3. +36 −12 spec/datevector_spec.rb
View
1  Rakefile
@@ -1,3 +1,4 @@
+require "rubygems"
require "bundler/setup"
Bundler::GemHelper.install_tasks
View
13 lib/datevector.rb
@@ -24,8 +24,11 @@ def last_day_of_month
def weekday
#week starts on monday
WEEKDAYS[cwday - 1]
-end
-
+ end
+
+ def inspect
+ self.strftime("%Y-%m-%d")
+ end
end
@@ -84,10 +87,12 @@ def get_dates(from = @from, to = @to)
while (to.class == Date ? d<= to : i <= to)
[@every].flatten.each do |e|
d = d.first_day_of_month + e.to_i - 1
- rv << d if d >= from and (to.class == Date ? d <= to : i <= to)
+ if d >= from and (to.class == Date ? d <= to : i <= to)
+ rv << d
+ i += 1
+ end
end
d = (d.last_day_of_month + 1) >> (@of_every - 1)
- i += 1
end
else
# handle 2nd tuesday every 2nd month type. every = 2, what = :tuesday, :of_every = 2, :period = :month
View
48 spec/datevector_spec.rb
@@ -1,4 +1,4 @@
-require File.join( File.dirname(__FILE__), '..', "spec_helper" )
+require File.join( File.dirname(__FILE__), "spec_helper" )
describe DateVector do
@@ -73,18 +73,42 @@
dv.get_dates(Date.new(2010,2,14), Date.new(2010,6,24)).should == all_thursdays_of_2010.select{|d| d >= Date.new(2010,2,14) and d <= Date.new(2010,6,24)}
end
- it "should return correct dates for monthly datevectors" do
- dv = DateVector.new(12, :day, 1, :month, Date.new(2010,1,1), Date.new(2010,12,31)) # 12th of every month
- ev = (0..11).map{|d| Date.new(2010,1,12) >> d}
- dv.get_dates.should == ev
- dv.get_dates(Date.new(2010,2,14)).should == ev.select{|d| d >= Date.new(2010,2,14)}
- dv.get_dates(Date.new(2010,3,12), Date.new(2010,6,24)).should == ev.select{|d| d >= Date.new(2010,3,12) and d <= Date.new(2010,6,24)}
+ describe "monthly datevectors" do
+ describe "one day per month" do
+ before :all do
+ @dv = DateVector.new(12, :day, 1, :month, Date.new(2010,1,1), Date.new(2010,12,31)) # 12th of every month
+ @ev = (1..12).map{|d| Date.new(2010,d,12)}
+ end
+ it "should get_dates correctly" do
+ @dv.get_dates.should == @ev
+ end
+ it "should get_dates with a from parameter correctly" do
+ @dv.get_dates(Date.new(2010,2,14)).should == @ev.select{|d| d >= Date.new(2010,2,14)}
+ end
+ it "should get dates with a from and to parameter correctly" do
+ @dv.get_dates(Date.new(2010,3,12), Date.new(2010,6,24)).should == @ev.select{|d| d >= Date.new(2010,3,12) and d <= Date.new(2010,6,24)}
+ end
+ it "should get dates with a from and to parameter as Fixnumcorrectly" do
+ @dv.get_dates(Date.new(2010,3,12), 3).should == @ev.select{|d| d >= Date.new(2010,3,12) and d <= Date.new(2010,6,24)}
+ end
+ end
+
+ describe "multiple days per month" do
+ before :all do
+ @dv = DateVector.new([12,27], :day, 1, :month, Date.new(2010,1,1), Date.new(2010,12,31)) # 12th and 27th of every month
+ @ev = ((0..11).map{|d| Date.new(2010,1,12) >> d} + (0..11).map{|d| Date.new(2010,1,27) >> d}).sort
+ end
+ it "should get_dates without parameters correctly" do
+ @dv.get_dates.should == @ev
+ end
+ it "should get_dates with a from parameter correctly" do
+ @dv.get_dates(Date.new(2010,2,14)).should == @ev.select{|d| d >= Date.new(2010,2,14)}
+ end
+ it "should get dates with a from and to parameter correctly" do
+ @dv.get_dates(Date.new(2010,3,12), Date.new(2010,6,24)).should == @ev.select{|d| d >= Date.new(2010,3,12) and d <= Date.new(2010,6,24)}
+ end
+ end
- dv = DateVector.new([12,27], :day, 1, :month, Date.new(2010,1,1), Date.new(2010,12,31)) # 12th and 27th of every month
- ev = ((0..11).map{|d| Date.new(2010,1,12) >> d} + (0..11).map{|d| Date.new(2010,1,27) >> d}).sort
- dv.get_dates.should == ev
- dv.get_dates(Date.new(2010,2,14)).should == ev.select{|d| d >= Date.new(2010,2,14)}
- dv.get_dates(Date.new(2010,3,12), Date.new(2010,6,24)).should == ev.select{|d| d >= Date.new(2010,3,12) and d <= Date.new(2010,6,24)}
end
it "should return correct dates for multi-monthly datevectors" do
Please sign in to comment.
Something went wrong with that request. Please try again.