tod 1.3.0 → 1.4.0

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: 853fd23075cb8c0131b49bae9b571249cf01f940
4
- data.tar.gz: 8fcb950ad2f427b980232962831d6ac53c25353a
3
+ metadata.gz: 859e21d92e38e6ed8e88f2ccde4ba7b65ece6386
4
+ data.tar.gz: 7e329ea20c81ffb0cd5018d4cd008f4e257a9cfc
5
5
  SHA512:
6
- metadata.gz: 07c301bb3668574f5d1129d7fcaed0109c50e1a9e85ec663da88d2f863831c703b9f30160cd5078a4d8b7807cb875f8fdb54162551ff2facac491075d0eb445f
7
- data.tar.gz: 49b0c82abd08efa118b80cd7c63b3d5c1c034a4e856ac79eea31c626034c868c9683ea825d6c4d6ab159331e19657d7a703eabc7d6aad1a286ac1796ec2e61d5
6
+ metadata.gz: f7cff04a19eaca1af690f985541192e0070e6a5e0a11b25a5ed03ac10f149180f3e3df9ff19bdb55a58fe9f39878c2fb987313f42b202a8ec73f323306167240
7
+ data.tar.gz: 60a3e8f699de9196d90c1d99027b8cd9e6f6c3c4b6d4f3259455439ddaa15b0d9203ea8af53c91bbe4a6a6d238e3d71af1757e7d765a3e6790af470ea8501a48
data/README.markdown CHANGED
@@ -45,6 +45,18 @@ are all optional.
45
45
  TimeOfDay.parse "515p" # => 17:15:00
46
46
  TimeOfDay.parse "151253" # => 15:12:53
47
47
 
48
+ TimeOfDay.parse raises an ArgumentError is the argument to parse is not
49
+ parsable. TimeOfDay.try_parse will instead return nil if the argument is not
50
+ parsable.
51
+
52
+ TimeOfDay.try_parse "3:30pm" # => 15:30:00
53
+ TimeOfDay.try_parse "foo" # => nil
54
+
55
+ Values can be tested with TimeOfDay.parsable? to see if they can be parsed.
56
+
57
+ TimeOfDay.parsable? "3:30pm" # => true
58
+ TimeOfDay.parsable? "foo" # => false
59
+
48
60
  Adding or subtracting time
49
61
  -----------------------------
50
62
 
@@ -87,6 +99,16 @@ then require 'tod/time_of_day' instead of 'tod'.
87
99
  Time.now.to_time_of_day # => 16:30:43
88
100
  DateTime.now.to_time_of_day # => 16:30:43
89
101
 
102
+ Conversion method
103
+ -----------------
104
+
105
+ Tod provides a conversion method which will handle a variety of input types:
106
+
107
+ TimeOfDay(TimeOfDay.new(8, 30)) # => 08:30:00
108
+ TimeOfDay("09:45") # => 09:45:00
109
+ TimeOfDay(Time.new(2014, 1, 1, 12, 30)) # => 12:30:00
110
+ TimeOfDay(Date.new(2014, 1, 1)) # => 00:00:00
111
+
90
112
 
91
113
  Shifts
92
114
  =======================
@@ -144,6 +166,11 @@ Tod is compatible with Ruby 1.9.3 and 2.0.0 and Rails 3.0, 3.1, 3.2, and 4.0.
144
166
  History
145
167
  =======
146
168
 
169
+ ## 1.4.0 (April 3, 2014)
170
+
171
+ * Add try_parse (Stuart Olivera)
172
+ * Add parse? (Stuart Olivera)
173
+
147
174
  ## 1.3.0 (December 9, 2013)
148
175
 
149
176
  * Add Shift class (Pablo Russo)
@@ -0,0 +1,15 @@
1
+ module Tod
2
+ module Conversions
3
+ module_function
4
+
5
+ def TimeOfDay(obj_or_string)
6
+ if obj_or_string.is_a?(TimeOfDay)
7
+ obj_or_string
8
+ elsif obj_or_string.respond_to?(:to_time_of_day)
9
+ obj_or_string.to_time_of_day
10
+ else
11
+ TimeOfDay.parse(obj_or_string)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module Tod
2
+ module Mongoization
3
+
4
+ # Converts an object of this instance into a database friendly value.
5
+ def mongoize
6
+ to_s
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ # Get the object as it was stored in the database, and instantiate
12
+ # this custom class from it.
13
+ def demongoize(object)
14
+ Tod::TimeOfDay.parse(object) if object
15
+ end
16
+
17
+ # Takes any possible object and converts it to how it would be
18
+ # stored in the database.
19
+ def mongoize(object)
20
+ case object
21
+ when TimeOfDay then object.mongoize
22
+ else object
23
+ end
24
+ end
25
+
26
+ # Converts the object that was supplied to a criteria and converts it
27
+ # into a database friendly form.
28
+ def evolve(object)
29
+ case object
30
+ when TimeOfDay then object.mongoize
31
+ else object
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -113,6 +113,15 @@ module Tod
113
113
  # TimeOfDay.parse "515p" # => 17:15:00
114
114
  # TimeOfDay.parse "151253" # => 15:12:53
115
115
  def self.parse(tod_string)
116
+ try_parse(tod_string) || (raise ArgumentError, "Invalid time of day string")
117
+ end
118
+
119
+ # Same as parse(), but return nil if not parsable (instead of raising an error)
120
+ # TimeOfDay.try_parse "8am" # => 08:00:00
121
+ # TimeOfDay.try_parse "" # => nil
122
+ # TimeOfDay.try_parse "abc" # => nil
123
+ def self.try_parse(tod_string)
124
+ tod_string = tod_string.to_s
116
125
  tod_string = tod_string.strip
117
126
  tod_string = tod_string.downcase
118
127
  if PARSE_24H_REGEX =~ tod_string || PARSE_12H_REGEX =~ tod_string
@@ -125,10 +134,17 @@ module Tod
125
134
 
126
135
  new hour, minute, second
127
136
  else
128
- raise ArgumentError, "Invalid time of day string"
137
+ nil
129
138
  end
130
139
  end
131
140
 
141
+ # Determine if a string is parsable into a TimeOfDay instance
142
+ # TimeOfDay.parsable? "8am" # => true
143
+ # TimeOfDay.parsable? "abc" # => false
144
+ def self.parsable?(tod_string)
145
+ !!try_parse(tod_string)
146
+ end
147
+
132
148
  # If ActiveSupport TimeZone is available and set use current time zone else return Time
133
149
  def self.time_zone
134
150
  (Time.respond_to?(:zone) && Time.zone) || Time
@@ -150,6 +166,4 @@ module Tod
150
166
  end
151
167
  end
152
168
  end
153
- end
154
-
155
- TimeOfDay = Tod::TimeOfDay
169
+ end
data/lib/tod/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tod
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
data/lib/tod.rb CHANGED
@@ -2,3 +2,12 @@ require 'tod/time_of_day'
2
2
  require 'tod/date'
3
3
  require 'tod/time'
4
4
  require 'tod/shift'
5
+ require 'tod/conversions'
6
+ require 'tod/mongoization'
7
+
8
+ include Tod
9
+ include Tod::Conversions
10
+ class TimeOfDay
11
+ include(Tod::Mongoization)
12
+ extend(Tod::Mongoization::ClassMethods)
13
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
2
+ require 'active_support/time'
3
+
4
+ class TimeOfDayConversionTest < Test::Unit::TestCase
5
+ should "handle TimeOfDay" do
6
+ t = TimeOfDay.new(13, 56, 12)
7
+ tod = TimeOfDay(t)
8
+
9
+ assert_equal(t, tod)
10
+ end
11
+ should "handle Time" do
12
+ t = Time.new(2014, 2, 27, 12, 01, 02)
13
+ tod = TimeOfDay(t)
14
+
15
+ assert_equal(tod, TimeOfDay.new(12, 01, 02))
16
+ end
17
+ should "handle Date" do
18
+ t = Date.new(2014, 2, 27)
19
+ tod = TimeOfDay(t)
20
+
21
+ assert_equal(tod, TimeOfDay.new(0, 0, 0))
22
+ end
23
+ should "handle DateTime" do
24
+ t = DateTime.new(2014, 2, 27, 12, 01, 02)
25
+ tod = TimeOfDay(t)
26
+
27
+ assert_equal(tod, TimeOfDay.new(12, 01, 02))
28
+ end
29
+ should "string" do
30
+ t = "12:01:02"
31
+ tod = TimeOfDay(t)
32
+
33
+ assert_equal(tod, TimeOfDay.new(12, 01, 02))
34
+ end
35
+
36
+ end
@@ -42,12 +42,16 @@ class TimeOfDayTest < Test::Unit::TestCase
42
42
  expected_tod = TimeOfDay.new expected_hour, expected_minute, expected_second
43
43
 
44
44
  should "parse '#{parse_string}' into #{expected_tod.inspect}" do
45
+ assert_equal true, TimeOfDay.parsable?(parse_string)
46
+ assert_equal expected_tod, TimeOfDay.try_parse(parse_string)
45
47
  assert_equal expected_tod, TimeOfDay.parse(parse_string)
46
48
  end
47
49
  end
48
50
 
49
51
  def self.should_not_parse(parse_string)
50
52
  should "not parse '#{parse_string}'" do
53
+ assert_equal false, TimeOfDay.parsable?(parse_string)
54
+ assert_equal nil, TimeOfDay.try_parse(parse_string)
51
55
  assert_raise(ArgumentError) { TimeOfDay.parse(parse_string) }
52
56
  end
53
57
  end
@@ -89,6 +93,15 @@ class TimeOfDayTest < Test::Unit::TestCase
89
93
  should_not_parse "13a"
90
94
  should_not_parse "13p"
91
95
  should_not_parse "10.5"
96
+ should_not_parse "abc"
97
+ should_not_parse ""
98
+ should_not_parse []
99
+
100
+ should "not parse 'nil'" do
101
+ assert_equal false, TimeOfDay.parsable?(nil)
102
+ assert_equal nil, TimeOfDay.try_parse(nil)
103
+ assert_raise(ArgumentError) { TimeOfDay.parse(nil) }
104
+ end
92
105
 
93
106
  should "provide spaceship operator" do
94
107
  assert_equal -1, TimeOfDay.new(8,0,0) <=> TimeOfDay.new(9,0,0)
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tod
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Christensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-09 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: shoulda
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mocha
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: tzinfo
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: activerecord
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: 3.0.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: 3.0.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: sqlite3
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: Supplies TimeOfDay and Shift class that includes parsing, strftime, comparison,
@@ -116,8 +116,8 @@ executables: []
116
116
  extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
- - .gitignore
120
- - .travis.yml
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
121
  - Gemfile
122
122
  - Gemfile.lock
123
123
  - MIT-LICENSE
@@ -128,7 +128,9 @@ files:
128
128
  - gemfiles/3.2.gemfile
129
129
  - gemfiles/4.0.gemfile
130
130
  - lib/tod.rb
131
+ - lib/tod/conversions.rb
131
132
  - lib/tod/date.rb
133
+ - lib/tod/mongoization.rb
132
134
  - lib/tod/shift.rb
133
135
  - lib/tod/time.rb
134
136
  - lib/tod/time_of_day.rb
@@ -136,6 +138,7 @@ files:
136
138
  - test/support/active_record.rb
137
139
  - test/test_helper.rb
138
140
  - test/tod/a_a_a_time_of_day_time_zone_without_active_support_test.rb
141
+ - test/tod/conversion_test.rb
139
142
  - test/tod/date_test.rb
140
143
  - test/tod/shift_test.rb
141
144
  - test/tod/time_of_day_serializable_attribute_test.rb
@@ -152,17 +155,17 @@ require_paths:
152
155
  - lib
153
156
  required_ruby_version: !ruby/object:Gem::Requirement
154
157
  requirements:
155
- - - '>='
158
+ - - ">="
156
159
  - !ruby/object:Gem::Version
157
160
  version: '0'
158
161
  required_rubygems_version: !ruby/object:Gem::Requirement
159
162
  requirements:
160
- - - '>='
163
+ - - ">="
161
164
  - !ruby/object:Gem::Version
162
165
  version: '0'
163
166
  requirements: []
164
167
  rubyforge_project:
165
- rubygems_version: 2.0.3
168
+ rubygems_version: 2.2.2
166
169
  signing_key:
167
170
  specification_version: 4
168
171
  summary: Supplies TimeOfDay and Shift class
@@ -170,10 +173,10 @@ test_files:
170
173
  - test/support/active_record.rb
171
174
  - test/test_helper.rb
172
175
  - test/tod/a_a_a_time_of_day_time_zone_without_active_support_test.rb
176
+ - test/tod/conversion_test.rb
173
177
  - test/tod/date_test.rb
174
178
  - test/tod/shift_test.rb
175
179
  - test/tod/time_of_day_serializable_attribute_test.rb
176
180
  - test/tod/time_of_day_test.rb
177
181
  - test/tod/time_of_day_time_zone_with_active_support_test.rb
178
182
  - test/tod/time_test.rb
179
- has_rdoc: