groupdate 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0cda5c33577e2d10a7701383aca6cd9571c2b70
4
- data.tar.gz: 474344cbd117c863f407db920986d93aeaca2dc7
3
+ metadata.gz: 6718db413e208c0735f68167fd6347a0e0770c28
4
+ data.tar.gz: 15f3b021beac304cca17c302fecafe71ddc9249f
5
5
  SHA512:
6
- metadata.gz: 344ec7cb8dd73d77ec23f18cb6f92ca58368084da1a9b8c02c658af92d0266fbb95598c4255ad6bc8616d1fc6111ee3fe386e322b65ea4d826362de5e1ed5074
7
- data.tar.gz: 142f62737693d891f0af21b31dc7de1d2a8a36e04b59a88d7d637725b3720df771348da697f02be86e6df0a619a641c009a39110f2d421a49a335bfe3c7f4097
6
+ metadata.gz: 12ec543e69db27f0f499e170b8d10e3475024d08c776673fbd30423d9979e4407c7c6bbbd6a2ecee18d3687715e09dc77eec2b78f02981be330eddbaddb3d154
7
+ data.tar.gz: 7ae5b1a1d879fd25b4f20ff07c78c5840ec8b7b7241e876c3bc359c69c487adcdd1be9f5cf40f2fc6befae697bdf0da48606751a0957eff7ca74c40d9c419644
data/README.md CHANGED
@@ -5,16 +5,8 @@ The simplest way to group by:
5
5
  - day
6
6
  - week
7
7
  - month
8
- - year
9
8
  - hour
10
- - microseconds
11
- - milliseconds
12
- - second
13
- - minute
14
- - quarter
15
- - decade
16
- - century
17
- - millennium
9
+ - *and more* (complete list at bottom)
18
10
 
19
11
  :tada: Time zones supported!!
20
12
 
@@ -24,34 +16,37 @@ PostgreSQL only at the moment - support for other datastores coming soon
24
16
 
25
17
  ```ruby
26
18
  User.group_by_day(:created_at).count
27
- # => {2013-04-16 00:00:00 UTC=>50,2013-04-17 00:00:00 UTC=>100}
19
+ # => {"2013-04-16 00:00:00+00" => 50, "2013-04-17 00:00:00+00" => 100}
28
20
 
29
21
  Task.group_by_month(:updated_at).count
30
- # => {2013-04-01 00:00:00 UTC=>23,2013-04-01 00:00:00 UTC=>44}
22
+ # => {"2013-03-01 00:00:00+00" => 23, "2013-04-01 00:00:00+00" => 44}
31
23
 
32
24
  Goal.group_by_year(:accomplished_at).count
33
- # => {2012-01-01 00:00:00 UTC=>11,2013-01-01 00:00:00 UTC=>3}
25
+ # => {"2012-01-01 00:00:00+00" => 11, "2013-01-01 00:00:00+00" => 3}
34
26
  ```
35
27
 
36
28
  The default time zone is `Time.zone`. Pass a time zone as the second argument.
37
29
 
38
30
  ```ruby
39
- User.group_by_day(:created_at, ActiveSupport::TimeZone["Pacific Time (US & Canada)"]).count
40
- # => {2013-04-16 00:00:00 UTC=>80,2013-04-17 00:00:00 UTC=>70}
31
+ time_zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
32
+ 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}
41
34
  ```
42
35
 
43
- Use it with anything that you can use `group` with:
36
+ Use it with anything you can use `group` with:
44
37
 
45
38
  ```ruby
46
- User.group_by_week(:created_at).sum(:tasks_count)
47
-
48
- User.group_by_hour(:created_at).average(:tasks_count)
39
+ Task.completed.group_by_hour(:completed_at).average(:priority)
40
+ ```
49
41
 
50
- User.group_by_quarter(:created_at).maximum(:tasks_count)
42
+ Go nuts!
51
43
 
52
- User.group_by_second(:created_at).average(:tasks_count)
44
+ ```ruby
45
+ Request.where(page: "/home").group_by_minute(:started_at).maximum(:request_time)
53
46
  ```
54
47
 
48
+ **Note:** On Rails 4 edge, queries return a Time object (much better!) as a result of [this commit](https://github.com/rails/rails/commit/2cc09441c2de57b024b11ba666ba1e72c2b20cfe)
49
+
55
50
  ## Installation
56
51
 
57
52
  Add this line to your application's Gemfile:
@@ -60,6 +55,22 @@ Add this line to your application's Gemfile:
60
55
  gem 'groupdate'
61
56
  ```
62
57
 
58
+ ## Complete list
59
+
60
+ - microseconds
61
+ - milliseconds
62
+ - second
63
+ - minute
64
+ - hour
65
+ - day
66
+ - week
67
+ - month
68
+ - quarter
69
+ - year
70
+ - decade
71
+ - century
72
+ - millennium
73
+
63
74
  ## Contributing
64
75
 
65
76
  1. Fork it
data/groupdate.gemspec CHANGED
@@ -22,4 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "pg"
25
27
  end
data/lib/groupdate.rb CHANGED
@@ -29,9 +29,10 @@ 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 time_zone.is_a?(ActiveSupport::TimeZone)
32
+ if defined?(ActiveSupport::TimeZone) and time_zone.is_a?(ActiveSupport::TimeZone)
33
33
  time_zone = time_zone.tzinfo.name
34
34
  end
35
+ time_zone ||= "Etc/UTC"
35
36
  sql = "DATE_TRUNC('#{field}', #{column}::timestamptz AT TIME ZONE ?) AT TIME ZONE ?"
36
37
  group(sanitize_sql_array([sql, time_zone, time_zone]))
37
38
  }
@@ -1,3 +1,3 @@
1
1
  module Groupdate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,54 @@
1
+ require "minitest/autorun"
2
+ require "active_record"
3
+ require "groupdate"
4
+ require "logger"
5
+
6
+ # for debugging
7
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
8
+
9
+ # rails does this in activerecord/lib/active_record/railtie.rb
10
+ ActiveRecord::Base.default_timezone = :utc
11
+ ActiveRecord::Base.time_zone_aware_attributes = true
12
+
13
+ # start connection
14
+ ActiveRecord::Base.establish_connection adapter: "postgresql", database: "groupdate"
15
+
16
+ # ActiveRecord::Migration.create_table :users do |t|
17
+ # t.string :name
18
+ # t.integer :score
19
+ # t.timestamps
20
+ # end
21
+
22
+ class User < ActiveRecord::Base
23
+ end
24
+
25
+ class TestGroupdate < MiniTest::Unit::TestCase
26
+ def setup
27
+ User.delete_all
28
+ [
29
+ {name: "Andrew", score: 1, created_at: Time.parse("2013-04-01 00:00:00 UTC")},
30
+ {name: "Jordan", score: 2, created_at: Time.parse("2013-04-01 00:00:00 UTC")},
31
+ {name: "Nick", score: 3, created_at: Time.parse("2013-04-02 00:00:00 UTC")}
32
+ ].each{|u| User.create!(u) }
33
+ end
34
+
35
+ def test_count
36
+ expected = {
37
+ "2013-04-01 00:00:00+00" => 2,
38
+ "2013-04-02 00:00:00+00" => 1
39
+ }
40
+ assert_equal expected, User.group_by_day(:created_at).count
41
+ end
42
+
43
+ def test_time_zone
44
+ expected = {
45
+ "2013-03-31 07:00:00+00" => 2,
46
+ "2013-04-01 07:00:00+00" => 1
47
+ }
48
+ assert_equal expected, User.group_by_day(:created_at, "America/Los_Angeles").count
49
+ end
50
+
51
+ def test_where
52
+ assert_equal({"2013-04-02 00:00:00+00" => 1}, User.where("score > 2").group_by_day(:created_at).count)
53
+ end
54
+ end
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description: The simplest way to group temporal data
56
84
  email:
57
85
  - acekane1@gmail.com
@@ -67,6 +95,7 @@ files:
67
95
  - groupdate.gemspec
68
96
  - lib/groupdate.rb
69
97
  - lib/groupdate/version.rb
98
+ - test/groupdate_test.rb
70
99
  homepage: ''
71
100
  licenses:
72
101
  - MIT
@@ -91,4 +120,5 @@ rubygems_version: 2.0.0
91
120
  signing_key:
92
121
  specification_version: 4
93
122
  summary: The simplest way to group temporal data
94
- test_files: []
123
+ test_files:
124
+ - test/groupdate_test.rb