groupdate 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -12
- data/.travis.yml +0 -1
- data/CHANGELOG.md +5 -0
- data/README.md +3 -0
- data/lib/groupdate.rb +5 -0
- data/lib/groupdate/scopes.rb +1 -1
- data/lib/groupdate/series.rb +5 -5
- data/lib/groupdate/version.rb +1 -1
- data/test/mysql_test.rb +3 -6
- data/test/postgresql_test.rb +5 -8
- data/test/test_helper.rb +44 -19
- metadata +45 -45
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
OGE0ZDVhNmNjNDNjYWI4ZmFlNGY4MTYzNTc5M2VjNDRlMWI4MjUwYQ==
|
3
|
+
metadata.gz: aca7ed94e70ba77cb94f64a51398c1c7a8b3a888
|
4
|
+
data.tar.gz: f8601b3f0fc5c9ee1a0886ecd717b02901c215ef
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
ZTRkM2RiYjY0ODZkM2IyOGQxMzU4NTA4MTNiOGNjNWRlMjUyZWQ2MDNhYWQw
|
11
|
-
ZDlkOGVkYjZiOGE4MTE5MDBiYzUxZmQxYjQ3NjBiYmNjYWJkZjQ=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
OWVmMTM2YmU1Y2IyY2ZmOGY5MWEzOTAxYjQ5NGIwNTVkOWM1Y2Q4NDZiOGVi
|
14
|
-
ODQ5MWZkZGM5ZDkzODU2ODlmZDk2MGU2NGMyOTFkZDRlZDU5ZDM4MmJkM2Q1
|
15
|
-
NTIyZjE0MzM4NzkzYzVmYWIzNzk0MWI1YjE2NTVlNzY4NWUxNGI=
|
6
|
+
metadata.gz: 285ff44107dc4028bf1369203abcf466443a0faf90d36060cdfc688511c87ca33b88a242cd37c6b9ad45f0c5f9702b381ef3a3fed00ce6d5b3e6b5b98934887e
|
7
|
+
data.tar.gz: 2a068e99ecdfe6c6feaf1dd1a6134440dcaf17b284fa85488abe57adabc27c2bfd4228c1a5307efe1cca4a0820c8d39c2cc43fd92f895cff6c6637d32fb278e8
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -68,6 +68,9 @@ User.group_by_week(:created_at, :start => :mon) # first three letters of day
|
|
68
68
|
|
69
69
|
# must be the last argument
|
70
70
|
User.group_by_week(:created_at, time_zone, :start => :sat)
|
71
|
+
|
72
|
+
# change globally
|
73
|
+
Groupdate.week_start = :mon
|
71
74
|
```
|
72
75
|
|
73
76
|
You can also group by the day of the week or hour of the day.
|
data/lib/groupdate.rb
CHANGED
data/lib/groupdate/scopes.rb
CHANGED
@@ -19,7 +19,7 @@ module Groupdate
|
|
19
19
|
end
|
20
20
|
|
21
21
|
# for week
|
22
|
-
week_start = [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:start] ||
|
22
|
+
week_start = [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:start] || Groupdate.week_start).to_sym)
|
23
23
|
if field == "week" and !week_start
|
24
24
|
raise "Unrecognized :start option"
|
25
25
|
end
|
data/lib/groupdate/series.rb
CHANGED
@@ -2,9 +2,11 @@ module Groupdate
|
|
2
2
|
class Series
|
3
3
|
|
4
4
|
def initialize(relation, field, column, time_zone, time_range, week_start)
|
5
|
-
@relation = relation
|
6
5
|
if time_range.is_a?(Range)
|
7
|
-
|
6
|
+
# doesn't matter whether we include the end of a ... range - it will be excluded later
|
7
|
+
@relation = relation.where("#{column} >= ? AND #{column} <= ?", time_range.first, time_range.last)
|
8
|
+
else
|
9
|
+
@relation = relation.where("#{column} IS NOT NULL")
|
8
10
|
end
|
9
11
|
@field = field
|
10
12
|
@time_zone = time_zone
|
@@ -23,9 +25,7 @@ module Groupdate
|
|
23
25
|
lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).utc }
|
24
26
|
end
|
25
27
|
|
26
|
-
count = Hash[count.map
|
27
|
-
[cast_method.call(k), v]
|
28
|
-
end]
|
28
|
+
count = Hash[ count.map{|k, v| [cast_method.call(k), v] } ]
|
29
29
|
|
30
30
|
series =
|
31
31
|
case @field
|
data/lib/groupdate/version.rb
CHANGED
data/test/mysql_test.rb
CHANGED
@@ -1,18 +1,15 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
|
-
class TestMysql < Minitest::
|
3
|
+
class TestMysql < Minitest::Unit::TestCase
|
4
4
|
include TestGroupdate
|
5
5
|
|
6
6
|
def setup
|
7
|
+
super
|
7
8
|
User.establish_connection :adapter => "mysql2", :database => "groupdate_test", :username => "root"
|
8
9
|
end
|
9
10
|
|
10
11
|
def time_key(key)
|
11
|
-
|
12
|
-
key.utc.strftime("%Y-%m-%d %H:%M:%S").gsub(/ 00\:00\:00\z/, "")
|
13
|
-
else
|
14
|
-
key
|
15
|
-
end
|
12
|
+
key
|
16
13
|
end
|
17
14
|
|
18
15
|
def number_key(key)
|
data/test/postgresql_test.rb
CHANGED
@@ -1,21 +1,18 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
|
-
class TestPostgresql < Minitest::
|
3
|
+
class TestPostgresql < Minitest::Unit::TestCase
|
4
4
|
include TestGroupdate
|
5
5
|
|
6
6
|
def setup
|
7
|
+
super
|
7
8
|
User.establish_connection :adapter => "postgresql", :database => "groupdate_test"
|
8
9
|
end
|
9
10
|
|
10
11
|
def time_key(key)
|
11
|
-
if
|
12
|
-
key.utc.strftime("%Y-%m-%d %H:%M:%S
|
12
|
+
if ActiveRecord::VERSION::MAJOR == 3
|
13
|
+
key.utc.strftime("%Y-%m-%d %H:%M:%S+00")
|
13
14
|
else
|
14
|
-
|
15
|
-
key.utc.strftime("%Y-%m-%d %H:%M:%S+00")
|
16
|
-
else
|
17
|
-
key
|
18
|
-
end
|
15
|
+
key
|
19
16
|
end
|
20
17
|
end
|
21
18
|
|
data/test/test_helper.rb
CHANGED
@@ -4,6 +4,11 @@ require "minitest/autorun"
|
|
4
4
|
require "minitest/pride"
|
5
5
|
require "logger"
|
6
6
|
|
7
|
+
# TODO determine why this is necessary
|
8
|
+
if RUBY_PLATFORM == "java"
|
9
|
+
ENV["TZ"] = "UTC"
|
10
|
+
end
|
11
|
+
|
7
12
|
# for debugging
|
8
13
|
# ActiveRecord::Base.logger = Logger.new(STDOUT)
|
9
14
|
|
@@ -18,17 +23,19 @@ end
|
|
18
23
|
%w(postgresql mysql2).each do |adapter|
|
19
24
|
ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil
|
20
25
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
t.timestamps
|
26
|
-
end
|
26
|
+
ActiveRecord::Migration.create_table :users, :force => true do |t|
|
27
|
+
t.string :name
|
28
|
+
t.integer :score
|
29
|
+
t.timestamps
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
30
33
|
module TestGroupdate
|
31
34
|
|
35
|
+
def setup
|
36
|
+
Groupdate.week_start = :sun
|
37
|
+
end
|
38
|
+
|
32
39
|
# second
|
33
40
|
|
34
41
|
def test_second_end_of_second
|
@@ -271,7 +278,7 @@ module TestGroupdate
|
|
271
278
|
7.times do |n|
|
272
279
|
expected[n] = n == 3 ? 1 : 0
|
273
280
|
end
|
274
|
-
assert_equal
|
281
|
+
assert_equal expected, User.group_by_day_of_week(:created_at, Time.zone, true).count(:created_at)
|
275
282
|
end
|
276
283
|
|
277
284
|
def test_zeros_hour_of_day
|
@@ -280,7 +287,7 @@ module TestGroupdate
|
|
280
287
|
24.times do |n|
|
281
288
|
expected[n] = n == 20 ? 1 : 0
|
282
289
|
end
|
283
|
-
assert_equal
|
290
|
+
assert_equal expected, User.group_by_hour_of_day(:created_at, Time.zone, true).count(:created_at)
|
284
291
|
end
|
285
292
|
|
286
293
|
def test_zeros_excludes_end
|
@@ -288,7 +295,7 @@ module TestGroupdate
|
|
288
295
|
expected = {
|
289
296
|
Time.parse("2013-05-01 00:00:00 UTC") => 0
|
290
297
|
}
|
291
|
-
assert_equal
|
298
|
+
assert_equal expected, User.group_by_day(:created_at, Time.zone, Time.parse("2013-05-01 00:00:00 UTC")...Time.parse("2013-05-02 00:00:00 UTC")).count
|
292
299
|
end
|
293
300
|
|
294
301
|
def test_zeros_previous_scope
|
@@ -296,7 +303,7 @@ module TestGroupdate
|
|
296
303
|
expected = {
|
297
304
|
Time.parse("2013-05-01 00:00:00 UTC") => 0
|
298
305
|
}
|
299
|
-
assert_equal
|
306
|
+
assert_equal expected, User.where("id = 0").group_by_day(:created_at, Time.zone, Time.parse("2013-05-01 00:00:00 UTC")..Time.parse("2013-05-01 23:59:59 UTC")).count
|
300
307
|
end
|
301
308
|
|
302
309
|
def test_zeros_datetime
|
@@ -304,30 +311,48 @@ module TestGroupdate
|
|
304
311
|
expected = {
|
305
312
|
Time.parse("2013-05-01 00:00:00 UTC") => 1
|
306
313
|
}
|
307
|
-
assert_equal
|
314
|
+
assert_equal expected, User.group_by_day(:created_at, Time.zone, DateTime.parse("2013-05-01 00:00:00 UTC")..DateTime.parse("2013-05-01 00:00:00 UTC")).count
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_zeros_null_value
|
318
|
+
user = User.create!(name: "Andrew")
|
319
|
+
user.update_column :created_at, nil
|
320
|
+
assert_equal 0, User.group_by_hour_of_day(:created_at, Time.zone, true).count[0]
|
321
|
+
end
|
322
|
+
|
323
|
+
# week_start
|
324
|
+
|
325
|
+
def test_week_start
|
326
|
+
Groupdate.week_start = :mon
|
327
|
+
assert_result_time :week, "2013-03-18 00:00:00 UTC", "2013-03-24 23:59:59"
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_week_start_and_start_option
|
331
|
+
Groupdate.week_start = :mon
|
332
|
+
assert_result_time :week, "2013-03-16 00:00:00 UTC", "2013-03-22 23:59:59", false, :start => :sat
|
308
333
|
end
|
309
334
|
|
310
335
|
# misc
|
311
336
|
|
312
337
|
def test_order_day
|
313
|
-
|
338
|
+
assert_empty User.group_by_day(:created_at).order("day desc").limit(20).count
|
314
339
|
end
|
315
340
|
|
316
341
|
def test_order_week
|
317
|
-
|
342
|
+
assert_empty User.group_by_week(:created_at).order("week asc").count
|
318
343
|
end
|
319
344
|
|
320
345
|
def test_order_hour_of_day
|
321
|
-
|
346
|
+
assert_empty User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count
|
322
347
|
end
|
323
348
|
|
324
349
|
def test_table_name
|
325
|
-
|
350
|
+
assert_empty User.group_by_day("users.created_at").count
|
326
351
|
end
|
327
352
|
|
328
353
|
def test_previous_scopes
|
329
354
|
create_user "2013-05-01 00:00:00 UTC"
|
330
|
-
|
355
|
+
assert_empty User.where("id = 0").group_by_day(:created_at).count
|
331
356
|
end
|
332
357
|
|
333
358
|
# helpers
|
@@ -339,7 +364,7 @@ module TestGroupdate
|
|
339
364
|
def assert_result(method, expected, time_str, time_zone = false, options = {})
|
340
365
|
create_user time_str
|
341
366
|
expected = expected.is_a?(Time) ? time_key(expected) : number_key(expected)
|
342
|
-
assert_equal
|
367
|
+
assert_equal ordered_hash({expected => 1}), User.send(:"group_by_#{method}", :created_at, time_zone ? "Pacific Time (US & Canada)" : nil, options).order(method.to_s).count
|
343
368
|
end
|
344
369
|
|
345
370
|
def assert_zeros(method, created_at, keys, range_start, range_end, time_zone = nil, options = {})
|
@@ -348,7 +373,7 @@ module TestGroupdate
|
|
348
373
|
keys.each_with_index do |key, i|
|
349
374
|
expected[Time.parse(key)] = i == 1 ? 1 : 0
|
350
375
|
end
|
351
|
-
assert_equal
|
376
|
+
assert_equal expected, User.send(:"group_by_#{method}", :created_at, time_zone ? "Pacific Time (US & Canada)" : nil, Time.parse(range_start)..Time.parse(range_end), options).count
|
352
377
|
end
|
353
378
|
|
354
379
|
def ordered_hash(hash)
|
@@ -356,7 +381,7 @@ module TestGroupdate
|
|
356
381
|
end
|
357
382
|
|
358
383
|
def create_user(created_at)
|
359
|
-
User.create!
|
384
|
+
User.create! :name => "Andrew", :score => 1, :created_at => ActiveSupport::TimeZone["UTC"].parse(created_at)
|
360
385
|
end
|
361
386
|
|
362
387
|
def teardown
|
metadata
CHANGED
@@ -1,99 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groupdate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 3.0.0
|
20
15
|
requirement: !ruby/object:Gem::Requirement
|
21
16
|
requirements:
|
22
|
-
- -
|
17
|
+
- - '>='
|
23
18
|
- !ruby/object:Gem::Version
|
24
19
|
version: 3.0.0
|
25
|
-
prerelease: false
|
26
20
|
type: :runtime
|
27
|
-
|
28
|
-
name: bundler
|
21
|
+
prerelease: false
|
29
22
|
version_requirements: !ruby/object:Gem::Requirement
|
30
23
|
requirements:
|
31
|
-
- -
|
24
|
+
- - '>='
|
32
25
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
34
29
|
requirement: !ruby/object:Gem::Requirement
|
35
30
|
requirements:
|
36
|
-
- -
|
31
|
+
- - ~>
|
37
32
|
- !ruby/object:Gem::Version
|
38
33
|
version: '1.3'
|
39
|
-
prerelease: false
|
40
34
|
type: :development
|
41
|
-
|
42
|
-
name: rake
|
35
|
+
prerelease: false
|
43
36
|
version_requirements: !ruby/object:Gem::Requirement
|
44
37
|
requirements:
|
45
|
-
- -
|
38
|
+
- - ~>
|
46
39
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
44
|
requirements:
|
50
|
-
- -
|
45
|
+
- - '>='
|
51
46
|
- !ruby/object:Gem::Version
|
52
47
|
version: '0'
|
53
|
-
prerelease: false
|
54
48
|
type: :development
|
55
|
-
|
56
|
-
name: minitest
|
49
|
+
prerelease: false
|
57
50
|
version_requirements: !ruby/object:Gem::Requirement
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
62
57
|
requirement: !ruby/object:Gem::Requirement
|
63
58
|
requirements:
|
64
|
-
- -
|
59
|
+
- - '>='
|
65
60
|
- !ruby/object:Gem::Version
|
66
61
|
version: '0'
|
67
|
-
prerelease: false
|
68
62
|
type: :development
|
69
|
-
|
70
|
-
name: activerecord-jdbcpostgresql-adapter
|
63
|
+
prerelease: false
|
71
64
|
version_requirements: !ruby/object:Gem::Requirement
|
72
65
|
requirements:
|
73
|
-
- -
|
66
|
+
- - '>='
|
74
67
|
- !ruby/object:Gem::Version
|
75
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pg
|
76
71
|
requirement: !ruby/object:Gem::Requirement
|
77
72
|
requirements:
|
78
|
-
- -
|
73
|
+
- - '>='
|
79
74
|
- !ruby/object:Gem::Version
|
80
75
|
version: '0'
|
81
|
-
prerelease: false
|
82
76
|
type: :development
|
83
|
-
|
84
|
-
name: activerecord-jdbcmysql-adapter
|
77
|
+
prerelease: false
|
85
78
|
version_requirements: !ruby/object:Gem::Requirement
|
86
79
|
requirements:
|
87
|
-
- -
|
80
|
+
- - '>='
|
88
81
|
- !ruby/object:Gem::Version
|
89
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mysql2
|
90
85
|
requirement: !ruby/object:Gem::Requirement
|
91
86
|
requirements:
|
92
|
-
- -
|
87
|
+
- - '>='
|
93
88
|
- !ruby/object:Gem::Version
|
94
89
|
version: '0'
|
95
|
-
prerelease: false
|
96
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
97
|
description: The simplest way to group temporal data
|
98
98
|
email:
|
99
99
|
- acekane1@gmail.com
|
@@ -101,8 +101,8 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
-
-
|
105
|
-
-
|
104
|
+
- .gitignore
|
105
|
+
- .travis.yml
|
106
106
|
- CHANGELOG.md
|
107
107
|
- Gemfile
|
108
108
|
- LICENSE.txt
|
@@ -121,24 +121,24 @@ homepage: ''
|
|
121
121
|
licenses:
|
122
122
|
- MIT
|
123
123
|
metadata: {}
|
124
|
-
post_install_message:
|
124
|
+
post_install_message:
|
125
125
|
rdoc_options: []
|
126
126
|
require_paths:
|
127
127
|
- lib
|
128
128
|
required_ruby_version: !ruby/object:Gem::Requirement
|
129
129
|
requirements:
|
130
|
-
- -
|
130
|
+
- - '>='
|
131
131
|
- !ruby/object:Gem::Version
|
132
132
|
version: '0'
|
133
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
134
|
requirements:
|
135
|
-
- -
|
135
|
+
- - '>='
|
136
136
|
- !ruby/object:Gem::Version
|
137
137
|
version: '0'
|
138
138
|
requirements: []
|
139
|
-
rubyforge_project:
|
140
|
-
rubygems_version: 2.0.
|
141
|
-
signing_key:
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.0.0
|
141
|
+
signing_key:
|
142
142
|
specification_version: 4
|
143
143
|
summary: The simplest way to group temporal data
|
144
144
|
test_files:
|