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.
- checksums.yaml +15 -0
- data/CHANGELOG.md +9 -1
- data/README.md +317 -208
- data/UPGRADING +5 -3
- data/lib/by_star/base.rb +18 -3
- data/lib/by_star/between.rb +52 -12
- data/lib/by_star/kernel.rb +3 -3
- data/lib/by_star/orm/active_record/by_star.rb +17 -10
- data/lib/by_star/orm/mongoid/by_star.rb +13 -9
- data/lib/by_star/version.rb +1 -1
- data/spec/fixtures/active_record/models.rb +4 -0
- data/spec/fixtures/active_record/schema.rb +8 -0
- data/spec/fixtures/mongoid/models.rb +14 -0
- data/spec/fixtures/shared/seeds.rb +13 -3
- data/spec/integration/active_record/active_record_spec.rb +3 -0
- data/spec/integration/mongoid/mongoid_spec.rb +3 -0
- data/spec/integration/shared/by_calendar_month.rb +5 -5
- data/spec/integration/shared/by_day.rb +29 -5
- data/spec/integration/shared/by_direction.rb +61 -2
- data/spec/integration/shared/by_fortnight.rb +3 -3
- data/spec/integration/shared/by_month.rb +4 -4
- data/spec/integration/shared/by_quarter.rb +4 -4
- data/spec/integration/shared/by_week.rb +3 -3
- data/spec/integration/shared/by_weekend.rb +1 -1
- data/spec/integration/shared/by_year.rb +6 -6
- data/spec/integration/shared/offset_parameter.rb +5 -5
- data/spec/integration/shared/relative.rb +174 -0
- data/spec/integration/shared/scope_parameter.rb +42 -0
- data/spec/unit/kernel_time_spec.rb +20 -20
- metadata +20 -37
checksums.yaml
ADDED
@@ -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=
|
data/CHANGELOG.md
CHANGED
@@ -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
|
-
#
|
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_*
|
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.
|
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
|
-
##
|
40
|
+
## Finder Methods
|
30
41
|
|
31
|
-
|
42
|
+
### Base Scopes
|
32
43
|
|
33
|
-
|
34
|
-
|
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
|
-
|
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
|
-
|
51
|
+
### Time Range Scopes
|
52
52
|
|
53
|
-
|
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
|
-
|
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
|
-
|
65
|
+
### Relative Scopes
|
58
66
|
|
59
|
-
|
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
|
-
|
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
|
-
|
85
|
+
### Instance Methods
|
64
86
|
|
65
|
-
|
87
|
+
In addition, ByStar adds instance methods to return the next / previous record in the timewise sequence:
|
66
88
|
|
67
|
-
|
89
|
+
* `object.next`
|
90
|
+
* `object.previous`
|
68
91
|
|
69
|
-
|
92
|
+
### Kernel Extensions
|
70
93
|
|
71
|
-
|
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
|
-
|
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
|
-
|
104
|
+
## Usage
|
76
105
|
|
77
|
-
|
106
|
+
### Setting the Query Field
|
78
107
|
|
79
|
-
By default, ByStar
|
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
|
-
|
89
|
-
|
90
|
-
|
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
|
-
|
107
|
-
Post.by_year(1999)
|
115
|
+
Alternatively, you may set a default in your model using the `by_star_field` macro:
|
108
116
|
|
109
|
-
|
117
|
+
```ruby
|
118
|
+
class Post < ActiveRecord::Base
|
119
|
+
by_star_field :updated_at
|
120
|
+
end
|
121
|
+
```
|
110
122
|
|
111
|
-
|
123
|
+
### Scoping the Find
|
112
124
|
|
113
|
-
|
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
|
-
|
127
|
+
```ruby
|
128
|
+
Post.by_month.your_scope
|
129
|
+
Post.by_month(1).include(:tags).where("tags.name" => "ruby")
|
130
|
+
```
|
116
131
|
|
117
|
-
|
132
|
+
Want to count records? Simple:
|
118
133
|
|
119
|
-
|
134
|
+
```ruby
|
135
|
+
Post.by_month.count
|
136
|
+
```
|
120
137
|
|
121
|
-
|
138
|
+
### :scope Option
|
122
139
|
|
123
|
-
|
140
|
+
You may pass a `:scope` option as a Relation or Proc to all ByStar methods like so:
|
124
141
|
|
125
|
-
|
142
|
+
```ruby
|
143
|
+
@post.next(scope: Post.where(category: @post.category))
|
144
|
+
@post.next(scope: ->{ where(category: 'blog') })
|
145
|
+
```
|
126
146
|
|
127
|
-
|
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
|
-
|
150
|
+
You may also set a default scope in the `by_star_field` macro. (It is recommended this be a Proc):
|
130
151
|
|
131
|
-
|
152
|
+
```ruby
|
153
|
+
class Post < ActiveRecord::Base
|
154
|
+
by_star_field scope: ->{ where(category: 'blog') }
|
155
|
+
end
|
156
|
+
```
|
132
157
|
|
133
|
-
|
158
|
+
### :offset Option
|
134
159
|
|
135
|
-
|
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
|
-
|
163
|
+
Post.by_day('2014-03-05', offset: 9.hours)
|
138
164
|
|
139
|
-
|
165
|
+
You may also set a offset scope in the `by_star_field` macro:
|
140
166
|
|
141
|
-
|
167
|
+
```ruby
|
168
|
+
class Post < ActiveRecord::Base
|
169
|
+
by_star_field offset: offset: 9.hours
|
170
|
+
end
|
171
|
+
```
|
142
172
|
|
143
|
-
|
173
|
+
### Time-Range Type Objects
|
144
174
|
|
145
|
-
|
175
|
+
If your object has both a start and end time, you may pass both params to `by_star_field`:
|
146
176
|
|
147
|
-
|
177
|
+
```ruby
|
178
|
+
by_star_field :start_time, :end_time
|
179
|
+
```
|
148
180
|
|
149
|
-
|
181
|
+
By default, ByStar queries will return all objects whose range has any overlap within the desired period (permissive):
|
150
182
|
|
151
|
-
|
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
|
-
|
188
|
+
If you'd like to confine results to starting and ending within the given range, use the `:strict` option:
|
154
189
|
|
155
|
-
|
190
|
+
```ruby
|
191
|
+
MultiDayEvent.by_month("January", strict => true) #=> returns MultiDayEvents that both start AND end in January
|
192
|
+
```
|
156
193
|
|
157
|
-
|
194
|
+
### Chronic Support
|
158
195
|
|
159
|
-
|
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
|
-
|
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
|
-
|
202
|
+
## Advanced Usage
|
166
203
|
|
167
|
-
|
204
|
+
### between_times
|
168
205
|
|
169
|
-
|
206
|
+
To find records between two times:
|
170
207
|
|
171
|
-
|
208
|
+
```ruby
|
209
|
+
Post.between_times(time1, time2)
|
210
|
+
```
|
172
211
|
|
173
|
-
|
212
|
+
Also works with dates:
|
174
213
|
|
175
|
-
|
214
|
+
```ruby
|
215
|
+
Post.between_times(date1, date2)
|
216
|
+
```
|
176
217
|
|
177
|
-
|
218
|
+
ActiveRecord only: `between` is an alias for `between_times`:
|
178
219
|
|
179
|
-
|
220
|
+
```ruby
|
221
|
+
Post.between(time1, time2) #=> results identical to between_times above
|
222
|
+
```
|
180
223
|
|
181
|
-
|
224
|
+
### before and after
|
182
225
|
|
183
|
-
|
226
|
+
To find all posts before / after the current time:
|
184
227
|
|
185
|
-
|
228
|
+
```ruby
|
229
|
+
Post.before
|
230
|
+
Post.after
|
231
|
+
```
|
186
232
|
|
187
|
-
|
233
|
+
To find all posts before certain time or date:
|
188
234
|
|
189
|
-
|
235
|
+
```ruby
|
236
|
+
Post.before(Date.today + 2)
|
237
|
+
Post.after(Time.now + 5.days)
|
238
|
+
```
|
190
239
|
|
191
|
-
You
|
240
|
+
You can also pass a string:
|
192
241
|
|
193
|
-
|
242
|
+
```ruby
|
243
|
+
Post.before("next tuesday")
|
244
|
+
```
|
194
245
|
|
195
|
-
|
246
|
+
For Time-Range type objects, only the start time is considered for `before` and `after`.
|
196
247
|
|
197
|
-
|
248
|
+
### previous and next
|
198
249
|
|
199
|
-
|
250
|
+
To find the prior/subsequent record to a model instance, `previous`/`next` on it:
|
200
251
|
|
201
|
-
|
252
|
+
```ruby
|
253
|
+
Post.last.previous
|
254
|
+
Post.first.next
|
255
|
+
```
|
202
256
|
|
203
|
-
|
204
|
-
Post.today
|
257
|
+
You can specify a field also:
|
205
258
|
|
206
|
-
|
259
|
+
```ruby
|
260
|
+
Post.last.previous(field: "published_at")
|
261
|
+
Post.first.next(field: "published_at")
|
262
|
+
```
|
207
263
|
|
208
|
-
|
264
|
+
For Time-Range type objects, only the start time is considered for `previous` and `next`.
|
209
265
|
|
210
|
-
|
266
|
+
### by_year
|
211
267
|
|
212
|
-
|
268
|
+
To find records from the current year, simply call the method without any arguments:
|
213
269
|
|
214
|
-
|
270
|
+
```ruby
|
271
|
+
Post.by_year
|
272
|
+
```
|
215
273
|
|
216
|
-
|
274
|
+
To find records based on a year you can pass it a two or four digit number:
|
217
275
|
|
218
|
-
|
276
|
+
```ruby
|
277
|
+
Post.by_year(09)
|
278
|
+
```
|
219
279
|
|
220
|
-
|
280
|
+
This will return all posts in 2009, whereas:
|
221
281
|
|
222
|
-
|
282
|
+
```ruby
|
283
|
+
Post.by_year(99)
|
284
|
+
```
|
223
285
|
|
224
|
-
|
286
|
+
will return all the posts in the year 1999.
|
225
287
|
|
226
|
-
|
288
|
+
You can also specify the full year:
|
227
289
|
|
228
|
-
|
290
|
+
```ruby
|
291
|
+
Post.by_year(2009)
|
292
|
+
Post.by_year(1999)
|
293
|
+
```
|
229
294
|
|
230
|
-
|
295
|
+
### by_month
|
231
296
|
|
232
|
-
|
297
|
+
If you know the number of the month you want:
|
233
298
|
|
234
|
-
|
299
|
+
```ruby
|
300
|
+
Post.by_month(1)
|
301
|
+
```
|
235
302
|
|
236
|
-
This will return all posts
|
303
|
+
This will return all posts in the first month (January) of the current year.
|
237
304
|
|
238
|
-
|
305
|
+
If you like being verbose:
|
239
306
|
|
240
|
-
|
307
|
+
```ruby
|
308
|
+
Post.by_month("January")
|
309
|
+
```
|
241
310
|
|
242
|
-
|
311
|
+
This will return all posts created in January of the current year.
|
243
312
|
|
244
|
-
|
313
|
+
If you want to find all posts in January of last year just do
|
245
314
|
|
246
|
-
|
315
|
+
```ruby
|
316
|
+
Post.by_month(1, :year => 2007)
|
317
|
+
```
|
247
318
|
|
248
|
-
|
249
|
-
Post.tomorrow(Time.now + 5.days)
|
319
|
+
or
|
250
320
|
|
251
|
-
|
321
|
+
```ruby
|
322
|
+
Post.by_month("January", :year => 2007)
|
252
323
|
|
253
|
-
|
324
|
+
This will perform a find using the column you've specified.
|
254
325
|
|
255
|
-
|
326
|
+
If you have a Time object you can use it to find the posts:
|
256
327
|
|
257
|
-
|
328
|
+
```ruby
|
329
|
+
Post.by_month(Time.local(2012, 11, 24))
|
330
|
+
```
|
258
331
|
|
259
|
-
|
332
|
+
This will find all the posts in November 2012.
|
260
333
|
|
261
|
-
|
334
|
+
### by_calendar_month
|
262
335
|
|
263
|
-
|
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
|
-
|
266
|
-
|
338
|
+
```ruby
|
339
|
+
Post.by_calendar_month
|
340
|
+
```
|
267
341
|
|
268
|
-
|
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
|
-
|
344
|
+
### by_fortnight
|
271
345
|
|
272
|
-
|
346
|
+
Fortnight numbering starts at 0. The beginning of a fortnight is Monday, 12am.
|
273
347
|
|
274
|
-
To find
|
348
|
+
To find records from the current fortnight:
|
275
349
|
|
276
|
-
|
350
|
+
```ruby
|
351
|
+
Post.by_fortnight
|
352
|
+
```
|
277
353
|
|
278
|
-
To find
|
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
|
-
|
281
|
-
|
356
|
+
```ruby
|
357
|
+
Post.by_fortnight(18)
|
358
|
+
```
|
282
359
|
|
283
|
-
|
360
|
+
This will return all posts in the 18th fortnight of the current year.
|
284
361
|
|
285
|
-
|
362
|
+
```ruby
|
363
|
+
Post.by_fortnight(18, :year => 2012)
|
364
|
+
```
|
286
365
|
|
287
|
-
|
366
|
+
This will return all posts in the 18th fortnight week of 2012.
|
288
367
|
|
289
|
-
|
368
|
+
```ruby
|
369
|
+
Post.by_fortnight(Time.local(2012,1,1))
|
370
|
+
```
|
290
371
|
|
291
|
-
|
372
|
+
This will return all posts from the first fortnight of 2012.
|
292
373
|
|
293
|
-
|
374
|
+
### by_week
|
294
375
|
|
295
|
-
|
376
|
+
Week numbering starts at 0. The beginning of a week is Monday, 12am.
|
296
377
|
|
297
|
-
|
298
|
-
Post.after(Time.now + 5.days)
|
378
|
+
To find records from the current week:
|
299
379
|
|
300
|
-
|
380
|
+
```ruby
|
381
|
+
Post.by_week
|
382
|
+
```
|
301
383
|
|
302
|
-
|
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
|
-
|
386
|
+
```ruby
|
387
|
+
Post.by_week(36)
|
388
|
+
```
|
305
389
|
|
306
|
-
|
390
|
+
This will return all posts in the 37th week of the current year (remember week numbering starts at 0).
|
307
391
|
|
308
|
-
|
392
|
+
```ruby
|
393
|
+
Post.by_week(36, :year => 2012)
|
394
|
+
```
|
309
395
|
|
310
|
-
|
396
|
+
This will return all posts in the 37th week of 2012.
|
311
397
|
|
312
|
-
|
398
|
+
```ruby
|
399
|
+
Post.by_week(Time.local(2012,1,1))
|
400
|
+
```
|
313
401
|
|
314
|
-
|
402
|
+
This will return all posts from the first week of 2012.
|
315
403
|
|
316
|
-
|
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
|
-
|
406
|
+
### by_weekend
|
319
407
|
|
320
|
-
|
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
|
-
|
410
|
+
```ruby
|
411
|
+
Post.by_weekend(Time.now)
|
412
|
+
```
|
323
413
|
|
324
|
-
|
414
|
+
### by_day and today
|
325
415
|
|
326
|
-
|
416
|
+
To find records for today:
|
327
417
|
|
328
|
-
|
418
|
+
```ruby
|
419
|
+
Post.by_day
|
420
|
+
Post.today
|
421
|
+
```
|
329
422
|
|
330
|
-
|
423
|
+
To find records for a certain day:
|
331
424
|
|
332
|
-
|
425
|
+
```ruby
|
426
|
+
Post.by_day(Time.local(2012, 1, 1))
|
427
|
+
```
|
333
428
|
|
334
|
-
|
429
|
+
You can also pass a string:
|
335
430
|
|
336
|
-
|
431
|
+
```ruby
|
432
|
+
Post.by_day("next tuesday")
|
433
|
+
```
|
337
434
|
|
338
|
-
|
435
|
+
This will return all posts for the given day.
|
339
436
|
|
340
|
-
|
437
|
+
### by_quarter
|
341
438
|
|
342
|
-
|
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
|
-
|
441
|
+
To find records from the current quarter:
|
345
442
|
|
346
|
-
|
347
|
-
|
443
|
+
```ruby
|
444
|
+
Post.by_quarter
|
445
|
+
```
|
348
446
|
|
349
|
-
|
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
|
-
|
449
|
+
```ruby
|
450
|
+
Post.by_quarter(4)
|
451
|
+
```
|
352
452
|
|
353
|
-
|
453
|
+
This will return all posts in the 4th quarter of the current year.
|
354
454
|
|
355
|
-
|
455
|
+
```ruby
|
456
|
+
Post.by_quarter(2, :year => 2012)
|
457
|
+
```
|
356
458
|
|
357
|
-
|
459
|
+
This will return all posts in the 2nd quarter of 2012.
|
358
460
|
|
359
|
-
|
461
|
+
```ruby
|
462
|
+
Post.by_week(Time.local(2012,1,1))
|
463
|
+
```
|
360
464
|
|
361
|
-
|
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
|
-
|
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
|
-
|
470
|
+
ByStar is tested against the following versions:
|
371
471
|
|
372
|
-
|
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
|
-
|
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
|
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
|