groupdate 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +1 -0
- data/groupdate.gemspec +25 -0
- data/lib/groupdate.rb +44 -0
- data/lib/groupdate/version.rb +3 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d0cda5c33577e2d10a7701383aca6cd9571c2b70
|
4
|
+
data.tar.gz: 474344cbd117c863f407db920986d93aeaca2dc7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 344ec7cb8dd73d77ec23f18cb6f92ca58368084da1a9b8c02c658af92d0266fbb95598c4255ad6bc8616d1fc6111ee3fe386e322b65ea4d826362de5e1ed5074
|
7
|
+
data.tar.gz: 142f62737693d891f0af21b31dc7de1d2a8a36e04b59a88d7d637725b3720df771348da697f02be86e6df0a619a641c009a39110f2d421a49a335bfe3c7f4097
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andrew Kane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Groupdate
|
2
|
+
|
3
|
+
The simplest way to group by:
|
4
|
+
|
5
|
+
- day
|
6
|
+
- week
|
7
|
+
- month
|
8
|
+
- year
|
9
|
+
- hour
|
10
|
+
- microseconds
|
11
|
+
- milliseconds
|
12
|
+
- second
|
13
|
+
- minute
|
14
|
+
- quarter
|
15
|
+
- decade
|
16
|
+
- century
|
17
|
+
- millennium
|
18
|
+
|
19
|
+
:tada: Time zones supported!!
|
20
|
+
|
21
|
+
PostgreSQL only at the moment - support for other datastores coming soon
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
User.group_by_day(:created_at).count
|
27
|
+
# => {2013-04-16 00:00:00 UTC=>50,2013-04-17 00:00:00 UTC=>100}
|
28
|
+
|
29
|
+
Task.group_by_month(:updated_at).count
|
30
|
+
# => {2013-04-01 00:00:00 UTC=>23,2013-04-01 00:00:00 UTC=>44}
|
31
|
+
|
32
|
+
Goal.group_by_year(:accomplished_at).count
|
33
|
+
# => {2012-01-01 00:00:00 UTC=>11,2013-01-01 00:00:00 UTC=>3}
|
34
|
+
```
|
35
|
+
|
36
|
+
The default time zone is `Time.zone`. Pass a time zone as the second argument.
|
37
|
+
|
38
|
+
```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}
|
41
|
+
```
|
42
|
+
|
43
|
+
Use it with anything that you can use `group` with:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
User.group_by_week(:created_at).sum(:tasks_count)
|
47
|
+
|
48
|
+
User.group_by_hour(:created_at).average(:tasks_count)
|
49
|
+
|
50
|
+
User.group_by_quarter(:created_at).maximum(:tasks_count)
|
51
|
+
|
52
|
+
User.group_by_second(:created_at).average(:tasks_count)
|
53
|
+
```
|
54
|
+
|
55
|
+
## Installation
|
56
|
+
|
57
|
+
Add this line to your application's Gemfile:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
gem 'groupdate'
|
61
|
+
```
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/groupdate.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'groupdate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "groupdate"
|
8
|
+
spec.version = Groupdate::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["acekane1@gmail.com"]
|
11
|
+
spec.description = %q{The simplest way to group temporal data}
|
12
|
+
spec.summary = %q{The simplest way to group temporal data}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", ">= 3.0.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/lib/groupdate.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "groupdate/version"
|
2
|
+
|
3
|
+
module Groupdate
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Pattern from kaminari
|
7
|
+
# https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/models/active_record_extension.rb
|
8
|
+
included do
|
9
|
+
# Future subclasses will pick up the model extension
|
10
|
+
class << self
|
11
|
+
def inherited_with_groupdate(kls) #:nodoc:
|
12
|
+
inherited_without_groupdate kls
|
13
|
+
kls.send(:include, ClassMethods) if kls.superclass == ActiveRecord::Base
|
14
|
+
end
|
15
|
+
alias_method_chain :inherited, :groupdate
|
16
|
+
end
|
17
|
+
|
18
|
+
# Existing subclasses pick up the model extension as well
|
19
|
+
self.descendants.each do |kls|
|
20
|
+
kls.send(:include, ClassMethods) if kls.superclass == ActiveRecord::Base
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
extend ActiveSupport::Concern
|
26
|
+
|
27
|
+
included do
|
28
|
+
# Field list from
|
29
|
+
# http://www.postgresql.org/docs/9.1/static/functions-datetime.html
|
30
|
+
%w(microseconds milliseconds second minute hour day week month quarter year decade century millennium).each do |field|
|
31
|
+
self.scope :"group_by_#{field}", lambda {|column, time_zone = Time.zone|
|
32
|
+
if time_zone.is_a?(ActiveSupport::TimeZone)
|
33
|
+
time_zone = time_zone.tzinfo.name
|
34
|
+
end
|
35
|
+
sql = "DATE_TRUNC('#{field}', #{column}::timestamptz AT TIME ZONE ?) AT TIME ZONE ?"
|
36
|
+
group(sanitize_sql_array([sql, time_zone, time_zone]))
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
ActiveRecord::Base.send :include, Groupdate
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: groupdate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: The simplest way to group temporal data
|
56
|
+
email:
|
57
|
+
- acekane1@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- groupdate.gemspec
|
68
|
+
- lib/groupdate.rb
|
69
|
+
- lib/groupdate/version.rb
|
70
|
+
homepage: ''
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.0.0
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: The simplest way to group temporal data
|
94
|
+
test_files: []
|