dateslices 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b46eaf82482052a2062f8e18b6995d24c227c7df
4
+ data.tar.gz: ae3fd8f4cc8dde2995c7c407084057d874dc40d6
5
+ SHA512:
6
+ metadata.gz: dca53556f5ac3f9a58e66cd7ffeb324034dc74d7fe14429899aabe2d2bbfaef5ae86472fe768d1198c0e2421815153cc38c521d81f0b2980074bca1348934ded
7
+ data.tar.gz: 6801b506c11d63a98118182c507c3760a7f0b5317205e09299969fb85224dcee7ed1f3e4a4e8ec70585d99053ee026de30fad537e1a5ba119f35678e611dcfc5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Dateslices'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
data/lib/dateslices.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "active_support/core_ext/module/attribute_accessors"
2
+ require "active_support/time"
3
+ # require "groupdate/version"
4
+ # require "dateslices/magic"
5
+
6
+ module Dateslices
7
+ FIELDS = [:second, :minute, :hour, :day, :week, :day_of_week, :month, :year ]
8
+ METHODS = FIELDS.map{|v| :"group_by_#{v}" }
9
+
10
+ mattr_accessor :week_start, :day_start, :time_zone
11
+ self.week_start = :sun
12
+ self.day_start = 0
13
+ end
14
+
15
+ # require "groupdate/enumerable"
16
+
17
+ begin
18
+ require "active_record"
19
+ rescue LoadError
20
+ # do nothing
21
+ end
22
+
23
+ require 'dateslices/scopes'
24
+
25
+ ActiveRecord::Base.send(:extend, Dateslices::Scopes)
@@ -0,0 +1,82 @@
1
+ module Dateslices
2
+ module Scopes
3
+ Dateslices::FIELDS.each do |field|
4
+ define_method :"group_by_#{field}" do |*args|
5
+ args = args.dup
6
+
7
+ column = args[0]
8
+ column = "created_at" if column.blank?
9
+
10
+ sql = ["count(*) as cnt"]
11
+
12
+ time_filter = ""
13
+
14
+ case connection.adapter_name
15
+ when 'SQLite'
16
+ time_filter = case field
17
+ when :second
18
+ "strftime( \"%Y-%m-%d %H:%M:%S UTC\", #{column} )"
19
+ when :minute
20
+ "strftime( \"%Y-%m-%d %H:%M:00 UTC\", #{column} )"
21
+ when :hour
22
+ "strftime( \"%Y-%m-%d %H:00:00 UTC\", #{column} )"
23
+ when :day
24
+ "strftime( \"%Y-%m-%d 00:00:00 UTC\", #{column} )"
25
+ when :week
26
+ "strftime('%Y-%m-%d 00:00:00 UTC', #{column}, '-6 days', 'weekday 0')"
27
+ when :month
28
+ "strftime( \"%Y-%m-01 00:00:00 UTC\", #{column} )"
29
+ when :year
30
+ "strftime( \"%Y-01-01 00:00:00 UTC\", #{column} )"
31
+ when :day_of_week
32
+ "strftime( \"%w\", #{column} )"
33
+ else
34
+ throw "Unknown time filter #{field}"
35
+ end
36
+ when "PostgreSQL", "PostGIS"
37
+ time_filter = case field
38
+ when :day_of_week
39
+ "EXTRACT(DOW from #{column})"
40
+ when :week # Postgres weeks start on monday
41
+ "(DATE_TRUNC( 'week', #{column} + INTERVAL '1 day' ) - INTERVAL '1 day')"
42
+ else
43
+ "DATE_TRUNC( '#{field.to_s}' , #{column} )"
44
+ end
45
+ when "MySQL"
46
+ time_filter = case field
47
+ when :day_of_week
48
+ "(DAYOFWEEK(#{column}) - 1)"
49
+ when :second
50
+ "DATE_FORMAT(#{column}, '%Y-%m-%d %H:%i:%S UTC')"
51
+ when :minute
52
+ "DATE_FORMAT(#{column}, '%Y-%m-%d %H:%i:00 UTC')"
53
+ when :hour
54
+ "DATE_FORMAT(#{column}, '%Y-%m-%d %H:00:00 UTC')"
55
+ when :day
56
+ "DATE_FORMAT(#{column}, '%Y-%m-%d 00:00:00 UTC')"
57
+ when :month
58
+ "DATE_FORMAT(#{column}, '%Y-%m-01 00:00:00 UTC')"
59
+ when :year
60
+ "DATE_FORMAT(#{column}, '%Y-01-01 00:00:00 UTC')"
61
+ when :week # Sigh...
62
+ "DATE_FORMAT( date_sub( created_at, interval ((weekday( created_at ) + 1)%7) day ), '%Y-%m-%d 00:00:00 UTC')"
63
+ else
64
+ throw "Implement #{field}"
65
+ end
66
+ else
67
+ throw "Unknown database adaptor #{connection.adapter_name}"
68
+ end
69
+
70
+ sql << "#{time_filter} as date_slice"
71
+
72
+ select( sql.join( ", " )).group("date_slice").order("date_slice").collect do |c|
73
+ slice = c["date_slice"]
74
+ slice = slice.to_i.to_s if slice.is_a? Float
75
+ slice = slice.to_s if slice.is_a? Integer
76
+ { date_slice: slice, count: c["cnt"] }
77
+ end
78
+ end
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module Dateslices
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :dateslices do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dateslices
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Will Schenk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
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
+ - !ruby/object:Gem::Dependency
56
+ name: mysql
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: rspec-rails
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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-autotest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: autotest-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A Rails 4 ActiveRecord plugin that adds group_by_day, group_by_month,
112
+ etc. Not timezone aware, but supports sqlite.
113
+ email:
114
+ - will@happyfuncorp.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - MIT-LICENSE
120
+ - Rakefile
121
+ - lib/dateslices.rb
122
+ - lib/dateslices/scopes.rb
123
+ - lib/dateslices/version.rb
124
+ - lib/tasks/dateslices_tasks.rake
125
+ homepage: https://github.com/sublimeguile/dateslices
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.2.2
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: A Rails 4 ActiveRecord plugin that adds group_by_day, group_by_month, etc.
149
+ test_files: []