kronos 0.1.10 → 0.2.0
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/lib/kronos.rb +10 -6
- data/lib/version.rb +2 -2
- data/spec/from_hash_spec.rb +4 -1
- data/spec/parse_spec.rb +0 -45
- data/spec/to_hash_spec.rb +46 -0
- metadata +6 -4
data/lib/kronos.rb
CHANGED
@@ -3,6 +3,9 @@ require 'parsedate'
|
|
3
3
|
|
4
4
|
class Kronos
|
5
5
|
|
6
|
+
class Exception < RuntimeError; end
|
7
|
+
class Invalid < Exception; end
|
8
|
+
|
6
9
|
attr_accessor :year, :month, :day
|
7
10
|
|
8
11
|
IGNORE_PATTERN = Regexp.compile("^prior")
|
@@ -33,7 +36,7 @@ class Kronos
|
|
33
36
|
|
34
37
|
def to_s
|
35
38
|
s = ""
|
36
|
-
raise
|
39
|
+
raise Invalid, errors unless valid?
|
37
40
|
s << "%04i" % year if year
|
38
41
|
s << "-%02i" % month if month
|
39
42
|
s << "-%02i" % day if day
|
@@ -97,11 +100,11 @@ class Kronos
|
|
97
100
|
elsif self.day == other.day
|
98
101
|
false
|
99
102
|
else
|
100
|
-
raise "
|
103
|
+
raise Exception, "unexpected error"
|
101
104
|
end
|
102
105
|
end
|
103
106
|
else
|
104
|
-
raise "
|
107
|
+
raise Exception, "unexpected error"
|
105
108
|
end
|
106
109
|
end
|
107
110
|
|
@@ -143,15 +146,16 @@ class Kronos
|
|
143
146
|
elsif self.day == other.day
|
144
147
|
false
|
145
148
|
else
|
146
|
-
raise "
|
149
|
+
raise Exception, "unexpected error"
|
147
150
|
end
|
148
151
|
end
|
149
152
|
else
|
150
|
-
raise "
|
153
|
+
raise Exception, "unexpected error"
|
151
154
|
end
|
152
155
|
end
|
153
156
|
|
154
157
|
def to_hash
|
158
|
+
raise Invalid, errors unless valid?
|
155
159
|
h = {}
|
156
160
|
h['year'] = year if year
|
157
161
|
h['month'] = month if month
|
@@ -164,7 +168,7 @@ class Kronos
|
|
164
168
|
o.year = h['year'] if h['year']
|
165
169
|
o.month = h['month'] if h['month']
|
166
170
|
o.day = h['day'] if h['day']
|
167
|
-
o
|
171
|
+
o
|
168
172
|
end
|
169
173
|
|
170
174
|
def self.parse(string)
|
data/lib/version.rb
CHANGED
data/spec/from_hash_spec.rb
CHANGED
data/spec/parse_spec.rb
CHANGED
@@ -7,11 +7,6 @@ describe "Kronos" do
|
|
7
7
|
c.year.should == 2001
|
8
8
|
c.month.should == 3
|
9
9
|
c.day.should == 15
|
10
|
-
c.to_hash.should == {
|
11
|
-
'year' => 2001,
|
12
|
-
'month' => 3,
|
13
|
-
'day' => 15,
|
14
|
-
}
|
15
10
|
end
|
16
11
|
|
17
12
|
it "January 1976" do
|
@@ -19,10 +14,6 @@ describe "Kronos" do
|
|
19
14
|
c.year.should == 1976
|
20
15
|
c.month.should == 1
|
21
16
|
c.day.should == nil
|
22
|
-
c.to_hash.should == {
|
23
|
-
'year' => 1976,
|
24
|
-
'month' => 1,
|
25
|
-
}
|
26
17
|
end
|
27
18
|
|
28
19
|
it "1991" do
|
@@ -30,9 +21,6 @@ describe "Kronos" do
|
|
30
21
|
c.year.should == 1991
|
31
22
|
c.month.should == nil
|
32
23
|
c.day.should == nil
|
33
|
-
c.to_hash.should == {
|
34
|
-
'year' => 1991,
|
35
|
-
}
|
36
24
|
end
|
37
25
|
|
38
26
|
it "91" do
|
@@ -40,9 +28,6 @@ describe "Kronos" do
|
|
40
28
|
c.year.should == 1991
|
41
29
|
c.month.should == nil
|
42
30
|
c.day.should == nil
|
43
|
-
c.to_hash.should == {
|
44
|
-
'year' => 1991,
|
45
|
-
}
|
46
31
|
end
|
47
32
|
|
48
33
|
it "'91" do
|
@@ -50,9 +35,6 @@ describe "Kronos" do
|
|
50
35
|
c.year.should == 1991
|
51
36
|
c.month.should == nil
|
52
37
|
c.day.should == nil
|
53
|
-
c.to_hash.should == {
|
54
|
-
'year' => 1991,
|
55
|
-
}
|
56
38
|
end
|
57
39
|
|
58
40
|
it "2019" do
|
@@ -60,9 +42,6 @@ describe "Kronos" do
|
|
60
42
|
c.year.should == 2019
|
61
43
|
c.month.should == nil
|
62
44
|
c.day.should == nil
|
63
|
-
c.to_hash.should == {
|
64
|
-
'year' => 2019,
|
65
|
-
}
|
66
45
|
end
|
67
46
|
|
68
47
|
it "19" do
|
@@ -70,9 +49,6 @@ describe "Kronos" do
|
|
70
49
|
c.year.should == 2019
|
71
50
|
c.month.should == nil
|
72
51
|
c.day.should == nil
|
73
|
-
c.to_hash.should == {
|
74
|
-
'year' => 2019,
|
75
|
-
}
|
76
52
|
end
|
77
53
|
|
78
54
|
it "'19" do
|
@@ -80,9 +56,6 @@ describe "Kronos" do
|
|
80
56
|
c.year.should == 2019
|
81
57
|
c.month.should == nil
|
82
58
|
c.day.should == nil
|
83
|
-
c.to_hash.should == {
|
84
|
-
'year' => 2019,
|
85
|
-
}
|
86
59
|
end
|
87
60
|
|
88
61
|
it "1/17/2007" do
|
@@ -90,11 +63,6 @@ describe "Kronos" do
|
|
90
63
|
c.year.should == 2007
|
91
64
|
c.month.should == 1
|
92
65
|
c.day.should == 17
|
93
|
-
c.to_hash.should == {
|
94
|
-
'year' => 2007,
|
95
|
-
'month' => 1,
|
96
|
-
'day' => 17,
|
97
|
-
}
|
98
66
|
end
|
99
67
|
|
100
68
|
it "Aug-96" do
|
@@ -102,10 +70,6 @@ describe "Kronos" do
|
|
102
70
|
c.year.should == 1996
|
103
71
|
c.month.should == 8
|
104
72
|
c.day.should == nil
|
105
|
-
c.to_hash.should == {
|
106
|
-
'year' => 1996,
|
107
|
-
'month' => 8,
|
108
|
-
}
|
109
73
|
end
|
110
74
|
|
111
75
|
it "15-Mar-96" do
|
@@ -113,11 +77,6 @@ describe "Kronos" do
|
|
113
77
|
c.year.should == 1996
|
114
78
|
c.month.should == 3
|
115
79
|
c.day.should == 15
|
116
|
-
c.to_hash.should == {
|
117
|
-
'year' => 1996,
|
118
|
-
'month' => 3,
|
119
|
-
'day' => 15,
|
120
|
-
}
|
121
80
|
end
|
122
81
|
|
123
82
|
it "nil" do
|
@@ -125,7 +84,6 @@ describe "Kronos" do
|
|
125
84
|
c.year.should == nil
|
126
85
|
c.month.should == nil
|
127
86
|
c.day.should == nil
|
128
|
-
c.to_hash.should == {}
|
129
87
|
end
|
130
88
|
|
131
89
|
it "empty string" do
|
@@ -133,7 +91,6 @@ describe "Kronos" do
|
|
133
91
|
c.year.should == nil
|
134
92
|
c.month.should == nil
|
135
93
|
c.day.should == nil
|
136
|
-
c.to_hash.should == {}
|
137
94
|
end
|
138
95
|
|
139
96
|
it "unknown" do
|
@@ -141,7 +98,6 @@ describe "Kronos" do
|
|
141
98
|
c.year.should == nil
|
142
99
|
c.month.should == nil
|
143
100
|
c.day.should == nil
|
144
|
-
c.to_hash.should == {}
|
145
101
|
end
|
146
102
|
|
147
103
|
it "prior to 1998" do
|
@@ -149,7 +105,6 @@ describe "Kronos" do
|
|
149
105
|
c.year.should == nil
|
150
106
|
c.month.should == nil
|
151
107
|
c.day.should == nil
|
152
|
-
c.to_hash.should == {}
|
153
108
|
end
|
154
109
|
|
155
110
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "to_hash" 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 "2007-1-17" do
|
14
|
+
new_kronos(2007, 1, 17).to_hash.should == {
|
15
|
+
'year' => 2007,
|
16
|
+
'month' => 1,
|
17
|
+
'day' => 17,
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
it "1996-8" do
|
22
|
+
new_kronos(1996, 8).to_hash.should == {
|
23
|
+
'year' => 1996,
|
24
|
+
'month' => 8,
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
it "1980" do
|
29
|
+
new_kronos(1980).to_hash.should == {
|
30
|
+
'year' => 1980,
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
it "nil, nil, nil" do
|
35
|
+
lambda do
|
36
|
+
new_kronos(nil, nil, nil).to_hash.should raise(Kronos::Invalid)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "nil, 7" do
|
41
|
+
lambda do
|
42
|
+
new_kronos(nil, 7).to_hash.should raise(Kronos::Invalid)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- David James
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- spec/parse_spec.rb
|
105
105
|
- spec/spec.opts
|
106
106
|
- spec/spec_helper.rb
|
107
|
+
- spec/to_hash_spec.rb
|
107
108
|
- spec/to_s_spec.rb
|
108
109
|
- spec/valid_spec.rb
|
109
110
|
has_rdoc: true
|
@@ -146,5 +147,6 @@ test_files:
|
|
146
147
|
- spec/from_hash_spec.rb
|
147
148
|
- spec/parse_spec.rb
|
148
149
|
- spec/spec_helper.rb
|
150
|
+
- spec/to_hash_spec.rb
|
149
151
|
- spec/to_s_spec.rb
|
150
152
|
- spec/valid_spec.rb
|