dateslices 0.0.3 → 0.0.4

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: 3efa5cfa855a50c696f0d04409b2ed0187920eea
4
- data.tar.gz: 26c2e49f84cb16c2c3dcd2a2a1f5924e0f134f21
3
+ metadata.gz: c16dd2641e86c2165c15b1cd0344a106c1fe52b0
4
+ data.tar.gz: 48f5de4c75a9bcf1571371e47c462ec8d1e2a84c
5
5
  SHA512:
6
- metadata.gz: 854d8b6e033cf34ce2a673fcc2a5eb4b1968bb2869fb77b9a4d3038fd8f31968fe2d7684c56e4a83b10669cb99c53c0a2b0abad20014f7c65529b6b13f4a1ac0
7
- data.tar.gz: 0ec27b6e51699b05d77facf95b1dfa280c2bdc6021dba6711450789571d808efda8f81efef6d75c98df0eeac2cd475b7046e0fa55aea5ff2a668edf63502919c
6
+ metadata.gz: 96e2b2c4d504fd46d1ef70b9d8f3f857b5a53bdb8bacc98395a19dd23b15a07696d72d6dda814f3a2e4a8d09265c553816bf7dd569c9be1b0300056e40e0155d
7
+ data.tar.gz: 72b054a038755a9459091715a4aaec631b1063fc564b5389159831ae15c8c619de054487f15dd46f77855650ab7eeae90adcfd6de2ffd7a4edd11c12ba13b1c7
data/README.md CHANGED
@@ -5,16 +5,19 @@ This project rocks and uses MIT-LICENSE.
5
5
  This project is based upon [groupdate](https://github.com/ankane/groupdate) and follows a similar API.
6
6
 
7
7
  The differences between the groupdate and dateslices are:
8
- - dateslices supports sqlite3.
8
+ - dateslices supports sqlite3in addition to PostgreSQL & MySQL.
9
9
  - dateslices has much less functionality.
10
10
  - dateslices ignores timezones.
11
11
  - dateslices is rails 4 only.
12
12
  - dateslices uses rspecs for tests.
13
- - dateslices doesn't have hour_of_day since it doesn't deal with timezones
14
-
15
13
 
16
14
  The reason that I wrote this is that I use sqlite in development, and wanted to make sure that I could test things locally. I didn't understand the test suite of groupdate, so I ended up rewriting it.
17
15
 
16
+ ## Walkthrough
17
+
18
+ A walkthrough of how this code is written is available on [my blog](http://willschenk.com/dateslice-writing-rails-extensions/)
19
+
20
+ Usage instructions below.
18
21
 
19
22
  ## Example Usage
20
23
 
@@ -47,14 +50,18 @@ These methods take three optional arguments:
47
50
  - group_by_hour
48
51
  - group_by_day
49
52
  - group_by_week
50
- - group_by_day_of_week
51
53
  - group_by_month
52
54
  - group_by_year
53
55
 
56
+ - group_by_hour_of_day
57
+ - group_by_day_of_week
58
+ - group_by_day_of_month
59
+ - group_by_month_of_year
60
+
54
61
  ## Configuration
55
62
 
56
- The output format now defaults to a Chartkick compatible hash of dates and values.
57
- If you wish to use the dateslice format, please add `Dateslices.output_format = :dateslice` to an initializer.
63
+ The output format defaults to a Chartkick compatible hash of dates and values.
64
+ If you wish to use the dateslices format, please add `Dateslices.output_format = :dateslices` to an initializer.
58
65
 
59
66
  ## Tests
60
67
 
@@ -2,7 +2,7 @@ require 'active_support/core_ext/module/attribute_accessors'
2
2
  require 'active_support/time'
3
3
 
4
4
  module Dateslices
5
- FIELDS = [:second, :minute, :hour, :day, :week, :day_of_week, :month, :year ]
5
+ FIELDS = [:second, :minute, :hour, :day, :week, :month, :year, :hour_of_day, :day_of_week, :day_of_month, :month_of_year ]
6
6
  METHODS = FIELDS.map{|v| :"group_by_#{v}" }
7
7
 
8
8
  mattr_accessor :output_format
@@ -3,8 +3,14 @@ module Dateslices
3
3
 
4
4
  def self.time_filter(column, field)
5
5
  case field
6
+ when :hour_of_day
7
+ "(EXTRACT(HOUR from #{column}))"
6
8
  when :day_of_week
7
9
  "(DAYOFWEEK(#{column}) - 1)"
10
+ when :day_of_month
11
+ "DAYOFMONTH(#{column})"
12
+ when :month_of_year
13
+ "MONTH(#{column})"
8
14
  when :second
9
15
  "DATE_FORMAT(#{column}, '%Y-%m-%d %H:%i:%S UTC')"
10
16
  when :minute
@@ -3,8 +3,14 @@ module Dateslices
3
3
 
4
4
  def self.time_filter(column, field)
5
5
  case field
6
+ when :hour_of_day
7
+ "EXTRACT(HOUR from #{column})"
6
8
  when :day_of_week
7
9
  "EXTRACT(DOW from #{column})"
10
+ when :day_of_month
11
+ "EXTRACT(DAY from #{column})"
12
+ when :month_of_year
13
+ "EXTRACT(MONTH from #{column})"
8
14
  when :week # Postgres weeks start on monday
9
15
  "(DATE_TRUNC( 'week', #{column} + INTERVAL '1 day' ) - INTERVAL '1 day')"
10
16
  else
@@ -30,25 +30,22 @@ module Dateslices
30
30
  slices = select( sql.join(', ')).where.not(column => nil).group('date_slice').order('date_slice')
31
31
 
32
32
  if Dateslices.output_format == :groupdate
33
-
34
- slices.collect! do |c|
35
- slice = c['date_slice']
36
- slice = slice.is_a?(Float) ? slice.to_i.to_s : slice.to_s
37
- [slice, c['count']]
33
+ slices = slices.collect do |c|
34
+ [slice(c), c['count']]
38
35
  end
39
36
 
40
37
  Hash[slices]
41
38
  else
42
-
43
39
  slices.collect do |c|
44
- slice = c['date_slice']
45
- slice = slice.is_a?(Float) ? slice.to_i.to_s : slice.to_s
46
- { date_slice: slice, aggregation.to_sym => c['count'] }
40
+ { date_slice: slice(c), aggregation.to_sym => c['count'] }
47
41
  end
48
-
49
42
  end
43
+ end
44
+ end
50
45
 
51
- end
46
+ def slice(c)
47
+ slice = c['date_slice']
48
+ slice.is_a?(Float) ? slice.to_i.to_s : slice.to_s
52
49
  end
53
50
 
54
51
  end
@@ -17,8 +17,14 @@ module Dateslices
17
17
  "strftime( \"%Y-%m-01 00:00:00 UTC\", #{column} )"
18
18
  when :year
19
19
  "strftime( \"%Y-01-01 00:00:00 UTC\", #{column} )"
20
+ when :hour_of_day
21
+ "strftime( \"%H\", #{column} )"
20
22
  when :day_of_week
21
23
  "strftime( \"%w\", #{column} )"
24
+ when :day_of_month
25
+ "strftime( \"%d\", #{column} )"
26
+ when :month_of_year
27
+ "strftime( \"%m\", #{column} )"
22
28
  else
23
29
  throw "Unknown time filter #{field}"
24
30
  end
@@ -1,3 +1,3 @@
1
1
  module Dateslices
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dateslices
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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-12-07 00:00:00.000000000 Z
11
+ date: 2016-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
19
+ version: '4'
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.0'
26
+ version: '4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pg
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mysql
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec-rails
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec-autotest
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: autotest-rails
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  description: A Rails 4 ActiveRecord plugin that adds group_by_day, group_by_month,
@@ -136,17 +136,17 @@ require_paths:
136
136
  - lib
137
137
  required_ruby_version: !ruby/object:Gem::Requirement
138
138
  requirements:
139
- - - '>='
139
+ - - ">="
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
142
  required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  requirements:
144
- - - '>='
144
+ - - ">="
145
145
  - !ruby/object:Gem::Version
146
146
  version: '0'
147
147
  requirements: []
148
148
  rubyforge_project:
149
- rubygems_version: 2.2.2
149
+ rubygems_version: 2.4.8
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: A Rails 4 ActiveRecord plugin that adds group_by_day, group_by_month, etc.