quack 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/quack/types/time.rb +38 -3
- data/lib/quack/version.rb +1 -1
- data/spec/quack/types/time_spec.rb +19 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7976e9ab3f6b3eecf61c0031b31b6e229ca51c92
|
4
|
+
data.tar.gz: 765329e2415c5f5f9e056a4388902a39e9d9f272
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9574ad80bcfe7280c8de4d8b19bd800923d8125ab8ae0b729eebe646f52ff6b009afcfea38600b2239c5d5154535db381aaf9e7c4271bbde1efe6226689bb31e
|
7
|
+
data.tar.gz: bb8c55b77390c153327560d4820cb2da76771ee4f773645108d718b39cf692113a5575a59bf932d7559192913086ac540d49eadfcbf75da09c196de1c7bc728b
|
data/lib/quack/types/time.rb
CHANGED
@@ -4,6 +4,12 @@ require "quack/type"
|
|
4
4
|
module Quack
|
5
5
|
module Types
|
6
6
|
class Time < Quack::Type
|
7
|
+
YMD_FORMAT = /
|
8
|
+
(?<year>\d{4})(-|\/)
|
9
|
+
(?<month>\d{2})(-|\/)
|
10
|
+
(?<day>\d{2})
|
11
|
+
/x
|
12
|
+
|
7
13
|
ISO_8601 = /
|
8
14
|
(?<year>\d{4})-
|
9
15
|
(?<month>\d{2})-
|
@@ -21,17 +27,32 @@ module Quack
|
|
21
27
|
[::Time]
|
22
28
|
end
|
23
29
|
|
30
|
+
def iso_8601?(value)
|
31
|
+
!!(value.to_s =~ ISO_8601)
|
32
|
+
end
|
33
|
+
|
34
|
+
def ymd?(value)
|
35
|
+
!!(value.to_s =~ YMD_FORMAT)
|
36
|
+
end
|
37
|
+
|
24
38
|
def matches?(value)
|
25
|
-
already_coerced?(value) ||
|
39
|
+
already_coerced?(value) || iso_8601?(value) || ymd?(value)
|
26
40
|
end
|
27
41
|
end
|
28
42
|
|
43
|
+
def iso_8601?
|
44
|
+
self.class.iso_8601?(value)
|
45
|
+
end
|
46
|
+
|
47
|
+
def ymd?
|
48
|
+
self.class.ymd?(value)
|
49
|
+
end
|
50
|
+
|
29
51
|
def parse_offset(offset)
|
30
52
|
offset == "Z" ? UTC : offset
|
31
53
|
end
|
32
54
|
|
33
|
-
def
|
34
|
-
return value if already_coerced?
|
55
|
+
def parse_iso8601
|
35
56
|
parts = value.to_s.scan(ISO_8601).flatten
|
36
57
|
offset = parse_offset(parts.pop)
|
37
58
|
parts = parts.map(&:to_i) << offset
|
@@ -40,6 +61,20 @@ module Quack
|
|
40
61
|
raise ParseError.new(ex.message)
|
41
62
|
end
|
42
63
|
|
64
|
+
def parse_ymd
|
65
|
+
parts = value.to_s.scan(YMD_FORMAT).flatten
|
66
|
+
parts = parts.map(&:to_i) + [0, 0, 0, UTC]
|
67
|
+
::Time.new(*parts)
|
68
|
+
rescue => ex
|
69
|
+
raise ParseError.new(ex.message)
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_coerced
|
73
|
+
return value if already_coerced?
|
74
|
+
return parse_iso8601 if iso_8601?
|
75
|
+
parse_ymd
|
76
|
+
end
|
77
|
+
|
43
78
|
def to_s
|
44
79
|
to_coerced.iso8601
|
45
80
|
end
|
data/lib/quack/version.rb
CHANGED
@@ -3,18 +3,26 @@ require "test_helper"
|
|
3
3
|
describe Quack::Types::Time do
|
4
4
|
describe ".matches?" do
|
5
5
|
it "should be true for Time objects" do
|
6
|
-
|
6
|
+
input = Time.new(2014, 3, 22)
|
7
|
+
Quack::Types::Time.matches?(input).must_equal(true)
|
7
8
|
end
|
8
9
|
|
9
10
|
it "should be true for ISO 8601 UTC times" do
|
10
|
-
|
11
|
+
input = "2014-03-22T03:00:00Z"
|
12
|
+
Quack::Types::Time.matches?(input).must_equal(true)
|
11
13
|
end
|
12
14
|
|
13
15
|
it "should be true for ISO 8601 non-UTC times" do
|
14
|
-
|
16
|
+
input = "2014-03-22T03:00:00-08:00"
|
17
|
+
Quack::Types::Time.matches?(input).must_equal(true)
|
15
18
|
end
|
16
19
|
|
17
|
-
it "should be
|
20
|
+
it "should be true for YMD formatted times" do
|
21
|
+
input = "2014-03-22"
|
22
|
+
Quack::Types::Time.matches?(input).must_equal(true)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be false for invalid dates" do
|
18
26
|
Quack::Types::Time.matches?("3/22/2014").must_equal(false)
|
19
27
|
end
|
20
28
|
end
|
@@ -32,8 +40,14 @@ describe Quack::Types::Time do
|
|
32
40
|
type.to_coerced.must_equal(expected)
|
33
41
|
end
|
34
42
|
|
35
|
-
it "should
|
43
|
+
it "should cast YMD formatted times" do
|
36
44
|
type = Quack::Types::Time.new("2014-03-22")
|
45
|
+
expected = Time.new(2014, 3, 22, 0, 0, 0, "+00:00")
|
46
|
+
type.to_coerced.must_equal(expected)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should raise a ParseError for invalid dates" do
|
50
|
+
type = Quack::Types::Time.new("foo")
|
37
51
|
proc { type.to_coerced }.must_raise(Quack::ParseError)
|
38
52
|
end
|
39
53
|
end
|