tod 1.5.0 → 2.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb8660843c84b0445056017a9e55c4e24a26759d
4
- data.tar.gz: af34e5365fdcc5fed74129ffcf1c46dd5476414a
3
+ metadata.gz: 7ea9182b8d4d70181c761c96a87a97bc3ac8c316
4
+ data.tar.gz: 846e0db24fb222b1e445e50eb5a6623b020ee82f
5
5
  SHA512:
6
- metadata.gz: 091d891b412086559a229fa04407f200315b01fc39f0a79c7305a0847b865f8d4e20252d53fe77bd0f1290d1b0cceea31f90d681f00a1bb8a39c2f2bd3942ea8
7
- data.tar.gz: f29c38d109354008fad673e1ad7c3afcb78510992c70f96b4bfe8aab512a3b132e8d6a6674e185aa8aed4ae46dec39ae44cfb840169d82459543a3b38ed8de1c
6
+ metadata.gz: 50d7a807b6cbee926593c019abba1ee47f2c9f13ca00f1cf85a7a1213a70951db5b490b02ceb10e93b40784d9243ba71ee68fa0f3ed6d51c327cc687abae066e
7
+ data.tar.gz: abdea735146667ace0f136b4040b2f9fd762ec1e2179febd7e51e608ef68827c62bdd720a2222aba77a1e3d8a0249979fce3bc5c83e491d05d44448935938c10
@@ -1,3 +1,10 @@
1
+ # 2.0.0 (April 18, 2015)
2
+
3
+ * Shift now supports exclusive ends like a Ruby range
4
+ * Add Shift#overlaps? (Michael Amor Righi)
5
+ * Add Shift#contains? (Michael Amor Righi)
6
+ * Do not pollute global namespace
7
+
1
8
  # 1.5.0 (January 15, 2015)
2
9
 
3
10
  * Fix: return nil unless other is comparable (Peter Yates)
@@ -1,52 +1,42 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tod (1.4.0)
4
+ tod (1.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activemodel (3.2.13)
10
- activesupport (= 3.2.13)
11
- builder (~> 3.0.0)
12
- activerecord (3.2.13)
13
- activemodel (= 3.2.13)
14
- activesupport (= 3.2.13)
15
- arel (~> 3.0.2)
16
- tzinfo (~> 0.3.29)
17
- activesupport (3.2.13)
18
- i18n (= 0.6.1)
19
- multi_json (~> 1.0)
20
- arel (3.0.2)
21
- bourne (1.4.0)
22
- mocha (~> 0.13.2)
23
- builder (3.0.4)
24
- i18n (0.6.1)
25
- metaclass (0.0.1)
26
- mocha (0.13.3)
27
- metaclass (~> 0.0.1)
28
- multi_json (1.7.2)
29
- rake (10.0.4)
30
- shoulda (3.4.0)
31
- shoulda-context (~> 1.0, >= 1.0.1)
32
- shoulda-matchers (~> 1.0, >= 1.4.1)
33
- shoulda-context (1.1.1)
34
- shoulda-matchers (1.5.6)
35
- activesupport (>= 3.0.0)
36
- bourne (~> 1.3)
37
- sqlite3 (1.3.7)
38
- test-unit (2.5.4)
39
- tzinfo (0.3.37)
9
+ activemodel (4.2.0)
10
+ activesupport (= 4.2.0)
11
+ builder (~> 3.1)
12
+ activerecord (4.2.0)
13
+ activemodel (= 4.2.0)
14
+ activesupport (= 4.2.0)
15
+ arel (~> 6.0)
16
+ activesupport (4.2.0)
17
+ i18n (~> 0.7)
18
+ json (~> 1.7, >= 1.7.7)
19
+ minitest (~> 5.1)
20
+ thread_safe (~> 0.3, >= 0.3.4)
21
+ tzinfo (~> 1.1)
22
+ arel (6.0.0)
23
+ builder (3.2.2)
24
+ i18n (0.7.0)
25
+ json (1.8.2)
26
+ minitest (5.5.1)
27
+ rake (10.4.2)
28
+ sqlite3 (1.3.10)
29
+ thread_safe (0.3.4)
30
+ tzinfo (1.2.2)
31
+ thread_safe (~> 0.1)
40
32
 
41
33
  PLATFORMS
42
34
  ruby
43
35
 
44
36
  DEPENDENCIES
45
37
  activerecord (>= 3.0.0)
46
- mocha
38
+ minitest
47
39
  rake
48
- shoulda
49
40
  sqlite3
50
- test-unit
51
41
  tod!
52
42
  tzinfo
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Jack Christensen
1
+ Copyright (c) 2010-2015 Jack Christensen
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -24,8 +24,8 @@ Loading Tod
24
24
  Creating from hour, minute, and second
25
25
  --------------------------------------
26
26
 
27
- TimeOfDay.new 8 # => 08:00:00
28
- TimeOfDay.new 8, 15, 30 # => 08:15:30
27
+ Tod::TimeOfDay.new 8 # => 08:00:00
28
+ Tod::TimeOfDay.new 8, 15, 30 # => 08:15:30
29
29
 
30
30
  Parsing text
31
31
  ------------
@@ -33,70 +33,74 @@ Parsing text
33
33
  Strings only need to contain an hour. Minutes, seconds, AM or PM, and colons
34
34
  are all optional.
35
35
 
36
- TimeOfDay.parse "8" # => 08:00:00
37
- TimeOfDay.parse "8am" # => 08:00:00
38
- TimeOfDay.parse "8pm" # => 20:00:00
39
- TimeOfDay.parse "8p" # => 20:00:00
40
- TimeOfDay.parse "9:30" # => 09:30:00
41
- TimeOfDay.parse "15:30" # => 15:30:00
42
- TimeOfDay.parse "3:30pm" # => 15:30:00
43
- TimeOfDay.parse "1230" # => 12:30:00
44
- TimeOfDay.parse "3:25:58" # => 03:25:58
45
- TimeOfDay.parse "515p" # => 17:15:00
46
- TimeOfDay.parse "151253" # => 15:12:53
47
- TimeOfDay.parse "noon" # => 12:00:00
48
- TimeOfDay.parse "midnight" # => 00:00:00
49
-
50
- TimeOfDay.parse raises an ArgumentError is the argument to parse is not
51
- parsable. TimeOfDay.try_parse will instead return nil if the argument is not
36
+ Tod::TimeOfDay.parse "8" # => 08:00:00
37
+ Tod::TimeOfDay.parse "8am" # => 08:00:00
38
+ Tod::TimeOfDay.parse "8pm" # => 20:00:00
39
+ Tod::TimeOfDay.parse "8p" # => 20:00:00
40
+ Tod::TimeOfDay.parse "9:30" # => 09:30:00
41
+ Tod::TimeOfDay.parse "15:30" # => 15:30:00
42
+ Tod::TimeOfDay.parse "3:30pm" # => 15:30:00
43
+ Tod::TimeOfDay.parse "1230" # => 12:30:00
44
+ Tod::TimeOfDay.parse "3:25:58" # => 03:25:58
45
+ Tod::TimeOfDay.parse "515p" # => 17:15:00
46
+ Tod::TimeOfDay.parse "151253" # => 15:12:53
47
+ Tod::TimeOfDay.parse "noon" # => 12:00:00
48
+ Tod::TimeOfDay.parse "midnight" # => 00:00:00
49
+
50
+ Tod::TimeOfDay.parse raises an ArgumentError if the argument to parse is not
51
+ parsable. Tod::TimeOfDay.try_parse will instead return nil if the argument is not
52
52
  parsable.
53
53
 
54
- TimeOfDay.try_parse "3:30pm" # => 15:30:00
55
- TimeOfDay.try_parse "foo" # => nil
54
+ Tod::TimeOfDay.try_parse "3:30pm" # => 15:30:00
55
+ Tod::TimeOfDay.try_parse "foo" # => nil
56
56
 
57
- Values can be tested with TimeOfDay.parsable? to see if they can be parsed.
57
+ Values can be tested with Tod::TimeOfDay.parsable? to see if they can be parsed.
58
58
 
59
- TimeOfDay.parsable? "3:30pm" # => true
60
- TimeOfDay.parsable? "foo" # => false
59
+ Tod::TimeOfDay.parsable? "3:30pm" # => true
60
+ Tod::TimeOfDay.parsable? "foo" # => false
61
61
 
62
62
  Adding or subtracting time
63
63
  -----------------------------
64
64
 
65
- Seconds can be added to or subtracted TimeOfDay objects. Time correctly wraps
65
+ Seconds can be added to or subtracted Tod::TimeOfDay objects. Time correctly wraps
66
66
  around midnight.
67
67
 
68
- TimeOfDay.new(8) + 3600 # => 09:00:00
69
- TimeOfDay.new(8) - 3600 # => 07:00:00
70
- TimeOfDay.new(0) - 30 # => 23:59:30
71
- TimeOfDay.new(23,59,45) + 30 # => 00:00:15
68
+ Tod::TimeOfDay.new(8) + 3600 # => 09:00:00
69
+ Tod::TimeOfDay.new(8) - 3600 # => 07:00:00
70
+ Tod::TimeOfDay.new(0) - 30 # => 23:59:30
71
+ Tod::TimeOfDay.new(23,59,45) + 30 # => 00:00:15
72
72
 
73
73
  Comparing
74
74
  --------------------
75
75
 
76
- TimeOfDay includes Comparable.
76
+ Tod::TimeOfDay includes Comparable.
77
77
 
78
- TimeOfDay.new(8) < TimeOfDay.new(9) # => true
79
- TimeOfDay.new(8) == TimeOfDay.new(9) # => false
80
- TimeOfDay.new(9) == TimeOfDay.new(9) # => true
81
- TimeOfDay.new(10) > TimeOfDay.new(9) # => true
78
+ Tod::TimeOfDay.new(8) < Tod::TimeOfDay.new(9) # => true
79
+ Tod::TimeOfDay.new(8) == Tod::TimeOfDay.new(9) # => false
80
+ Tod::TimeOfDay.new(9) == Tod::TimeOfDay.new(9) # => true
81
+ Tod::TimeOfDay.new(10) > Tod::TimeOfDay.new(9) # => true
82
82
 
83
83
  Formatting
84
84
  ----------
85
85
 
86
86
  Format strings are passed to Time#strftime.
87
87
 
88
- TimeOfDay.new(8,30).strftime("%H:%M") # => "08:30"
89
- TimeOfDay.new(17,15).strftime("%I:%M %p") # => "05:15 PM"
90
- TimeOfDay.new(22,5,15).strftime("%I:%M:%S %p") # => "10:05:15 PM"
88
+ Tod::TimeOfDay.new(8,30).strftime("%H:%M") # => "08:30"
89
+ Tod::TimeOfDay.new(17,15).strftime("%I:%M %p") # => "05:15 PM"
90
+ Tod::TimeOfDay.new(22,5,15).strftime("%I:%M:%S %p") # => "10:05:15 PM"
91
91
 
92
92
  Convenience methods for dates and times
93
93
  ---------------------------------------
94
94
 
95
- Tod adds Date#on and Time#to_time_of_day. If you do not want the core extensions
96
- then require 'tod/time_of_day' instead of 'tod'.
95
+ Pass a date to Tod::TimeOfDay#on and it will return a time with that date and time.
97
96
 
98
- tod = TimeOfDay.new 8, 30 # => 08:30:00
97
+ tod = Tod::TimeOfDay.new 8, 30 # => 08:30:00
99
98
  tod.on Date.today # => 2010-12-29 08:30:00 -0600
99
+
100
+ Tod offers Date#at and Time#to_time_of_day. Require 'tod/core_extensions' to enable.
101
+
102
+ require 'tod/core_extensions'
103
+ tod = Tod::TimeOfDay.new 8, 30 # => 08:30:00
100
104
  Date.today.at tod # => 2010-12-29 08:30:00 -0600
101
105
  Time.now.to_time_of_day # => 16:30:43
102
106
  DateTime.now.to_time_of_day # => 16:30:43
@@ -106,57 +110,132 @@ Conversion method
106
110
 
107
111
  Tod provides a conversion method which will handle a variety of input types:
108
112
 
109
- TimeOfDay(TimeOfDay.new(8, 30)) # => 08:30:00
110
- TimeOfDay("09:45") # => 09:45:00
111
- TimeOfDay(Time.new(2014, 1, 1, 12, 30)) # => 12:30:00
112
- TimeOfDay(Date.new(2014, 1, 1)) # => 00:00:00
113
+ Tod::TimeOfDay(Tod::TimeOfDay.new(8, 30)) # => 08:30:00
114
+ Tod::TimeOfDay("09:45") # => 09:45:00
115
+ Tod::TimeOfDay(Time.new(2014, 1, 1, 12, 30)) # => 12:30:00
116
+ Tod::TimeOfDay(Date.new(2014, 1, 1)) # => 00:00:00
113
117
 
114
118
 
115
119
  Shifts
116
120
  =======================
117
121
 
118
- Represents a period of time, using a beginning and ending TimeOfDay. Allows to calculate its duration and
119
- to determine if a TimeOfDay is included inside the shift. For nightly shifts (when beginning time is greater than ending time),
120
- it supposes the shift ends the following day.
122
+ Tod::Shift is a range-like object that represents a period of time, using a
123
+ beginning and ending Tod::TimeOfDay. Allows to calculate its duration and to
124
+ determine if a Tod::TimeOfDay is included inside the shift. For nightly shifts
125
+ (when beginning time is greater than ending time), it supposes the shift ends
126
+ the following day. Tod::Shift behaves like a Ruby range in that it defaults to
127
+ inclusive endings. For exclusive endings, pass true as the third argument
128
+ (like a Ruby range).
121
129
 
122
- Creating from TimeOfDay
130
+ Creating from Tod::TimeOfDay
123
131
  --------------------------------------
124
132
 
125
- Shift.new(TimeOfDay.new(9), TimeOfDay.new(17))
126
- Shift.new(TimeOfDay.new(22), TimeOfDay.new(4))
133
+ Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(17))
134
+ Tod::Shift.new(Tod::TimeOfDay.new(22), Tod::TimeOfDay.new(4))
127
135
 
128
136
  Duration
129
137
  --------------------
130
138
 
131
- Shift.new(TimeOfDay.new(9), TimeOfDay.new(17)).duration # => 28800
132
- Shift.new(TimeOfDay.new(20), TimeOfDay.new(2)).duration # => 21600
139
+ Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(17)).duration # => 28800
140
+ Tod::Shift.new(Tod::TimeOfDay.new(20), Tod::TimeOfDay.new(2)).duration # => 21600
133
141
 
134
142
  Include?
135
143
  --------------------
136
144
 
137
- Shift.new(TimeOfDay.new(9), TimeOfDay.new(17)).include?(TimeOfDay.new(12)) # => true
138
- Shift.new(TimeOfDay.new(9), TimeOfDay.new(17)).include?(TimeOfDay.new(7)) # => false
139
- Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(2)) # => true
140
- Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(18)) # => false
145
+ Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(17)).include?(Tod::TimeOfDay.new(12)) # => true
146
+ Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(17)).include?(Tod::TimeOfDay.new(7)) # => false
147
+ Tod::Shift.new(Tod::TimeOfDay.new(20), Tod::TimeOfDay.new(4)).include?(Tod::TimeOfDay.new(2)) # => true
148
+ Tod::Shift.new(Tod::TimeOfDay.new(20), Tod::TimeOfDay.new(4)).include?(Tod::TimeOfDay.new(18)) # => false
149
+
150
+ #include? respects exclusive endings.
151
+
152
+ Tod::Shift.new(Tod::TimeOfDay.new(5), Tod::TimeOfDay.new(9)).include?(Tod::TimeOfDay.new(9)) # => true
153
+ Tod::Shift.new(Tod::TimeOfDay.new(5), Tod::TimeOfDay.new(9), true).include?(Tod::TimeOfDay.new(9)) # => false
154
+
155
+
156
+ Overlap?
157
+ --------------------
158
+
159
+ breakfast = Tod::Shift.new(Tod::TimeOfDay.new(8), Tod::TimeOfDay.new(11))
160
+ lunch = Tod::Shift.new(Tod::TimeOfDay.new(10), Tod::TimeOfDay.new(14))
161
+ breakfast.overlaps?(lunch) # => true
162
+ lunch.overlaps?(breakfast) # => true
163
+
164
+ dinner = Tod::Shift.new(Tod::TimeOfDay.new(18), Tod::TimeOfDay.new(20))
165
+ dinner.overlaps?(lunch) # => false
166
+
167
+ # Exclude ending
168
+ morning_shift = Tod::Shift.new(Tod::TimeOfDay.new(9), Tod::TimeOfDay.new(17), true)
169
+ evening_shift = Tod::Shift.new(Tod::TimeOfDay.new(17), Tod::TimeOfDay.new(1), true)
170
+ morning_shift.overlaps?(evening_shift) # => false
171
+
172
+
173
+ Contains?
174
+ --------------------
175
+ workday = Shift.new(TimeOfDay.new(9), TimeOfDay.new(17))
176
+ lunch = Shift.new(TimeOfDay.new(10), TimeOfDay.new(14))
177
+ workday.contains?(lunch) # => true
178
+ lunch.contains?(workday) # => false
179
+
180
+ dinner = Shift.new(TimeOfDay.new(18), TimeOfDay.new(20))
181
+ dinner.overlaps?(lunch) # => false
182
+
141
183
 
142
184
  Rails Time Zone Support
143
185
  =======================
144
186
 
145
- If Rails time zone support is loaded, Date#on and TimeOfDay#at will automatically use Time.zone.
187
+ If Rails time zone support is loaded, Date#on and Tod::TimeOfDay#at will automatically use Time.zone.
146
188
 
147
189
  Active Record Serializable Attribute Support
148
190
  =======================
149
- TimeOfDay implements a custom serialization contract for activerecord serialize which allows to store TimeOfDay directly
191
+ Tod::TimeOfDay implements a custom serialization contract for ActiveRecord serialize which allows to store Tod::TimeOfDay directly
150
192
  in a column of the time type.
151
193
  Example:
152
194
  ```ruby
153
195
  class Order < ActiveRecord::Base
154
196
  serialize :time, Tod::TimeOfDay
155
197
  end
156
- order = Order.create(time: TimeOfDay.new(9,30))
198
+ order = Order.create(time: Tod::TimeOfDay.new(9,30))
157
199
  order.time # => 09:30:00
158
200
  ```
159
201
 
202
+ MongoDB Support
203
+ ===============
204
+
205
+ Tod includes optional serialization support for Tod::TimeOfDay to be serialized to MongoDB.
206
+
207
+ ```
208
+ require 'tod/mongoization'
209
+ ```
210
+
211
+ Upgrading from Versions Prior to 2.0.0
212
+ ======================================
213
+
214
+ Tod has a new focus on not polluting the global namespace.
215
+
216
+ Tod no longer puts Tod::TimeOfDay and Tod::Shift in the global namespace by default. You can either fully qualify access to these classes or include Tod in the global namespace.
217
+
218
+ ```
219
+ require 'tod'
220
+ include Tod # TimeOfDay and Shift are now in the global namespace like in versions prior to 2.0.0
221
+ ```
222
+
223
+ Tod no longer automatically extends the Time and Date classes. Require them explicitly.
224
+
225
+ ```
226
+ require 'tod/core_extensions'
227
+ tod = Tod::TimeOfDay.new 8, 30 # => 08:30:00
228
+ Date.today.at tod # => 2010-12-29 08:30:00 -0600
229
+ Time.now.to_time_of_day # => 16:30:43
230
+ DateTime.now.to_time_of_day # => 16:30:43
231
+ ```
232
+
233
+ Tod no longer automatically includes MongoDB serialization methods in Tod::TimeOfDay. Require them explicitly.
234
+
235
+ ```
236
+ require 'tod/mongoization'
237
+ ```
238
+
160
239
  Compatibility
161
240
  =============
162
241
 
@@ -168,4 +247,4 @@ Tod is compatible with Ruby 1.9.3, 2.0.0, 2.1.8, and 2.2.0. It is tested against
168
247
  License
169
248
  =======
170
249
 
171
- Copyright (c) 2010-2013 Jack Christensen, released under the MIT license
250
+ Copyright (c) 2010-2015 Jack Christensen, released under the MIT license
data/lib/tod.rb CHANGED
@@ -1,13 +1,3 @@
1
1
  require 'tod/time_of_day'
2
- require 'tod/date'
3
- require 'tod/time'
4
2
  require 'tod/shift'
5
3
  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
@@ -1,15 +1,13 @@
1
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
2
+ def TimeOfDay(obj_or_string)
3
+ if obj_or_string.is_a?(TimeOfDay)
4
+ obj_or_string
5
+ elsif obj_or_string.respond_to?(:to_time_of_day)
6
+ obj_or_string.to_time_of_day
7
+ else
8
+ TimeOfDay.parse(obj_or_string)
13
9
  end
14
10
  end
11
+
12
+ module_function :TimeOfDay
15
13
  end
@@ -0,0 +1,2 @@
1
+ require 'tod/date_extensions'
2
+ require 'tod/time_extensions'
@@ -0,0 +1,15 @@
1
+ module Tod
2
+ module DateExtensions
3
+ # Returns a local Time instance with this date and time_of_day
4
+ # Pass in time_zone to use ActiveSupport::TimeZone
5
+ def at(time_of_day, time_zone=Tod::TimeOfDay.time_zone)
6
+ time_zone.local year, month, day, time_of_day.hour, time_of_day.minute, time_of_day.second
7
+ end
8
+
9
+ def to_time_of_day
10
+ Tod::TimeOfDay.new hour, minute, second
11
+ end
12
+ end
13
+ end
14
+
15
+ Date.send :include, Tod::DateExtensions
@@ -1,3 +1,5 @@
1
+ require 'tod/time_of_day'
2
+
1
3
  module Tod
2
4
  module Mongoization
3
5
 
@@ -33,4 +35,11 @@ module Tod
33
35
  end
34
36
  end
35
37
  end
36
- end
38
+ end
39
+
40
+ module Tod
41
+ class TimeOfDay
42
+ include(Tod::Mongoization)
43
+ extend(Tod::Mongoization::ClassMethods)
44
+ end
45
+ end