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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6718db413e208c0735f68167fd6347a0e0770c28
4
- data.tar.gz: 15f3b021beac304cca17c302fecafe71ddc9249f
3
+ metadata.gz: 389dc65b32212f41266af6d39d4f422c5ed00143
4
+ data.tar.gz: b0345ab008fc00b1ae2ac0dffecb1b6677e29daf
5
5
  SHA512:
6
- metadata.gz: 12ec543e69db27f0f499e170b8d10e3475024d08c776673fbd30423d9979e4407c7c6bbbd6a2ecee18d3687715e09dc77eec2b78f02981be330eddbaddb3d154
7
- data.tar.gz: 7ae5b1a1d879fd25b4f20ff07c78c5840ec8b7b7241e876c3bc359c69c487adcdd1be9f5cf40f2fc6befae697bdf0da48606751a0957eff7ca74c40d9c419644
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
- # => {"2013-04-16 00:00:00+00" => 50, "2013-04-17 00:00:00+00" => 100}
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
- # => {"2013-03-01 00:00:00+00" => 23, "2013-04-01 00:00:00+00" => 44}
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
- # => {"2012-01-01 00:00:00+00" => 11, "2013-01-01 00:00:00+00" => 3}
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
- if defined?(ActiveSupport::TimeZone) and time_zone.is_a?(ActiveSupport::TimeZone)
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
  }
@@ -1,3 +1,3 @@
1
1
  module Groupdate
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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, "America/Los_Angeles").count
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groupdate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane