pbyrne-valid-date 1.0.1 → 1.1.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.
Files changed (4) hide show
  1. data/README +13 -7
  2. data/lib/valid-date.rb +23 -0
  3. data/spec/valid-date_spec.rb +26 -0
  4. metadata +3 -2
data/README CHANGED
@@ -1,13 +1,19 @@
1
- Adds String#valid_date? method to add utility to the Rails String#to_date method. Determines whether the string contains a valid date, to check before converting to a date and avoiding an exception.
1
+ Adds String#valid_date? and String#valid_time? methods to add utility to the Rails String#to_date and String#to_time methods. Determines whether the string contains a valid date or time, to check before converting to a date and avoiding an exception.
2
2
 
3
3
  Examples:
4
4
 
5
- "2008-01-01".valid_date? #=> true
6
- "20080101".valid_date? #=> true
7
- "0000-00-00".valid_date? #=> false
8
- "".valid_date? #=> nil
5
+ "2008-01-01".valid_date? #=> true
6
+ "20080101".valid_date? #=> true
7
+ "0000-00-00".valid_date? #=> false
8
+ "".valid_date? #=> nil
9
+
10
+ "2008-01-01".valid_time? #=> true
11
+ "20080101".valid_time? #=> true
12
+ "2008-01-01 12:00".valid_time #=> true
13
+ "2008-01-01 52:99".valid_time #=> false
14
+ "0000-00-00".valid_time? #=> false
15
+ "".valid_time? #=> nil
16
+
9
17
 
10
18
 
11
19
  This gem is released under a broad open-source license. See LICENSE for more details.
12
-
13
- # TODO consider switching from manual gemspec to using jeweler (the recommended tool by github to manage the gemspec)
@@ -22,4 +22,27 @@ class String
22
22
 
23
23
  result
24
24
  end
25
+
26
+ #
27
+ # Determines whether the string contains a valid time for use with the Rails String#to_time method.
28
+ #
29
+ # "2008-01-01".valid_time? #=> true
30
+ # "20080101".valid_time? #=> true
31
+ # "2008-01-01 12:00".valid_time #=> true
32
+ # "2008-01-01 52:99".valid_time #=> false
33
+ # "0000-00-00".valid_time? #=> false
34
+ # "".valid_time? #=> nil
35
+ #
36
+ def valid_time?
37
+ unless self == ''
38
+ begin
39
+ date = self.to_time
40
+ result = true
41
+ rescue Exception => e
42
+ result = false
43
+ end
44
+ end
45
+
46
+ result
47
+ end
25
48
  end
@@ -20,4 +20,30 @@ describe String do
20
20
  s.valid_date?.should be_nil
21
21
  end
22
22
  end
23
+
24
+ context "knowing whether it contains a valid time" do
25
+ it "returns true when contains a valid time" do
26
+ s = "2008-10-25"
27
+ t = "2008-10-25 12:35"
28
+ u = "2008-10-25 12:35:40"
29
+
30
+ s.valid_time?.should be_true
31
+ t.valid_time?.should be_true
32
+ u.valid_time?.should be_true
33
+ end
34
+
35
+ it "returns false when contains an invalid time" do
36
+ s = "0000-00-00"
37
+ t = "2008-10-25 36:99"
38
+
39
+ s.valid_time?.should be_false
40
+ t.valid_time?.should be_false
41
+ end
42
+
43
+ it "returns nil when is empty" do
44
+ s = ""
45
+
46
+ s.valid_time?.should be_nil
47
+ end
48
+ end
23
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pbyrne-valid-date
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Byrne
@@ -35,6 +35,7 @@ files:
35
35
  - README
36
36
  has_rdoc: true
37
37
  homepage: http://github.com/pbyrne/valid-date/
38
+ licenses:
38
39
  post_install_message:
39
40
  rdoc_options: []
40
41
 
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  requirements: []
56
57
 
57
58
  rubyforge_project:
58
- rubygems_version: 1.2.0
59
+ rubygems_version: 1.3.5
59
60
  signing_key:
60
61
  specification_version: 2
61
62
  summary: Adds String#valid_date? method to add utility to the Rails String#to_date method. Determines whether the string contains a valid date, to check before converting to a date and avoiding an exception.