date_ext 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/date_ext.gemspec +1 -1
- data/lib/quarter.rb +36 -19
- data/lib/year.rb +6 -0
- data/test/quarter_test.rb +27 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/date_ext.gemspec
CHANGED
data/lib/quarter.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
class Quarter
|
2
|
+
include Comparable
|
3
|
+
|
2
4
|
attr_accessor :year, :quarter
|
3
5
|
|
4
6
|
def initialize(year, quarter)
|
@@ -10,25 +12,40 @@ class Quarter
|
|
10
12
|
Year.new(@year)
|
11
13
|
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
15
|
+
def <=>(other)
|
16
|
+
self.first_date <=> other.first_date
|
17
|
+
end
|
18
|
+
|
19
|
+
def -(x)
|
20
|
+
if x.is_a?(Numeric)
|
21
|
+
y = year - (x / 4)
|
22
|
+
q = quarter - (x % 4)
|
23
|
+
if q < 1
|
24
|
+
y -= 1
|
25
|
+
q += 4
|
26
|
+
end
|
27
|
+
return Quarter.new(y, q)
|
28
|
+
else
|
29
|
+
raise TypeError, 'expected numeric'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def +(x)
|
34
|
+
if x.is_a?(Numeric)
|
35
|
+
y = year + (x / 4)
|
36
|
+
q = quarter + (x % 4)
|
37
|
+
if q > 4
|
38
|
+
q -= 4
|
39
|
+
y += 1
|
40
|
+
end
|
41
|
+
return Quarter.new(y, q)
|
42
|
+
else
|
43
|
+
raise TypeError, 'expected numeric'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def next; self + 1; end
|
48
|
+
def previous; self - 1; end
|
32
49
|
|
33
50
|
def months; (first_month..last_month).to_a; end
|
34
51
|
|
data/lib/year.rb
CHANGED
data/test/quarter_test.rb
CHANGED
@@ -1,6 +1,33 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper'
|
2
2
|
|
3
3
|
class QuarterTest < Test::Unit::TestCase
|
4
|
+
context "#next #previous" do
|
5
|
+
setup {
|
6
|
+
@quarter = Quarter.new(2009,1)
|
7
|
+
}
|
8
|
+
|
9
|
+
should "- 1" do
|
10
|
+
assert_equal Quarter.new(2008,4), @quarter - 1
|
11
|
+
end
|
12
|
+
should "- 2" do
|
13
|
+
assert_equal Quarter.new(2008,3), @quarter - 2
|
14
|
+
end
|
15
|
+
should "+ 1" do
|
16
|
+
assert_equal Quarter.new(2009,2), @quarter + 1
|
17
|
+
end
|
18
|
+
should "+ 4" do
|
19
|
+
assert_equal Quarter.new(2010,1), @quarter + 4
|
20
|
+
end
|
21
|
+
|
22
|
+
should "#next" do
|
23
|
+
assert_equal Quarter.new(2009,2), @quarter.next
|
24
|
+
end
|
25
|
+
|
26
|
+
should "#previous" do
|
27
|
+
assert_equal Quarter.new(2008,4), @quarter.previous
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
4
31
|
|
5
32
|
context "Quarter 2009,1" do
|
6
33
|
setup {
|