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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41b54691b6c9da7f2e7e3845fcdb59d198112e51
4
- data.tar.gz: d7ad2d6352067154f62d95bff8551f58415fc9cd
3
+ metadata.gz: 7976e9ab3f6b3eecf61c0031b31b6e229ca51c92
4
+ data.tar.gz: 765329e2415c5f5f9e056a4388902a39e9d9f272
5
5
  SHA512:
6
- metadata.gz: 175ceab4331f79108bcfea7340871df4fa11414c5c9b7d0a74d969faa1d71528956ab1f768f9cbcbf1c1f99342ea8e31fbcd7a1dc3605533355fe0785ac336af
7
- data.tar.gz: 665560693fdb34104082723afc9fb9b50564d772cd48142d9d3b8a320a69c7967cca9acb0162d1270f5533e20841fd6953c9752c3542b6d70ae921e6f50e9363
6
+ metadata.gz: 9574ad80bcfe7280c8de4d8b19bd800923d8125ab8ae0b729eebe646f52ff6b009afcfea38600b2239c5d5154535db381aaf9e7c4271bbde1efe6226689bb31e
7
+ data.tar.gz: bb8c55b77390c153327560d4820cb2da76771ee4f773645108d718b39cf692113a5575a59bf932d7559192913086ac540d49eadfcbf75da09c196de1c7bc728b
@@ -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) || !!(value.to_s =~ ISO_8601)
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 to_coerced
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
@@ -1,3 +1,3 @@
1
1
  module Quack
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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
- Quack::Types::Time.matches?(Time.new(2014, 3, 22)).must_equal(true)
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
- Quack::Types::Time.matches?("2014-03-22T03:00:00Z").must_equal(true)
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
- Quack::Types::Time.matches?("2014-03-22T03:00:00-08:00").must_equal(true)
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 false for non ISO 8601 dates" do
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 raise a ParseError for invalid dates" do
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derrick Reimer