by_star 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +92 -84
- data/VERSION +1 -1
- data/by_star.gemspec +3 -2
- data/cleaner.rb +25 -0
- data/lib/by_star.rb +3 -3
- data/lib/calculations.rb +1 -1
- data/lib/calculations/count.rb +1 -1
- data/lib/calculations/sum.rb +1 -1
- data/lib/range_calculations.rb +5 -5
- data/lib/shared.rb +1 -1
- data/lib/time_ext.rb +1 -1
- data/lib/vanilla.rb +26 -32
- data/spec/by_star_spec.rb +171 -140
- data/spec/fixtures/models.rb +2 -2
- data/spec/fixtures/schema.rb +7 -7
- metadata +4 -3
data/README.markdown
CHANGED
@@ -17,19 +17,42 @@ by_* (byStar) is a plugin that allows you to find ActiveRecord objects given cer
|
|
17
17
|
|
18
18
|
|
19
19
|
It also allows you to do nested finds on the records returned which I personally think is the coolest feature of the whole plugin:
|
20
|
-
|
20
|
+
|
21
21
|
Post.by_month(1) do
|
22
22
|
{ :include => "tags", :conditions => ["tags.name = ?", 'ruby'] }
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
If you're not using the standard `created_at` field: don't worry! I've covered that scenario too.
|
26
26
|
|
27
|
+
## Scoping the find
|
28
|
+
|
29
|
+
You can treat all `by_*` methods exactly how you would treat `named_scope`s: they are effectively scopes in their own right. This means you are able to call them like this:
|
30
|
+
|
31
|
+
Post.by_month.my_special_scope
|
32
|
+
|
33
|
+
Where `my_special_scope` is a `named_scope` you have specified.
|
34
|
+
|
35
|
+
All the `by_*` methods take a block which will then scope the find based on the options passed into it. You can also specify these options for each method, but the syntax may differ. The supported options are the same options that are supported by `find` from ActiveRecord. Please note that if you want to use conditions you *have* to use this syntax:
|
36
|
+
|
37
|
+
Post.by_month(1) { { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] } }
|
38
|
+
|
39
|
+
or the lengthened:
|
40
|
+
|
41
|
+
Post.by_month(1) do
|
42
|
+
{ :include => "tags", :conditions => ["tags.name = ?", 'ruby'] }
|
43
|
+
end
|
44
|
+
|
45
|
+
An alternative syntax to this is:
|
46
|
+
|
47
|
+
Post.by_month(1, { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] })
|
48
|
+
|
49
|
+
|
27
50
|
## count_by* methods
|
28
51
|
|
29
52
|
`count_by` methods can be scoped to only count those records which have a specific field set, and you do this by specifying the symbol version of the name of the field, e.g;
|
30
53
|
|
31
54
|
Invoice.count_by_year(:value)
|
32
|
-
|
55
|
+
|
33
56
|
If you want to specify further arguments but do not care about the scoped field:
|
34
57
|
|
35
58
|
Invoice.count_by_year(:all, 2009)
|
@@ -38,9 +61,9 @@ If you want to specify further arguments but do not care about the scoped field:
|
|
38
61
|
## By Year (`by_year`)
|
39
62
|
|
40
63
|
To find records based on a year you can pass it a two or four digit number:
|
41
|
-
|
64
|
+
|
42
65
|
Post.by_year(09)
|
43
|
-
|
66
|
+
|
44
67
|
This will return all posts in 2009, whereas:
|
45
68
|
|
46
69
|
Post.by_year(99)
|
@@ -48,46 +71,46 @@ This will return all posts in 2009, whereas:
|
|
48
71
|
will return all the posts in the year 1999.
|
49
72
|
|
50
73
|
You can also specify the full year:
|
51
|
-
|
74
|
+
|
52
75
|
Post.by_year(2009)
|
53
76
|
Post.by_year(1999)
|
54
|
-
|
77
|
+
|
55
78
|
When you specify a year *less than* 1902 and *greater than* 2039 using specific versions of Ruby (i.e. 1.8.6p114) an `ArgumentError` will be raised. We recommend you upgrade Ruby to *at least* 1.8.7 to stop this problem occuring.
|
56
79
|
|
57
80
|
## Sum By Year (`sum_by_year`)
|
58
81
|
|
59
82
|
To sum records for the current year based on a field:
|
60
|
-
|
83
|
+
|
61
84
|
Invoice.sum_by_year(:value)
|
62
85
|
|
63
86
|
To sum records for a year based on a field:
|
64
87
|
|
65
88
|
Invoice.sum_by_year(:value, 09)
|
66
|
-
|
89
|
+
|
67
90
|
You can also pass it a full year:
|
68
|
-
|
91
|
+
|
69
92
|
Invoice.sum_by_year(:value, 2009)
|
70
|
-
|
93
|
+
|
71
94
|
## Count By Year (`count_by_year`)
|
72
95
|
|
73
96
|
To count the records in the current year regardless of field:
|
74
97
|
|
75
98
|
Invoice.count_by_year
|
76
|
-
|
99
|
+
|
77
100
|
To count records in the current year where only a specific field is set:
|
78
101
|
|
79
102
|
Invoice.count_by_year(:value)
|
80
|
-
|
103
|
+
|
81
104
|
To count records in a different year regardless of field:
|
82
105
|
|
83
106
|
Invoice.count_by_year(:all, :year => 2009)
|
84
|
-
|
107
|
+
|
85
108
|
## By Month (`by_month`)
|
86
109
|
|
87
110
|
If you know the number of the month you want:
|
88
|
-
|
111
|
+
|
89
112
|
Post.by_month(1)
|
90
|
-
|
113
|
+
|
91
114
|
This will return all posts in the first month (January) of the current year.
|
92
115
|
|
93
116
|
If you like being verbose:
|
@@ -97,19 +120,19 @@ If you like being verbose:
|
|
97
120
|
This will return all posts created in January of the current year.
|
98
121
|
|
99
122
|
If you want to find all posts in January of last year just do
|
100
|
-
|
123
|
+
|
101
124
|
Post.by_month(1, :year => 2007)
|
102
|
-
|
125
|
+
|
103
126
|
or
|
104
|
-
|
127
|
+
|
105
128
|
Post.by_month("January", :year => 2007)
|
106
|
-
|
129
|
+
|
107
130
|
This will perform a find using the column you've specified.
|
108
131
|
|
109
132
|
If you have a Time object you can use it to find the posts:
|
110
133
|
|
111
134
|
Post.by_month(Time.local(2008, 11, 24))
|
112
|
-
|
135
|
+
|
113
136
|
This will find all the posts in November 2008.
|
114
137
|
|
115
138
|
When you specify a year *less than* 1902 and *greater than* 2039 using specific versions of Ruby (i.e. 1.8.6p114) an `ArgumentError` will be raised. We recommend you upgrade Ruby to *at least* 1.8.7 to stop this problem occuring.
|
@@ -124,13 +147,13 @@ To sum records for the current month:
|
|
124
147
|
To sum records for a numbered month based on a field:
|
125
148
|
|
126
149
|
Invoice.sum_by_month(:value, 9)
|
127
|
-
|
150
|
+
|
128
151
|
You can also specify the name of the month:
|
129
152
|
|
130
153
|
Invoice.sum_by_month(:value, "September")
|
131
|
-
|
154
|
+
|
132
155
|
You can also lookup on a different year:
|
133
|
-
|
156
|
+
|
134
157
|
Invoice.sum_by_year(:value, 9, :year => "2009")
|
135
158
|
|
136
159
|
## Count By Month (`count_by_month`)
|
@@ -142,39 +165,39 @@ To count records for the current month regardless of field:
|
|
142
165
|
To count records for the current month where only a specific field is set:
|
143
166
|
|
144
167
|
Invoice.count_by_month(:value)
|
145
|
-
|
168
|
+
|
146
169
|
To count records for a different month regardless of field:
|
147
|
-
|
170
|
+
|
148
171
|
Invoice.count_by_month(:all, 9)
|
149
|
-
|
172
|
+
|
150
173
|
To count records for a different month in the current year:
|
151
174
|
|
152
175
|
Invoice.count_by_month(:number, 9)
|
153
|
-
|
176
|
+
|
154
177
|
To count records for a different month in a different year:
|
155
|
-
|
178
|
+
|
156
179
|
Invoice.count_by_month(:number, 9, :year => 2008)
|
157
|
-
|
180
|
+
|
158
181
|
## By Fortnight (`by_fortnight`)
|
159
182
|
|
160
183
|
Fortnight numbering starts at 0. The beginning of a fortnight is Monday, 12am.
|
161
184
|
|
162
185
|
To find records from the current fortnight:
|
163
|
-
|
186
|
+
|
164
187
|
Post.by_fortnight
|
165
|
-
|
188
|
+
|
166
189
|
To find records based on a fortnight, you can pass in a number (representing the fortnight number) or a time object:
|
167
190
|
|
168
191
|
Post.by_fortnight(18)
|
169
|
-
|
192
|
+
|
170
193
|
This will return all posts in the 18th fortnight of the current year.
|
171
194
|
|
172
195
|
Post.by_fortnight(18, :year => 2008)
|
173
|
-
|
196
|
+
|
174
197
|
This will return all posts in the 18th fortnight week of 2008.
|
175
198
|
|
176
199
|
Post.by_fortnight(Time.local(2008,1,1))
|
177
|
-
|
200
|
+
|
178
201
|
This will return all posts from the first fortnight of 2008.
|
179
202
|
|
180
203
|
## By Week (`by_week`)
|
@@ -184,19 +207,19 @@ Week numbering starts at 0. The beginning of a week is Monday, 12am.
|
|
184
207
|
To find records from the current week:
|
185
208
|
|
186
209
|
Post.by_week
|
187
|
-
|
210
|
+
|
188
211
|
To find records based on a week, you can pass in a number (representing the week number) or a time object:
|
189
212
|
|
190
213
|
Post.by_week(36)
|
191
|
-
|
214
|
+
|
192
215
|
This will return all posts in the 36th week of the current year.
|
193
216
|
|
194
217
|
Post.by_week(36, :year => 2008)
|
195
|
-
|
218
|
+
|
196
219
|
This will return all posts in the 36th week of 2008.
|
197
220
|
|
198
221
|
Post.by_week(Time.local(2008,1,1))
|
199
|
-
|
222
|
+
|
200
223
|
This will return all posts from the first week of 2008.
|
201
224
|
|
202
225
|
## By Weekend (`by_weekend`)
|
@@ -204,22 +227,22 @@ This will return all posts from the first week of 2008.
|
|
204
227
|
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.
|
205
228
|
|
206
229
|
Post.by_weekend(Time.now)
|
207
|
-
|
230
|
+
|
208
231
|
## By Day (`by_day` or `today`)
|
209
232
|
|
210
233
|
To find records for today:
|
211
|
-
|
234
|
+
|
212
235
|
Post.by_day
|
213
236
|
Post.today
|
214
|
-
|
237
|
+
|
215
238
|
To find records for a certain day:
|
216
239
|
|
217
240
|
Post.by_day(Time.local(2008, 1, 1))
|
218
241
|
|
219
242
|
You can also pass a string:
|
220
|
-
|
243
|
+
|
221
244
|
Post.by_day("next tuesday")
|
222
|
-
|
245
|
+
|
223
246
|
This will return all posts for the given day.
|
224
247
|
|
225
248
|
## Sum By Day (`sum_by_day`)
|
@@ -253,16 +276,16 @@ If you are currently in a work week (between 3am Monday and 3pm Friday) this wil
|
|
253
276
|
To find all posts from the day after the current date:
|
254
277
|
|
255
278
|
Post.tomorrow
|
256
|
-
|
279
|
+
|
257
280
|
To find all posts after a given Date or Time object:
|
258
|
-
|
281
|
+
|
259
282
|
Post.tomorrow(Date.today + 2)
|
260
283
|
Post.tomorrow(Time.now + 5.days)
|
261
|
-
|
284
|
+
|
262
285
|
You can also pass a string:
|
263
|
-
|
286
|
+
|
264
287
|
Post.tomorrow("next tuesday")
|
265
|
-
|
288
|
+
|
266
289
|
## Yesterday (`yesterday`)
|
267
290
|
|
268
291
|
*This method has been shown to be shifty when passed a `Date` object, it is recommended that you pass it a `Time` object instead.*
|
@@ -270,16 +293,16 @@ You can also pass a string:
|
|
270
293
|
To find all posts from the day before the current date:
|
271
294
|
|
272
295
|
Post.yesterday
|
273
|
-
|
296
|
+
|
274
297
|
To find all posts before a given Date or Time object:
|
275
|
-
|
298
|
+
|
276
299
|
Post.yesterday(Date.today + 2)
|
277
300
|
Post.yesterday(Time.now + 5.days)
|
278
|
-
|
301
|
+
|
279
302
|
You can also pass a string:
|
280
|
-
|
303
|
+
|
281
304
|
Post.yesterday("next tuesday")
|
282
|
-
|
305
|
+
|
283
306
|
## Past (`past`)
|
284
307
|
|
285
308
|
To find all posts before the current time:
|
@@ -290,7 +313,7 @@ To find all posts before certain time or date:
|
|
290
313
|
|
291
314
|
Post.past(Date.today + 2)
|
292
315
|
Post.past(Time.now + 5.days)
|
293
|
-
|
316
|
+
|
294
317
|
You can also pass a string:
|
295
318
|
|
296
319
|
Post.past("next tuesday")
|
@@ -305,31 +328,31 @@ To find all posts after certain time or date:
|
|
305
328
|
|
306
329
|
Post.future(Date.today + 2)
|
307
330
|
Post.future(Time.now + 5.days)
|
308
|
-
|
331
|
+
|
309
332
|
You can also pass a string:
|
310
333
|
|
311
334
|
Post.future("next tuesday")
|
312
|
-
|
335
|
+
|
313
336
|
## Between (`between`)
|
314
337
|
|
315
338
|
To find records between two times:
|
316
339
|
|
317
340
|
Post.between(time1, time2)
|
318
|
-
|
341
|
+
|
319
342
|
Also works with dates:
|
320
|
-
|
343
|
+
|
321
344
|
Post.between(date1, date2)
|
322
|
-
|
345
|
+
|
323
346
|
And with strings:
|
324
347
|
|
325
348
|
Post.between("last tuesday", "next wednesday")
|
326
|
-
|
349
|
+
|
327
350
|
## As of (`as_of_<dynamic>`)
|
328
351
|
|
329
352
|
To find records as of a certain date up until the current time:
|
330
|
-
|
353
|
+
|
331
354
|
Post.as_of_2_weeks_ago
|
332
|
-
|
355
|
+
|
333
356
|
This uses the Chronic "human mind reading" (read: it's really good at determining what time you mean using written English) library to work it out.
|
334
357
|
|
335
358
|
## Up to (`up_to_<dynamic>`)
|
@@ -343,46 +366,31 @@ To find records up to a certain time from the current time:
|
|
343
366
|
If your database uses something other than `created_at` for storing a timestamp, you can specify the field option like this:
|
344
367
|
|
345
368
|
Post.by_month("January", :field => :something_else)
|
346
|
-
|
347
|
-
All methods support this extra option.
|
348
369
|
|
349
|
-
|
350
|
-
|
351
|
-
All the `by_*` methods take a block which will then scope the find based on the options passed into it. You can also specify these options for each method, but the syntax may differ. The supported options are the same options that are supported by `find` from ActiveRecord. Please note that if you want to use conditions you *have* to use this syntax:
|
352
|
-
|
353
|
-
Post.by_month(1) { { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] } }
|
354
|
-
|
355
|
-
or the lengthened:
|
356
|
-
|
357
|
-
Post.by_month(1) do
|
358
|
-
{ :include => "tags", :conditions => ["tags.name = ?", 'ruby'] }
|
359
|
-
end
|
360
|
-
|
361
|
-
An alternative syntax to this is:
|
370
|
+
All methods support this extra option.
|
362
371
|
|
363
|
-
Post.by_month(1, { :include => "tags", :conditions => ["tags.name = ?", 'ruby'] })
|
364
|
-
|
365
372
|
## Ordering records
|
366
373
|
|
367
374
|
To order the returned set of records you may specify an `:order` option which works the same was as a standard AR `:order` option:
|
368
375
|
|
369
376
|
Item.by_month(1, :order => "position DESC")
|
370
377
|
|
371
|
-
|
378
|
+
|
372
379
|
## "Chronicable string"
|
373
380
|
|
374
381
|
This means a string that can be parsed with the Chronic gem.
|
375
|
-
|
382
|
+
|
376
383
|
## Collaborators
|
377
|
-
|
384
|
+
|
378
385
|
Unfortunately I forget who exactly prompted me to write the plugin, but I would like to thank #rubyonrails for their support and the following people:
|
379
386
|
|
380
387
|
* Mislav Marohnic
|
381
388
|
* August Lilleas (leethal)
|
382
389
|
* gte351s
|
383
390
|
* Thomase Sinclair (anathematic)
|
391
|
+
* Sam Elliott (lenaryg)
|
384
392
|
* The dude(s) & gal(s) who created Chronic
|
385
|
-
|
393
|
+
|
386
394
|
## Suggestions?
|
387
395
|
|
388
396
|
If you have suggestions, please contact me at radarlistener@gmail.com
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.4
|
data/by_star.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{by_star}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ryan Bigg", "Mislav Marohni\304\207"]
|
12
|
-
s.date = %q{2010-03-
|
12
|
+
s.date = %q{2010-03-25}
|
13
13
|
s.description = %q{ActiveRecord extension for easier date scopes and time ranges}
|
14
14
|
s.email = %q{radarlistener@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
"Rakefile",
|
23
23
|
"VERSION",
|
24
24
|
"by_star.gemspec",
|
25
|
+
"cleaner.rb",
|
25
26
|
"init.rb",
|
26
27
|
"lib/by_star.rb",
|
27
28
|
"lib/calculations.rb",
|
data/cleaner.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
files = Dir["**/*"]
|
2
|
+
ignored_files = [
|
3
|
+
/log\/.*/,
|
4
|
+
]
|
5
|
+
|
6
|
+
files.delete_if do |file|
|
7
|
+
if File.directory?(file)
|
8
|
+
true
|
9
|
+
else
|
10
|
+
ignored_files.any? do |condition|
|
11
|
+
if condition.is_a?(String)
|
12
|
+
file == condition
|
13
|
+
else
|
14
|
+
condition.match(file)
|
15
|
+
end
|
16
|
+
end || false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
for file in files - ignored_files
|
21
|
+
if File.file?(file)
|
22
|
+
lines = File.readlines(file).map { |line| line.gsub(/^\s+$/, "\n") }
|
23
|
+
File.open(file, "w+") { |f| f.write(lines.join) }
|
24
|
+
end
|
25
|
+
end
|