jnewland (owner)

Revisions

  • 842bd6 jnewland Fri Mar 27 07:15:34 -0700 2009
  • a9a721 jnewland Thu Mar 19 08:37:57 -0700 2009
gist: 81882 Download_button fork
public
Public Clone URL: git://gist.github.com/81882.git
Embed All Files: show embed
date_weekdays_ext.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class Date
  def self.next_weekday(original_date=Date.today)
    weekdays_from(original_date, 1)
  end
 
  def self.weekdays_since(original_date, now=Date.today)
    weekdays = 1..5
    cursor = original_date
    weekdays_skipped = 0
    until cursor == now do
      cursor += 1
      weekdays_skipped += 1 if weekdays.member?(cursor.wday)
    end
    weekdays_skipped
  end
 
  def self.weekdays_from(original_date=Date.today, num=1)
    weekdays = 1..5
    cursor = original_date
    weekdays_skipped = 0
    until weekdays_skipped == num do
      cursor += 1
      weekdays_skipped += 1 if weekdays.member?(cursor.wday)
    end
    cursor
  end
 
end
 
if $0 == __FILE__
  %w( rubygems test/spec ).each { |f| require f }
 
  context "The next weekday" do
    setup do
      @thursday = Date.parse('03/19/2009')
      @friday = Date.parse('03/20/2009')
      @monday = Date.parse('03/23/2009')
    end
 
    specify "after thursday is friday" do
      Date.next_weekday(@thursday).to_s.should == @friday.to_s
      Date.weekdays_since(@thursday, @friday).should == 1
    end
 
    specify "after friday is monday" do
      Date.next_weekday(@friday).to_s.should == @monday.to_s
      Date.weekdays_since(@friday, @monday).should == 1
    end
  end
 
  context "2 weekdays" do
    setup do
      @thursday = Date.parse('03/19/2009')
      @friday = Date.parse('03/20/2009')
      @monday = Date.parse('03/23/2009')
      @tuesday = Date.parse('03/24/2009')
    end
 
    specify "from thursday is monday" do
      Date.weekdays_from(@thursday, 2).to_s.should == @monday.to_s
      Date.weekdays_since(@thursday, @monday).should == 2
    end
 
    specify "from friday is tuesday" do
      Date.weekdays_from(@friday, 2).to_s.should == @tuesday.to_s
      Date.weekdays_since(@friday, @tuesday).should == 2
    end
  end
 
  context "3 weekdays" do
    setup do
      @thursday = Date.parse('03/19/2009')
      @friday = Date.parse('03/20/2009')
      @tuesday = Date.parse('03/24/2009')
      @wednesday = Date.parse('03/25/2009')
    end
 
    specify "from thursday is tuesday" do
      Date.weekdays_from(@thursday, 3).to_s.should == @tuesday.to_s
      Date.weekdays_since(@thursday, @tuesday).should == 3
    end
 
    specify "from friday is wednesday" do
      Date.weekdays_from(@friday, 3).to_s.should == @wednesday.to_s
      Date.weekdays_since(@friday, @wednesday).should == 3
    end
  end
end