dateslices 0.0.1 → 0.0.2

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: b46eaf82482052a2062f8e18b6995d24c227c7df
4
- data.tar.gz: ae3fd8f4cc8dde2995c7c407084057d874dc40d6
3
+ metadata.gz: b68aa17be20ba5b07f590ae06385ccf18b5a5c64
4
+ data.tar.gz: 15da28cf505321ef535b07a0a5e51859c9b19085
5
5
  SHA512:
6
- metadata.gz: dca53556f5ac3f9a58e66cd7ffeb324034dc74d7fe14429899aabe2d2bbfaef5ae86472fe768d1198c0e2421815153cc38c521d81f0b2980074bca1348934ded
7
- data.tar.gz: 6801b506c11d63a98118182c507c3760a7f0b5317205e09299969fb85224dcee7ed1f3e4a4e8ec70585d99053ee026de30fad537e1a5ba119f35678e611dcfc5
6
+ metadata.gz: df4ef7ce5e1bc900f25b55e92bf8ecaf843bc8869f5a6abcb35ef162267554b61d50f45cf1a9bdb516fa21bc112f9391956a35d2021c906ee3b4b6f9f186fc22
7
+ data.tar.gz: 987181e91f8152f6d6ba21b388f6330e42253188be26928158e3434d9cc31aa09cd8a8be63cd83c477028b142b3ae03f56f6ba7ba73a347e1e16b958eb974587
data/lib/dateslices.rb CHANGED
@@ -1,7 +1,5 @@
1
- require "active_support/core_ext/module/attribute_accessors"
2
- require "active_support/time"
3
- # require "groupdate/version"
4
- # require "dateslices/magic"
1
+ require 'active_support/core_ext/module/attribute_accessors'
2
+ require 'active_support/time'
5
3
 
6
4
  module Dateslices
7
5
  FIELDS = [:second, :minute, :hour, :day, :week, :day_of_week, :month, :year ]
@@ -12,14 +10,15 @@ module Dateslices
12
10
  self.day_start = 0
13
11
  end
14
12
 
15
- # require "groupdate/enumerable"
16
-
17
13
  begin
18
- require "active_record"
14
+ require 'active_record'
19
15
  rescue LoadError
20
16
  # do nothing
21
17
  end
22
18
 
19
+ require 'dateslices/sqlite'
20
+ require 'dateslices/postgresql'
21
+ require 'dateslices/mysql'
23
22
  require 'dateslices/scopes'
24
23
 
25
24
  ActiveRecord::Base.send(:extend, Dateslices::Scopes)
@@ -0,0 +1,28 @@
1
+ module Dateslices
2
+ module Mysql
3
+
4
+ def self.time_filter(column, field)
5
+ case field
6
+ when :day_of_week
7
+ "(DAYOFWEEK(#{column}) - 1)"
8
+ when :second
9
+ "DATE_FORMAT(#{column}, '%Y-%m-%d %H:%i:%S UTC')"
10
+ when :minute
11
+ "DATE_FORMAT(#{column}, '%Y-%m-%d %H:%i:00 UTC')"
12
+ when :hour
13
+ "DATE_FORMAT(#{column}, '%Y-%m-%d %H:00:00 UTC')"
14
+ when :day
15
+ "DATE_FORMAT(#{column}, '%Y-%m-%d 00:00:00 UTC')"
16
+ when :month
17
+ "DATE_FORMAT(#{column}, '%Y-%m-01 00:00:00 UTC')"
18
+ when :year
19
+ "DATE_FORMAT(#{column}, '%Y-01-01 00:00:00 UTC')"
20
+ when :week # Sigh...
21
+ "DATE_FORMAT( date_sub( created_at, interval ((weekday( created_at ) + 1)%7) day ), '%Y-%m-%d 00:00:00 UTC')"
22
+ else
23
+ throw "Implement #{field}"
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module Dateslices
2
+ module Postgresql
3
+
4
+ def self.time_filter(column, field)
5
+ case field
6
+ when :day_of_week
7
+ "EXTRACT(DOW from #{column})"
8
+ when :week # Postgres weeks start on monday
9
+ "(DATE_TRUNC( 'week', #{column} + INTERVAL '1 day' ) - INTERVAL '1 day')"
10
+ else
11
+ "DATE_TRUNC( '#{field.to_s}' , #{column} )"
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -1,79 +1,42 @@
1
1
  module Dateslices
2
2
  module Scopes
3
+
3
4
  Dateslices::FIELDS.each do |field|
4
5
  define_method :"group_by_#{field}" do |*args|
5
6
  args = args.dup
6
7
 
7
8
  column = args[0]
8
- column = "created_at" if column.blank?
9
+ column = 'created_at' if column.blank?
10
+
11
+ aggregation = args[1]
12
+ aggregation = 'count' if aggregation.blank?
13
+
14
+ aggregation_column = args[2]
15
+ aggregation_column = "*" if aggregation_column.blank?
9
16
 
10
- sql = ["count(*) as cnt"]
17
+ sql = ["#{aggregation}(#{aggregation_column}) as cnt"]
11
18
 
12
- time_filter = ""
19
+ time_filter = ''
13
20
 
14
21
  case connection.adapter_name
15
22
  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
23
+ time_filter = Dateslices::Sqlite.time_filter(column, field)
24
+ when 'PostgreSQL', 'PostGIS'
25
+ time_filter = Dateslices::Postgresql.time_filter(column, field)
26
+ when 'MySQL', 'Mysql2'
27
+ time_filter = Dateslices::Mysql.time_filter(column, field)
66
28
  else
67
29
  throw "Unknown database adaptor #{connection.adapter_name}"
68
30
  end
69
31
 
70
32
  sql << "#{time_filter} as date_slice"
71
33
 
72
- select( sql.join( ", " )).group("date_slice").order("date_slice").collect do |c|
73
- slice = c["date_slice"]
34
+ select( sql.join(', ')).where.not(column => nil).group('date_slice').order('date_slice').collect do |c|
35
+ slice = c['date_slice']
74
36
  slice = slice.to_i.to_s if slice.is_a? Float
75
37
  slice = slice.to_s if slice.is_a? Integer
76
- { date_slice: slice, count: c["cnt"] }
38
+ slice = slice.to_s
39
+ { date_slice: slice, aggregation.to_sym => c["cnt"] }
77
40
  end
78
41
  end
79
42
  end
@@ -0,0 +1,28 @@
1
+ module Dateslices
2
+ module Sqlite
3
+
4
+ def self.time_filter(column, field)
5
+ case field
6
+ when :second
7
+ "strftime( \"%Y-%m-%d %H:%M:%S UTC\", #{column} )"
8
+ when :minute
9
+ "strftime( \"%Y-%m-%d %H:%M:00 UTC\", #{column} )"
10
+ when :hour
11
+ "strftime( \"%Y-%m-%d %H:00:00 UTC\", #{column} )"
12
+ when :day
13
+ "strftime( \"%Y-%m-%d 00:00:00 UTC\", #{column} )"
14
+ when :week
15
+ "strftime('%Y-%m-%d 00:00:00 UTC', #{column}, '-6 days', 'weekday 0')"
16
+ when :month
17
+ "strftime( \"%Y-%m-01 00:00:00 UTC\", #{column} )"
18
+ when :year
19
+ "strftime( \"%Y-01-01 00:00:00 UTC\", #{column} )"
20
+ when :day_of_week
21
+ "strftime( \"%w\", #{column} )"
22
+ else
23
+ throw "Unknown time filter #{field}"
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Dateslices
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dateslices
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
  - Will Schenk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-19 00:00:00.000000000 Z
11
+ date: 2014-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.4
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 4.1.4
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -119,7 +119,10 @@ files:
119
119
  - MIT-LICENSE
120
120
  - Rakefile
121
121
  - lib/dateslices.rb
122
+ - lib/dateslices/mysql.rb
123
+ - lib/dateslices/postgresql.rb
122
124
  - lib/dateslices/scopes.rb
125
+ - lib/dateslices/sqlite.rb
123
126
  - lib/dateslices/version.rb
124
127
  - lib/tasks/dateslices_tasks.rake
125
128
  homepage: https://github.com/sublimeguile/dateslices