groupdate 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +23 -4
- data/lib/groupdate.rb +4 -2
- data/lib/groupdate/version.rb +1 -1
- data/test/groupdate_test.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 389dc65b32212f41266af6d39d4f422c5ed00143
|
4
|
+
data.tar.gz: b0345ab008fc00b1ae2ac0dffecb1b6677e29daf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe786c3908de9a1288f147b4bddac9f62d4e7f67c33734da2b94a98d1b2601ae8c35731a7cacdc0d439ba4a68d0a00dffe9554cee67ed95c907e111c44262e41
|
7
|
+
data.tar.gz: 6534a271d8e8a7730b58cdd0aa73f818a92194b3ee58d2259097634544b2667ef10be5ac75cd5368841e65e7601597f44227bbec4f9414c0e1264a70c1218c0a
|
data/README.md
CHANGED
@@ -16,21 +16,40 @@ PostgreSQL only at the moment - support for other datastores coming soon
|
|
16
16
|
|
17
17
|
```ruby
|
18
18
|
User.group_by_day(:created_at).count
|
19
|
-
#
|
19
|
+
# {
|
20
|
+
# "2013-04-16 00:00:00+00" => 50,
|
21
|
+
# "2013-04-17 00:00:00+00" => 100,
|
22
|
+
# "2013-04-18 00:00:00+00" => 34
|
23
|
+
# }
|
20
24
|
|
21
25
|
Task.group_by_month(:updated_at).count
|
22
|
-
#
|
26
|
+
# {
|
27
|
+
# "2013-02-01 00:00:00+00" => 84,
|
28
|
+
# "2013-03-01 00:00:00+00" => 23,
|
29
|
+
# "2013-04-01 00:00:00+00" => 44
|
30
|
+
# }
|
23
31
|
|
24
32
|
Goal.group_by_year(:accomplished_at).count
|
25
|
-
#
|
33
|
+
# {
|
34
|
+
# "2011-01-01 00:00:00+00" => 7,
|
35
|
+
# "2012-01-01 00:00:00+00" => 11,
|
36
|
+
# "2013-01-01 00:00:00+00" => 3
|
37
|
+
# }
|
26
38
|
```
|
27
39
|
|
28
40
|
The default time zone is `Time.zone`. Pass a time zone as the second argument.
|
29
41
|
|
30
42
|
```ruby
|
43
|
+
User.group_by_week(:created_at, "Pacific Time (US & Canada)").count
|
44
|
+
# {
|
45
|
+
# "2013-02-25 08:00:00+00" => 80,
|
46
|
+
# "2013-03-04 08:00:00+00" => 70,
|
47
|
+
# "2013-03-11 07:00:00+00" => 54
|
48
|
+
# }
|
49
|
+
|
50
|
+
# equivalently
|
31
51
|
time_zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
|
32
52
|
User.group_by_week(:created_at, time_zone).count
|
33
|
-
# => {"2013-04-16 07:00:00+00" => 80, "2013-04-17 07:00:00+00" => 70}
|
34
53
|
```
|
35
54
|
|
36
55
|
Use it with anything you can use `group` with:
|
data/lib/groupdate.rb
CHANGED
@@ -29,10 +29,12 @@ module Groupdate
|
|
29
29
|
# http://www.postgresql.org/docs/9.1/static/functions-datetime.html
|
30
30
|
%w(microseconds milliseconds second minute hour day week month quarter year decade century millennium).each do |field|
|
31
31
|
self.scope :"group_by_#{field}", lambda {|column, time_zone = Time.zone|
|
32
|
-
|
32
|
+
time_zone ||= "Etc/UTC"
|
33
|
+
if time_zone.is_a?(ActiveSupport::TimeZone) or time_zone = ActiveSupport::TimeZone[time_zone]
|
33
34
|
time_zone = time_zone.tzinfo.name
|
35
|
+
else
|
36
|
+
raise "Unrecognized time zone"
|
34
37
|
end
|
35
|
-
time_zone ||= "Etc/UTC"
|
36
38
|
sql = "DATE_TRUNC('#{field}', #{column}::timestamptz AT TIME ZONE ?) AT TIME ZONE ?"
|
37
39
|
group(sanitize_sql_array([sql, time_zone, time_zone]))
|
38
40
|
}
|
data/lib/groupdate/version.rb
CHANGED
data/test/groupdate_test.rb
CHANGED
@@ -45,7 +45,7 @@ class TestGroupdate < MiniTest::Unit::TestCase
|
|
45
45
|
"2013-03-31 07:00:00+00" => 2,
|
46
46
|
"2013-04-01 07:00:00+00" => 1
|
47
47
|
}
|
48
|
-
assert_equal expected, User.group_by_day(:created_at, "
|
48
|
+
assert_equal expected, User.group_by_day(:created_at, "Pacific Time (US & Canada)").count
|
49
49
|
end
|
50
50
|
|
51
51
|
def test_where
|