by_star 2.2.0.rc1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTIzYTk4N2IwY2ExNjIwNGNhNGRmODNiNjA3NGQ5YmQyYWI1ODkyZQ==
5
+ data.tar.gz: !binary |-
6
+ Zjg5MDgwZDMzMTcyOThlZGE4N2M4OTg5MmMzNDMxNzE0MGUyYjhlMg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MmU1OTgwYWY3YTBmMjQwMzgyYmI0YWNkMTkxM2I5NGMxZTQzYTI2MmEyYTIw
10
+ OTZjMmJiZmNkZjk4YTUyMmNkOTcxN2IyYjZjZjc4ZGFkMjE5YmNmNjUwNzhj
11
+ MzEyYTdlZTcyN2QwMjE1YzFjODIyYTA4ZTFkOTQ2ODVjMjJkZTI=
12
+ data.tar.gz: !binary |-
13
+ YzVhM2U3YzRlZTE5ZWY4ZTdjZDc2MDc3MDI4YTVlMWU4OWY4MjNiM2Q3Y2Fk
14
+ N2JmNTdmN2M2YmNjYjY1MjJkZGUxMGVmNTdlNzA0OTBiNjJlZWRjN2RhOTcy
15
+ YWVhMzIwZmUxMjdjNWEwNmI2NjdiYWNiNGE4NjZhMjdjMmE1NDM=
@@ -1,6 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
- ## v2.2.0
3
+ ## v2.2.0 - 2014-04-01
4
+
5
+ * Add `:scope` parameter support on all finders - @pnomolos / @johnnyshields
6
+ * Feature: Add `past_*` and `next_*` finders - @davegudge
7
+ * Bug Fix: `:field`, `:start_field`, and `:end_field` options were being ignored on finder - @johnnyshields / @gamov
8
+ * Bug Fix: `by_star_field` should accept options without start/end_time args - @johnnyshields
9
+ * Improve readme documentation - @johnnyshields
10
+
11
+ ## v2.2.0.rc1 - 2014-01-14
4
12
 
5
13
  * Begin tracking CHANGELOG
6
14
  * Drop official support for Ruby <= 1.9.2
data/README.md CHANGED
@@ -1,10 +1,20 @@
1
- # by_*
1
+ # ByStar
2
2
 
3
3
  [![Build Status](https://travis-ci.org/radar/by_star.png)](https://travis-ci.org/radar/by_star)
4
4
  [![Code Climate](https://codeclimate.com/github/radar/by_star.png)](https://codeclimate.com/github/radar/by_star)
5
5
 
6
- by_* (by_star) is a plugin that allows you to find ActiveRecord and/or Mongoid objects given certain date objects.
6
+ ByStar (by_*) allows you easily and reliably query ActiveRecord and Mongoid objects based on time.
7
7
 
8
+ ### Examples
9
+
10
+ ```ruby
11
+ Post.by_year(2013) # all posts in 2013
12
+ Post.before(Date.today) # all posts for before today
13
+ Post.yesterday # all posts in 2013
14
+ Post.between_times(Time.zone.now - 3.hours, # all posts in last 3 hours
15
+ Time.zone.now)
16
+ @post.next # next post after a given post
17
+ ```
8
18
 
9
19
  ## Installation
10
20
 
@@ -14,11 +24,12 @@ Install this gem by adding this to your Gemfile:
14
24
  gem 'by_star', :git => "git://github.com/radar/by_star"
15
25
  ```
16
26
 
17
- Then run `bundle install`.
27
+ Then run `bundle install`
18
28
 
19
29
  If you are using ActiveRecord, you're done!
20
30
 
21
- Mongoid users, please include the Mongoid::ByStar module for each model you wish to use the functionality. This is the convention among Mongoid plugins.
31
+ Mongoid users, please include the Mongoid::ByStar module for each model you wish to use the functionality.
32
+ This is the convention among Mongoid plugins.
22
33
 
23
34
  ```ruby
24
35
  class MyModel
@@ -26,355 +37,447 @@ class MyModel
26
37
  include Mongoid::ByStar
27
38
  ```
28
39
 
29
- ## What it does
40
+ ## Finder Methods
30
41
 
31
- This was originally crafted for only finding objects within a given month, but now has extended out to much more. It now supports finding objects for:
42
+ ### Base Scopes
32
43
 
33
- * A given year
34
- * A given month
35
- * A given fortnight
36
- * A given week
37
- * A given weekend
38
- * A given day
39
- * A given quarter
40
- * The weeks of a given month as shown on a calendar
41
- * The current weekend
42
- * The current work week
43
- * The Past
44
- * The Future
45
- * Between certain times
46
- * Before a specific record
47
- * After a specific record
44
+ ByStar adds the following finder scopes (class methods) to your model to query time ranges.
45
+ These accept a `Date`, `Time`, or `DateTime` object as an argument, which defaults to `Time.zone.now` if not specified:
48
46
 
49
- All methods return scopes. I love these. You love these. Everybody loves these.
47
+ * `between_times(start_time, end_time)` Finds all records occurring between the two given times
48
+ * `before(end_time)` Finds all records occurring before the given time
49
+ * `after(start_time)` Finds all records occurring after the given time
50
50
 
51
- It also allows you to do nested finds on the records returned which I personally think is the coolest feature of the whole plugin:
51
+ ### Time Range Scopes
52
52
 
53
- Post.by_month(1).include(:tags).where("tags.name" => "ruby")
53
+ ByStar adds additional shortcut scopes based on commonly used time ranges.
54
+ See sections below for detailed argument usage of each:
54
55
 
55
- If you're not using the standard `created_at` field: don't worry! I've covered that scenario too.
56
+ * `by_day`
57
+ * `by_week`
58
+ * `by_weekend` 60-hour period from 15:00 Friday to 03:00 Monday
59
+ * `by_fortnight` A two-week period, with the first fortnight of the year beginning on 1st January
60
+ * `by_month`
61
+ * `by_calendar_month` Month as it appears on a calendar; days form previous/following months which are part of the first/last weeks of the given month
62
+ * `by_quarter` 3-month intervals of the year
63
+ * `by_year`
56
64
 
57
- ## Scoping the find
65
+ ### Relative Scopes
58
66
 
59
- You can treat all `by_*` methods exactly how you would treat normal, every-day ActiveRecord scopes. This is because all `by_*` methods return `ActiveRecord::Relation` objects, with the exception of `previous` and `next`, which return a single record. You can call them like this:
67
+ ByStar also adds scopes which are relative to the current time.
68
+ Note the `past_*` and `next_*` methods represent a time distance from current time (`Time.zone.now`),
69
+ and do not strictly end/begin evenly on a calendar week/month/year (unlike `by_*` methods which do.)
60
70
 
61
- Post.by_month.your_scope
71
+ * `today` Finds all occurrences on today's date
72
+ * `yesterday` Finds all occurrences on yesterday's date
73
+ * `tomorrow` Finds all occurrences on tomorrow's date
74
+ * `past_day` Prior 24-hour period from current time
75
+ * `past_week` Prior 7-day period from current time
76
+ * `past_fortnight` Prior 14-day period from current time
77
+ * `past_month` Prior 30-day period from current time
78
+ * `past_year` Prior 365-day period from current time
79
+ * `next_day` Subsequent 24-hour period from current time
80
+ * `next_week` Subsequent 7-day period from current time
81
+ * `next_fortnight` Subsequent 14-day period from current time
82
+ * `next_month` Subsequent 30-day period from current time
83
+ * `next_year` Subsequent 365-day period from current time
62
84
 
63
- Where `my_special_scope` is a `named_scope` you have specified.
85
+ ### Instance Methods
64
86
 
65
- You can also call typical `ActiveRecord::Relation` methods on the `by_*` methods (like I showed before):
87
+ In addition, ByStar adds instance methods to return the next / previous record in the timewise sequence:
66
88
 
67
- Post.by_month.include(:tags).where("tags.name" => "ruby")
89
+ * `object.next`
90
+ * `object.previous`
68
91
 
69
- Want to count records? Simple:
92
+ ### Kernel Extensions
70
93
 
71
- Post.by_month.count
94
+ Lastly, ByStar extends the kernel `Date`, `Time`, and `DateTime` objects with the following instance methods,
95
+ which mirror the ActiveSupport methods `beginning_of_day`, `end_of_week`, etc:
72
96
 
73
- ## Time-Range Type Objects
97
+ * `beginning_of_weekend`
98
+ * `end_of_weekend`
99
+ * `beginning_of_fortnight`
100
+ * `end_of_fortnight`
101
+ * `beginning_of_calendar_month`
102
+ * `end_of_calendar_month`
74
103
 
75
- If your object has both a start and end time, you may pass both params to `by_star_field`:
104
+ ## Usage
76
105
 
77
- by_star_field :start_time, :end_time
106
+ ### Setting the Query Field
78
107
 
79
- By default, ByStar queries will return all objects whose range has any overlap within the desired period (permissive):
80
-
81
- MultiDayEvent.by_month("January") #=> returns MultiDayEvents that overlap in January,
82
- even if they start in December and/or end in February
83
-
84
- If you'd like to confine results to starting and ending within the given range, use the `:strict` option:
85
-
86
- MultiDayEvent.by_month("January", strict => true) #=> returns MultiDayEvents that both start AND end in January
108
+ By default, ByStar assumes you will use the `created_at` field to query objects by time.
109
+ You may specify an alternate field on all query methods as follows:
87
110
 
88
- ## By Year (`by_year`)
89
-
90
- To find records from the current year, simply call the method without any arguments:
91
-
92
- Post.by_year
93
-
94
- To find records based on a year you can pass it a two or four digit number:
95
-
96
- Post.by_year(09)
97
-
98
- This will return all posts in 2009, whereas:
99
-
100
- Post.by_year(99)
101
-
102
- will return all the posts in the year 1999.
103
-
104
- You can also specify the full year:
111
+ ```ruby
112
+ Post.by_month("January", field: :updated_at)
113
+ ```
105
114
 
106
- Post.by_year(2009)
107
- Post.by_year(1999)
115
+ Alternatively, you may set a default in your model using the `by_star_field` macro:
108
116
 
109
- ## By Month (`by_month`)
117
+ ```ruby
118
+ class Post < ActiveRecord::Base
119
+ by_star_field :updated_at
120
+ end
121
+ ```
110
122
 
111
- If you know the number of the month you want:
123
+ ### Scoping the Find
112
124
 
113
- Post.by_month(1)
125
+ All ByStar methods (except `previous` and `next`) return `ActiveRecord::Relation` and/or `Mongoid::Criteria` objects, which can be daisy-chained with other scopes/finder methods:
114
126
 
115
- This will return all posts in the first month (January) of the current year.
127
+ ```ruby
128
+ Post.by_month.your_scope
129
+ Post.by_month(1).include(:tags).where("tags.name" => "ruby")
130
+ ```
116
131
 
117
- If you like being verbose:
132
+ Want to count records? Simple:
118
133
 
119
- Post.by_month("January")
134
+ ```ruby
135
+ Post.by_month.count
136
+ ```
120
137
 
121
- This will return all posts created in January of the current year.
138
+ ### :scope Option
122
139
 
123
- If you want to find all posts in January of last year just do
140
+ You may pass a `:scope` option as a Relation or Proc to all ByStar methods like so:
124
141
 
125
- Post.by_month(1, :year => 2007)
142
+ ```ruby
143
+ @post.next(scope: Post.where(category: @post.category))
144
+ @post.next(scope: ->{ where(category: 'blog') })
145
+ ```
126
146
 
127
- or
147
+ This is particularly useful for `previous` and `next`, which return a model instance rather than
148
+ a Relation and hence cannot be daisy-chained with other scopes.
128
149
 
129
- Post.by_month("January", :year => 2007)
150
+ You may also set a default scope in the `by_star_field` macro. (It is recommended this be a Proc):
130
151
 
131
- This will perform a find using the column you've specified.
152
+ ```ruby
153
+ class Post < ActiveRecord::Base
154
+ by_star_field scope: ->{ where(category: 'blog') }
155
+ end
156
+ ```
132
157
 
133
- If you have a Time object you can use it to find the posts:
158
+ ### :offset Option
134
159
 
135
- Post.by_month(Time.local(2012, 11, 24))
160
+ All ByStar finders support an `:offset` option which offsets the time period for which the query is performed.
161
+ This is useful in cases where the daily cycle occurs at a time other than midnight.
136
162
 
137
- This will find all the posts in November 2012.
163
+ Post.by_day('2014-03-05', offset: 9.hours)
138
164
 
139
- ## By Calendar Month (`by_calendar_month`)
165
+ You may also set a offset scope in the `by_star_field` macro:
140
166
 
141
- Finds records for a given month as shown on a calendar. Includes all the results of `by_month`, plus any results which fall in the same week as the first and last of the month. Useful for working with UI calendars which show rows of weeks.
167
+ ```ruby
168
+ class Post < ActiveRecord::Base
169
+ by_star_field offset: offset: 9.hours
170
+ end
171
+ ```
142
172
 
143
- Post.by_calendar_month
173
+ ### Time-Range Type Objects
144
174
 
145
- Parameter behavior is otherwise the same as `by_month`. Also, `:start_day` option is supported to specify the start day of the week (`:monday`, `:tuesday`, etc.)
175
+ If your object has both a start and end time, you may pass both params to `by_star_field`:
146
176
 
147
- ## By Fortnight (`by_fortnight`)
177
+ ```ruby
178
+ by_star_field :start_time, :end_time
179
+ ```
148
180
 
149
- Fortnight numbering starts at 0. The beginning of a fortnight is Monday, 12am.
181
+ By default, ByStar queries will return all objects whose range has any overlap within the desired period (permissive):
150
182
 
151
- To find records from the current fortnight:
183
+ ```ruby
184
+ MultiDayEvent.by_month("January") #=> returns MultiDayEvents that overlap in January,
185
+ even if they start in December and/or end in February
186
+ ```
152
187
 
153
- Post.by_fortnight
188
+ If you'd like to confine results to starting and ending within the given range, use the `:strict` option:
154
189
 
155
- To find records based on a fortnight, you can pass in a number (representing the fortnight number) or a time object:
190
+ ```ruby
191
+ MultiDayEvent.by_month("January", strict => true) #=> returns MultiDayEvents that both start AND end in January
192
+ ```
156
193
 
157
- Post.by_fortnight(18)
194
+ ### Chronic Support
158
195
 
159
- This will return all posts in the 18th fortnight of the current year.
196
+ If [Chronic](https://github.com/mojombo/chronic) gem is present, it will be used to parse natural-language date/time
197
+ strings in all ByStar finder methods. Otherwise, the Ruby `Time.parse` kernel method will be used as a fallback.
160
198
 
161
- Post.by_fortnight(18, :year => 2012)
199
+ As of ByStar 2.2.0, you must explicitly include `gem chronic` into your Gemfile in order to use Chronic.
162
200
 
163
- This will return all posts in the 18th fortnight week of 2012.
164
201
 
165
- Post.by_fortnight(Time.local(2012,1,1))
202
+ ## Advanced Usage
166
203
 
167
- This will return all posts from the first fortnight of 2012.
204
+ ### between_times
168
205
 
169
- ## By Week (`by_week`)
206
+ To find records between two times:
170
207
 
171
- Week numbering starts at 0. The beginning of a week is Monday, 12am.
208
+ ```ruby
209
+ Post.between_times(time1, time2)
210
+ ```
172
211
 
173
- To find records from the current week:
212
+ Also works with dates:
174
213
 
175
- Post.by_week
214
+ ```ruby
215
+ Post.between_times(date1, date2)
216
+ ```
176
217
 
177
- To find records based on a week, you can pass in a number (representing the week number) or a time object:
218
+ ActiveRecord only: `between` is an alias for `between_times`:
178
219
 
179
- Post.by_week(36)
220
+ ```ruby
221
+ Post.between(time1, time2) #=> results identical to between_times above
222
+ ```
180
223
 
181
- This will return all posts in the 37th week of the current year (remember week numbering starts at 0).
224
+ ### before and after
182
225
 
183
- Post.by_week(36, :year => 2012)
226
+ To find all posts before / after the current time:
184
227
 
185
- This will return all posts in the 37th week of 2012.
228
+ ```ruby
229
+ Post.before
230
+ Post.after
231
+ ```
186
232
 
187
- Post.by_week(Time.local(2012,1,1))
233
+ To find all posts before certain time or date:
188
234
 
189
- This will return all posts from the first week of 2012.
235
+ ```ruby
236
+ Post.before(Date.today + 2)
237
+ Post.after(Time.now + 5.days)
238
+ ```
190
239
 
191
- You may pass in a `:start_day` option (`:monday`, `:tuesday`, etc.) to specify the starting day of the week. This may also be configured in Rails.
240
+ You can also pass a string:
192
241
 
193
- ## By Weekend (`by_weekend`)
242
+ ```ruby
243
+ Post.before("next tuesday")
244
+ ```
194
245
 
195
- If the time passed in (or the time now is a weekend) it will return posts from 12am Saturday to 11:59:59PM Sunday. If the time is a week day, it will show all posts for the coming weekend.
246
+ For Time-Range type objects, only the start time is considered for `before` and `after`.
196
247
 
197
- Post.by_weekend(Time.now)
248
+ ### previous and next
198
249
 
199
- ## By Day (`by_day` or `today`)
250
+ To find the prior/subsequent record to a model instance, `previous`/`next` on it:
200
251
 
201
- To find records for today:
252
+ ```ruby
253
+ Post.last.previous
254
+ Post.first.next
255
+ ```
202
256
 
203
- Post.by_day
204
- Post.today
257
+ You can specify a field also:
205
258
 
206
- To find records for a certain day:
259
+ ```ruby
260
+ Post.last.previous(field: "published_at")
261
+ Post.first.next(field: "published_at")
262
+ ```
207
263
 
208
- Post.by_day(Time.local(2012, 1, 1))
264
+ For Time-Range type objects, only the start time is considered for `previous` and `next`.
209
265
 
210
- You can also pass a string:
266
+ ### by_year
211
267
 
212
- Post.by_day("next tuesday")
268
+ To find records from the current year, simply call the method without any arguments:
213
269
 
214
- This will return all posts for the given day.
270
+ ```ruby
271
+ Post.by_year
272
+ ```
215
273
 
216
- ## By Quarter (`by_quarter`)
274
+ To find records based on a year you can pass it a two or four digit number:
217
275
 
218
- Finds records by 3-month quarterly period of year. Quarter numbering starts at 1. The four quarters of the year begin on Jan 1, Apr 1, Jul 1, and Oct 1 respectively.
276
+ ```ruby
277
+ Post.by_year(09)
278
+ ```
219
279
 
220
- To find records from the current quarter:
280
+ This will return all posts in 2009, whereas:
221
281
 
222
- Post.by_quarter
282
+ ```ruby
283
+ Post.by_year(99)
284
+ ```
223
285
 
224
- To find records based on a quarter, you can pass in a number (representing the quarter number) or a time object:
286
+ will return all the posts in the year 1999.
225
287
 
226
- Post.by_quarter(4)
288
+ You can also specify the full year:
227
289
 
228
- This will return all posts in the 4th quarter of the current year.
290
+ ```ruby
291
+ Post.by_year(2009)
292
+ Post.by_year(1999)
293
+ ```
229
294
 
230
- Post.by_quarter(2, :year => 2012)
295
+ ### by_month
231
296
 
232
- This will return all posts in the 2nd quarter of 2012.
297
+ If you know the number of the month you want:
233
298
 
234
- Post.by_week(Time.local(2012,1,1))
299
+ ```ruby
300
+ Post.by_month(1)
301
+ ```
235
302
 
236
- This will return all posts from the first quarter of 2012.
303
+ This will return all posts in the first month (January) of the current year.
237
304
 
238
- ## Tomorrow (`tomorrow`)
305
+ If you like being verbose:
239
306
 
240
- *This method has been shown to be shifty when passed a `Date` object, it is recommended that you pass it an `ActiveSupport::TimeWithZone` object instead.*
307
+ ```ruby
308
+ Post.by_month("January")
309
+ ```
241
310
 
242
- To find all posts from the day after the current date:
311
+ This will return all posts created in January of the current year.
243
312
 
244
- Post.tomorrow
313
+ If you want to find all posts in January of last year just do
245
314
 
246
- To find all posts after a given Date or Time object:
315
+ ```ruby
316
+ Post.by_month(1, :year => 2007)
317
+ ```
247
318
 
248
- Post.tomorrow(Date.today + 2)
249
- Post.tomorrow(Time.now + 5.days)
319
+ or
250
320
 
251
- You can also pass a string:
321
+ ```ruby
322
+ Post.by_month("January", :year => 2007)
252
323
 
253
- Post.tomorrow("next tuesday")
324
+ This will perform a find using the column you've specified.
254
325
 
255
- ## Yesterday (`yesterday`)
326
+ If you have a Time object you can use it to find the posts:
256
327
 
257
- *This method has been shown to be shifty when passed a `Date` object, it is recommended that you pass it an `ActiveSupport::TimeWithZone` object instead.*
328
+ ```ruby
329
+ Post.by_month(Time.local(2012, 11, 24))
330
+ ```
258
331
 
259
- To find all posts from the day before the current date:
332
+ This will find all the posts in November 2012.
260
333
 
261
- Post.yesterday
334
+ ### by_calendar_month
262
335
 
263
- To find all posts before a given Date or Time object:
336
+ Finds records for a given month as shown on a calendar. Includes all the results of `by_month`, plus any results which fall in the same week as the first and last of the month. Useful for working with UI calendars which show rows of weeks.
264
337
 
265
- Post.yesterday(Date.today + 2)
266
- Post.yesterday(Time.now + 5.days)
338
+ ```ruby
339
+ Post.by_calendar_month
340
+ ```
267
341
 
268
- You can also pass a string:
342
+ Parameter behavior is otherwise the same as `by_month`. Also, `:start_day` option is supported to specify the start day of the week (`:monday`, `:tuesday`, etc.)
269
343
 
270
- Post.yesterday("next tuesday")
344
+ ### by_fortnight
271
345
 
272
- ## Before (`before`)
346
+ Fortnight numbering starts at 0. The beginning of a fortnight is Monday, 12am.
273
347
 
274
- To find all posts before the current time:
348
+ To find records from the current fortnight:
275
349
 
276
- Post.before
350
+ ```ruby
351
+ Post.by_fortnight
352
+ ```
277
353
 
278
- To find all posts before certain time or date:
354
+ To find records based on a fortnight, you can pass in a number (representing the fortnight number) or a time object:
279
355
 
280
- Post.before(Date.today + 2)
281
- Post.before(Time.now + 5.days)
356
+ ```ruby
357
+ Post.by_fortnight(18)
358
+ ```
282
359
 
283
- You can also pass a string:
360
+ This will return all posts in the 18th fortnight of the current year.
284
361
 
285
- Post.before("next tuesday")
362
+ ```ruby
363
+ Post.by_fortnight(18, :year => 2012)
364
+ ```
286
365
 
287
- For Time-Range type objects, only the start time is considered for `before`.
366
+ This will return all posts in the 18th fortnight week of 2012.
288
367
 
289
- ## After (`after`)
368
+ ```ruby
369
+ Post.by_fortnight(Time.local(2012,1,1))
370
+ ```
290
371
 
291
- To find all posts after the current time:
372
+ This will return all posts from the first fortnight of 2012.
292
373
 
293
- Post.after
374
+ ### by_week
294
375
 
295
- To find all posts after certain time or date:
376
+ Week numbering starts at 0. The beginning of a week is Monday, 12am.
296
377
 
297
- Post.after(Date.today + 2)
298
- Post.after(Time.now + 5.days)
378
+ To find records from the current week:
299
379
 
300
- You can also pass a string:
380
+ ```ruby
381
+ Post.by_week
382
+ ```
301
383
 
302
- Post.after("next tuesday")
384
+ To find records based on a week, you can pass in a number (representing the week number) or a time object:
303
385
 
304
- For Time-Range type objects, only the start time is considered for `after`.
386
+ ```ruby
387
+ Post.by_week(36)
388
+ ```
305
389
 
306
- ## Between (`between_times`)
390
+ This will return all posts in the 37th week of the current year (remember week numbering starts at 0).
307
391
 
308
- To find records between two times:
392
+ ```ruby
393
+ Post.by_week(36, :year => 2012)
394
+ ```
309
395
 
310
- Post.between_times(time1, time2)
396
+ This will return all posts in the 37th week of 2012.
311
397
 
312
- Also works with dates:
398
+ ```ruby
399
+ Post.by_week(Time.local(2012,1,1))
400
+ ```
313
401
 
314
- Post.between_times(date1, date2)
402
+ This will return all posts from the first week of 2012.
315
403
 
316
- ActiveRecord only: `between` is an alias for `between_times`:
404
+ You may pass in a `:start_day` option (`:monday`, `:tuesday`, etc.) to specify the starting day of the week. This may also be configured in Rails.
317
405
 
318
- Post.between(time1, time2) #=> results identical to between_times above
406
+ ### by_weekend
319
407
 
320
- ## Previous (`previous`)
408
+ If the time passed in (or the time now is a weekend) it will return posts from 12am Saturday to 11:59:59PM Sunday. If the time is a week day, it will show all posts for the coming weekend.
321
409
 
322
- To find the record prior to this one call `previous` on any model instance:
410
+ ```ruby
411
+ Post.by_weekend(Time.now)
412
+ ```
323
413
 
324
- Post.last.previous
414
+ ### by_day and today
325
415
 
326
- You can specify a field also:
416
+ To find records for today:
327
417
 
328
- Post.last.previous("published_at")
418
+ ```ruby
419
+ Post.by_day
420
+ Post.today
421
+ ```
329
422
 
330
- For Time-Range type objects, only the start time is considered for `previous`.
423
+ To find records for a certain day:
331
424
 
332
- ## Next (`next`)
425
+ ```ruby
426
+ Post.by_day(Time.local(2012, 1, 1))
427
+ ```
333
428
 
334
- To find the record after this one call `next` on any model instance:
429
+ You can also pass a string:
335
430
 
336
- Post.last.next
431
+ ```ruby
432
+ Post.by_day("next tuesday")
433
+ ```
337
434
 
338
- You can specify a field also:
435
+ This will return all posts for the given day.
339
436
 
340
- Post.last.next("published_at")
437
+ ### by_quarter
341
438
 
342
- For Time-Range type objects, only the start time is considered for `next`.
439
+ Finds records by 3-month quarterly period of year. Quarter numbering starts at 1. The four quarters of the year begin on Jan 1, Apr 1, Jul 1, and Oct 1 respectively.
343
440
 
344
- ## Offset option
441
+ To find records from the current quarter:
345
442
 
346
- All ByStar finders support an `:offset` option which offsets the time period for which the query is performed.
347
- This is useful in cases where the daily cycle occurs at a time other than midnight.
443
+ ```ruby
444
+ Post.by_quarter
445
+ ```
348
446
 
349
- Post.by_day('2014-03-05', offset: 9.hours)
447
+ To find records based on a quarter, you can pass in a number (representing the quarter number) or a time object:
350
448
 
351
- ## Not using created_at? No worries!
449
+ ```ruby
450
+ Post.by_quarter(4)
451
+ ```
352
452
 
353
- If your database uses something other than `created_at` for storing a timestamp, you can specify the field option like this:
453
+ This will return all posts in the 4th quarter of the current year.
354
454
 
355
- Post.by_month("January", :field => :something_else)
455
+ ```ruby
456
+ Post.by_quarter(2, :year => 2012)
457
+ ```
356
458
 
357
- All methods support this extra option.
459
+ This will return all posts in the 2nd quarter of 2012.
358
460
 
359
- Or if you're doing it all the time on your model, then it's best to use `by_star_field` at the top of your model:
461
+ ```ruby
462
+ Post.by_week(Time.local(2012,1,1))
463
+ ```
360
464
 
361
- class Post < ActiveRecord::Base
362
- by_star_field :something_else
363
- end
465
+ This will return all posts from the first quarter of 2012.
364
466
 
365
- ## Chronic Support
366
467
 
367
- If [Chronic](https://github.com/mojombo/chronic) gem is present, it will be used to parse natural-language date/time
368
- strings in all ByStar finder methods. Otherwise, the Ruby `Time.parse` kernel method will be used as a fallback.
468
+ ## Version Support
369
469
 
370
- As of ByStar 2.2.0, you must explicitly include `gem chronic` into your Gemfile in order to use Chronic.
470
+ ByStar is tested against the following versions:
371
471
 
372
- ## Mongoid
472
+ * Ruby 1.9.3+
473
+ * Rails/ActiveRecord 3.0+
474
+ * Mongoid 3.0+
373
475
 
374
- Mongoid is only tested/supported on version 3.0+
375
476
 
376
477
  ## Testing
377
478
 
479
+ ### Test Setup
480
+
378
481
  Specify a database by supplying a `DB` environmental variable:
379
482
 
380
483
  `bundle exec rake spec DB=sqlite`
@@ -383,7 +486,6 @@ You can also take an ORM-specific test task for a ride:
383
486
 
384
487
  `bundle exec rake spec:active_record`
385
488
 
386
-
387
489
  Have an Active Record or Mongoid version in mind? Set the environment variables
388
490
  `ACTIVE_RECORD_VERSION` and `MONGOID_VERSION` to a version of your choice. A
389
491
  version number provided will translate to `~> VERSION`, and the string `master`
@@ -397,18 +499,25 @@ ACTIVE_RECORD_VERSION=4.0.0 MONGOID_VERSION=master bundle update
397
499
  ACTIVE_RECORD_VERSION=4.0.0 MONGOID_VERSION=master bundle exec rpsec spec
398
500
  ```
399
501
 
502
+ ### Test Implementation
503
+
504
+ ByStar tests use TimeCop to lock the system `Time.now` at Jan 01, 2014, and seed
505
+ objects with fixed dates according to `spec/fixtures/shared/seeds.rb`.
506
+ Note that the timezone is randomized on each run to shake-out timezone related quirks.
507
+
508
+
400
509
  ## Collaborators
401
510
 
402
- Thanks to Thomas Sinclair for the original bump for implementing it. I would like to thank #rubyonrails for their support and the following people:
511
+ ByStar is actively maintained by Ryan Biggs (radar) and Johnny Shields (johnnyshields)
403
512
 
513
+ Thank you to the following people:
514
+
515
+ * Thomas Sinclair for the original bump for implementing ByStar
516
+ * [Ruby on Rails](http://rubyonrails.org/) for their support
404
517
  * Mislav Marohnic
405
518
  * August Lilleas (leethal)
406
519
  * gte351s
407
520
  * Sam Elliott (lenary)
408
- * The people who created [Chronic](https://github.com/mojombo/chronic) gem
521
+ * The creators of the [Chronic](https://github.com/mojombo/chronic) gem
409
522
  * Erik Fonselius
410
523
  * Johnny Shields (johnnyshields)
411
-
412
- ## Suggestions?
413
-
414
- If you have suggestions, please contact me at radarlistener@gmail.com