ranged_find 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +24 -0
- data/lib/dreamer3/railtie.rb +10 -0
- data/lib/dreamer3/ranged_find.rb +59 -0
- data/lib/ranged_find.rb +1 -0
- metadata +70 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Josh Goebel
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
WHAT IS IT?
|
2
|
+
===========
|
3
|
+
|
4
|
+
Allows you to easily do queries based on date ranges.
|
5
|
+
|
6
|
+
You can either pass a symbol:
|
7
|
+
|
8
|
+
:today, :yesterday, :last_year, :this_year, :next_year, :this_month,
|
9
|
+
:last_month, :next_month, :month_to_date
|
10
|
+
|
11
|
+
Or you can pass a range:
|
12
|
+
|
13
|
+
5.days.ago .. 10.days.from_now
|
14
|
+
|
15
|
+
|
16
|
+
Examples
|
17
|
+
--------
|
18
|
+
|
19
|
+
Book.when(:yesterday).count # defaults filtering based on created_at field
|
20
|
+
|
21
|
+
Book.when(:this_month,:updated_at).count
|
22
|
+
|
23
|
+
Book.when(Date.civil(2002)..Date.civil(2006),:deleted_at).count
|
24
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Dreamer3 # :nodoc:
|
2
|
+
module RangedFind # :nodoc:
|
3
|
+
|
4
|
+
DATE_RANGES={
|
5
|
+
# daily
|
6
|
+
:yesterday => [ Time.zone.now.yesterday.midnight, 1.day ],
|
7
|
+
:today => [ Time.zone.now.midnight, 1.day ],
|
8
|
+
:tomorrow => [ Time.zone.now.tomorrow.midnight, 1.day ],
|
9
|
+
# yearly
|
10
|
+
:last_year => [ Time.zone.now.prev_year.beginning_of_year, 1.year ],
|
11
|
+
:this_year => [ Time.zone.now.beginning_of_year, 1.year ],
|
12
|
+
:next_year => [ Time.zone.now.next_year.beginning_of_year, 1.year ],
|
13
|
+
# monthly
|
14
|
+
:last_month => [ Time.zone.now.prev_month.beginning_of_month, 1.month ],
|
15
|
+
:this_month => [ Time.zone.now.beginning_of_month, 1.month ],
|
16
|
+
:next_month => [ Time.zone.now.next_month.beginning_of_month, 1.month ],
|
17
|
+
}
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
|
21
|
+
def when(range, field = "created_at")
|
22
|
+
return self if range.nil?
|
23
|
+
field = "#{table_name}.#{field}" unless field.to_s.include?(".")
|
24
|
+
self.where(date_range_conditions(field, range))
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# returns a conditions array to scope a specific field to a certain time range
|
30
|
+
def date_range_conditions(field, time_range)
|
31
|
+
conditions = ["? <= #{field} and #{field} < ?"]
|
32
|
+
if time_range.is_a?(Range)
|
33
|
+
conditions << time_range.begin
|
34
|
+
conditions << time_range.end
|
35
|
+
elsif range=DATE_RANGES[time_range]
|
36
|
+
conditions << range.first
|
37
|
+
conditions << (range.last===Time ? range.last : range.first + range.last)
|
38
|
+
else
|
39
|
+
case time_range
|
40
|
+
# calcuates the count of last month, but only as far as we are in the current month
|
41
|
+
# so if we're half way thru April, then this will only show the stats as of half way thru March
|
42
|
+
when :last_month_to_date
|
43
|
+
end_of_this_month=(Time.zone.now.next_month.beginning_of_month-1.day).mday
|
44
|
+
end_of_last_month=(Time.zone.now.beginning_of_month-1.day).mday
|
45
|
+
ratio=Time.zone.now.mday.to_f/end_of_this_month
|
46
|
+
end_date=Time.zone.now.prev_month.beginning_of_month.midnight+(ratio*end_of_last_month).days
|
47
|
+
|
48
|
+
conditions << Time.zone.now.prev_month.beginning_of_month
|
49
|
+
conditions << end_date
|
50
|
+
else
|
51
|
+
raise "invalid date range provided"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
conditions
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/ranged_find.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'dreamer3/railtie' if defined?(Rails)
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ranged_find
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Josh Goebel
|
13
|
+
autorequire: ranged_find
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-12-23 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Helpler for making date range queries easier in Rails
|
22
|
+
email: me@joshgoebel.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/dreamer3/railtie.rb
|
31
|
+
- lib/dreamer3/ranged_find.rb
|
32
|
+
- lib/ranged_find.rb
|
33
|
+
- MIT-LICENSE
|
34
|
+
- README
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: https://github.com/yyyc514/ranged_find
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements:
|
63
|
+
- none
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.6.2
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Helpler for making date range queries easier in Rails
|
69
|
+
test_files: []
|
70
|
+
|