kronos 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/kronos.rb +29 -13
- data/lib/version.rb +1 -1
- data/spec/errors_spec.rb +71 -0
- metadata +5 -3
data/lib/kronos.rb
CHANGED
@@ -40,21 +40,37 @@ class Kronos
|
|
40
40
|
s
|
41
41
|
end
|
42
42
|
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
43
|
+
def errors
|
44
|
+
errors = []
|
45
|
+
if day && (!month || !year)
|
46
|
+
errors << "if day is given, then month and year must also be given"
|
47
|
+
elsif month && !year
|
48
|
+
errors << "if month is given, then year must also be given"
|
49
|
+
elsif !year
|
50
|
+
errors << "year must be given"
|
51
|
+
else
|
52
|
+
if day && !((1 .. 31) === day)
|
53
|
+
errors << "day (#{day}) must be between 1 and 31"
|
54
|
+
end
|
55
|
+
if month && !((1 .. 12) === month)
|
56
|
+
errors << "month (#{month}) must be between 1 and 12"
|
57
|
+
end
|
58
|
+
if year && !((1970 .. 2069) === year)
|
59
|
+
errors << "year (#{year}) must be between 1970 and 2069"
|
60
|
+
end
|
61
|
+
if errors.length == 0 && day && month && year
|
62
|
+
begin
|
63
|
+
Date.new(year, month, day)
|
64
|
+
rescue ArgumentError
|
65
|
+
errors << "date (#{year}-#{month}-#{day}) must be valid"
|
66
|
+
end
|
55
67
|
end
|
56
68
|
end
|
57
|
-
|
69
|
+
errors
|
70
|
+
end
|
71
|
+
|
72
|
+
def valid?
|
73
|
+
errors.length == 0
|
58
74
|
end
|
59
75
|
|
60
76
|
def <(other)
|
data/lib/version.rb
CHANGED
data/spec/errors_spec.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Kronos Comparisons" do
|
4
|
+
|
5
|
+
def new_kronos(year=nil, month=nil, day=nil)
|
6
|
+
k = Kronos.new
|
7
|
+
k.year = year if year
|
8
|
+
k.month = month if month
|
9
|
+
k.day = day if day
|
10
|
+
k
|
11
|
+
end
|
12
|
+
|
13
|
+
it "year only is valid" do
|
14
|
+
new_kronos(2005).errors.should == []
|
15
|
+
end
|
16
|
+
|
17
|
+
it "month and year is valid" do
|
18
|
+
new_kronos(2005, 8).errors.should == []
|
19
|
+
end
|
20
|
+
|
21
|
+
it "day and year is invalid" do
|
22
|
+
errors = new_kronos(2005, nil, 22).errors
|
23
|
+
errors.length.should == 1
|
24
|
+
errors.should include("if day is given, then month and year must also be given")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "nothing is invalid" do
|
28
|
+
errors = new_kronos(nil, nil, nil).errors
|
29
|
+
errors.length.should == 1
|
30
|
+
errors.should include("year must be given")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "month, day, and year is valid" do
|
34
|
+
new_kronos(2005, 8, 22).errors.should == []
|
35
|
+
end
|
36
|
+
|
37
|
+
it "month, day, and year with out-of-range month is invalid" do
|
38
|
+
errors = new_kronos(2005, 13, 22).errors
|
39
|
+
errors.length.should == 1
|
40
|
+
errors.should include("month (13) must be between 1 and 12")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "month, day, and year with out-of-range day is invalid" do
|
44
|
+
errors = new_kronos(2005, 4, 32).errors
|
45
|
+
errors.length.should == 1
|
46
|
+
errors.should include("day (32) must be between 1 and 31")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "June 0, 2005 is invalid" do
|
50
|
+
errors = new_kronos(2005, 6, 0).errors
|
51
|
+
errors.length.should == 1
|
52
|
+
errors.should include("day (0) must be between 1 and 31")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "June 31, 2005 is invalid" do
|
56
|
+
errors = new_kronos(2005, 6, 31).errors
|
57
|
+
errors.length.should == 1
|
58
|
+
errors.should include("date (2005-6-31) must be valid")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "February 29, 2008 is valid" do
|
62
|
+
new_kronos(2008, 2, 29).errors.should == []
|
63
|
+
end
|
64
|
+
|
65
|
+
it "February 29, 2010 is invalid" do
|
66
|
+
errors = new_kronos(2010, 2, 29).errors
|
67
|
+
errors.length.should == 1
|
68
|
+
errors.should include("date (2010-2-29) must be valid")
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kronos
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 10
|
10
|
+
version: 0.1.10
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- David James
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- lib/kronos.rb
|
100
100
|
- lib/version.rb
|
101
101
|
- spec/compare_spec.rb
|
102
|
+
- spec/errors_spec.rb
|
102
103
|
- spec/from_hash_spec.rb
|
103
104
|
- spec/parse_spec.rb
|
104
105
|
- spec/spec.opts
|
@@ -141,6 +142,7 @@ specification_version: 3
|
|
141
142
|
summary: Simple and flexible date parsing.
|
142
143
|
test_files:
|
143
144
|
- spec/compare_spec.rb
|
145
|
+
- spec/errors_spec.rb
|
144
146
|
- spec/from_hash_spec.rb
|
145
147
|
- spec/parse_spec.rb
|
146
148
|
- spec/spec_helper.rb
|