by_star 2.2.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +5 -13
  2. data/.github/workflows/mysql.yml +92 -0
  3. data/.github/workflows/postgresql.yml +99 -0
  4. data/.gitignore +6 -5
  5. data/.travis.yml +92 -35
  6. data/CHANGELOG.md +59 -29
  7. data/Gemfile +18 -25
  8. data/MIT-LICENSE +20 -20
  9. data/README.md +616 -523
  10. data/Rakefile +18 -18
  11. data/UPGRADING +4 -12
  12. data/by_star.gemspec +34 -32
  13. data/cleaner.rb +24 -24
  14. data/lib/by_star/base.rb +69 -68
  15. data/lib/by_star/between.rb +185 -120
  16. data/lib/by_star/directional.rb +35 -21
  17. data/lib/by_star/kernel/date.rb +41 -0
  18. data/lib/by_star/kernel/in_time_zone.rb +20 -0
  19. data/lib/by_star/{kernel.rb → kernel/time.rb} +41 -41
  20. data/lib/by_star/normalization.rb +156 -118
  21. data/lib/by_star/orm/active_record/by_star.rb +75 -59
  22. data/lib/by_star/orm/mongoid/by_star.rb +90 -63
  23. data/lib/by_star/orm/mongoid/reorder.rb +23 -0
  24. data/lib/by_star/version.rb +3 -3
  25. data/lib/by_star.rb +18 -15
  26. data/spec/database.yml +15 -15
  27. data/spec/fixtures/active_record/models.rb +12 -10
  28. data/spec/fixtures/active_record/schema.rb +19 -19
  29. data/spec/fixtures/mongoid/models.rb +31 -29
  30. data/spec/fixtures/shared/seeds.rb +36 -26
  31. data/spec/gemfiles/Gemfile.rails +5 -0
  32. data/spec/gemfiles/Gemfile.rails32 +7 -0
  33. data/spec/gemfiles/Gemfile.rails40 +7 -0
  34. data/spec/gemfiles/Gemfile.rails41 +7 -0
  35. data/spec/gemfiles/Gemfile.rails42 +7 -0
  36. data/spec/gemfiles/Gemfile.rails50 +7 -0
  37. data/spec/gemfiles/Gemfile.rails51 +7 -0
  38. data/spec/gemfiles/Gemfile.rails52 +7 -0
  39. data/spec/gemfiles/Gemfile.rails60 +7 -0
  40. data/spec/gemfiles/Gemfile.rails61 +7 -0
  41. data/spec/integration/active_record/active_record_spec.rb +41 -53
  42. data/spec/integration/mongoid/mongoid_spec.rb +39 -46
  43. data/spec/integration/shared/at_time.rb +53 -0
  44. data/spec/integration/shared/between_dates.rb +99 -0
  45. data/spec/integration/shared/between_times.rb +99 -0
  46. data/spec/integration/shared/by_calendar_month.rb +55 -55
  47. data/spec/integration/shared/by_cweek.rb +54 -0
  48. data/spec/integration/shared/by_day.rb +120 -108
  49. data/spec/integration/shared/by_direction.rb +126 -114
  50. data/spec/integration/shared/by_fortnight.rb +48 -48
  51. data/spec/integration/shared/by_month.rb +50 -50
  52. data/spec/integration/shared/by_quarter.rb +49 -49
  53. data/spec/integration/shared/by_week.rb +54 -54
  54. data/spec/integration/shared/by_weekend.rb +49 -49
  55. data/spec/integration/shared/by_year.rb +48 -48
  56. data/spec/integration/shared/index_scope_parameter.rb +111 -0
  57. data/spec/integration/shared/offset_parameter.rb +32 -31
  58. data/spec/integration/shared/order_parameter.rb +36 -0
  59. data/spec/integration/shared/relative.rb +174 -174
  60. data/spec/spec_helper.rb +33 -29
  61. data/spec/unit/kernel_date_spec.rb +113 -0
  62. data/spec/unit/kernel_time_spec.rb +57 -57
  63. data/spec/unit/normalization_spec.rb +384 -255
  64. data/tmp/.gitignore +1 -1
  65. metadata +82 -68
  66. data/spec/integration/shared/scope_parameter.rb +0 -42
@@ -1,46 +1,39 @@
1
- require 'spec_helper'
2
- Dir[File.dirname(__FILE__) + '/../shared/*.rb'].each {|file| require file }
3
-
4
- describe 'Mongoid' do
5
-
6
- before(:all) do
7
- DATABASE_NAME = "mongoid_#{Process.pid}"
8
- # Moped.logger = Logger.new(STDOUT)
9
-
10
- Mongoid.configure do |config|
11
- config.connect_to DATABASE_NAME
12
- end
13
-
14
- load File.dirname(__FILE__) + '/../../fixtures/mongoid/models.rb'
15
- load File.dirname(__FILE__) + '/../../fixtures/shared/seeds.rb'
16
- end
17
-
18
- after(:all) do
19
- Mongoid.purge!
20
- end
21
-
22
- it_behaves_like 'by day'
23
- it_behaves_like 'by direction'
24
- it_behaves_like 'by fortnight'
25
- it_behaves_like 'by month'
26
- it_behaves_like 'by calendar month'
27
- it_behaves_like 'by quarter'
28
- it_behaves_like 'by week'
29
- it_behaves_like 'by weekend'
30
- it_behaves_like 'by year'
31
- it_behaves_like 'relative'
32
- it_behaves_like 'offset parameter'
33
- it_behaves_like 'scope parameter'
34
-
35
- describe '#between_times' do
36
- subject { Post.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-06')) }
37
- it { should be_a(Mongoid::Criteria) }
38
- its(:count) { should eq 3 }
39
- end
40
-
41
- describe '#between' do
42
- it 'should not override Mongoid between method' do
43
- Post.between(created_at: Time.parse('2014-01-01')..Time.parse('2014-01-06')).count.should eq 3
44
- end
45
- end
46
- end if testing_mongoid?
1
+ require 'spec_helper'
2
+ Dir[File.dirname(__FILE__) + '/../shared/*.rb'].each {|file| require file }
3
+
4
+ describe 'Mongoid' do
5
+
6
+ before(:all) do
7
+ DATABASE_NAME = "mongoid_#{Process.pid}"
8
+ # Moped.logger = Logger.new(STDOUT)
9
+
10
+ Mongoid.configure do |config|
11
+ config.connect_to DATABASE_NAME
12
+ end
13
+
14
+ load File.dirname(__FILE__) + '/../../fixtures/mongoid/models.rb'
15
+ load File.dirname(__FILE__) + '/../../fixtures/shared/seeds.rb'
16
+ end
17
+
18
+ after(:all) do
19
+ Mongoid.purge!
20
+ end
21
+
22
+ it_behaves_like 'between_times'
23
+ it_behaves_like 'between_dates'
24
+ it_behaves_like 'at_time'
25
+ it_behaves_like 'offset parameter'
26
+ it_behaves_like 'order parameter'
27
+ it_behaves_like 'index_scope parameter'
28
+ it_behaves_like 'by day'
29
+ it_behaves_like 'by direction'
30
+ it_behaves_like 'by fortnight'
31
+ it_behaves_like 'by month'
32
+ it_behaves_like 'by calendar month'
33
+ it_behaves_like 'by quarter'
34
+ it_behaves_like 'by week'
35
+ it_behaves_like 'by cweek'
36
+ it_behaves_like 'by weekend'
37
+ it_behaves_like 'by year'
38
+ it_behaves_like 'relative'
39
+ end if testing_mongoid?
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'at_time' do
4
+
5
+ describe '#at_time' do
6
+
7
+ context 'point object' do
8
+
9
+ context 'exactly equal' do
10
+ subject { Post.at_time(Time.zone.parse('2013-12-28 17:00:00')) }
11
+ it { expect(subject.count).to eql(1) }
12
+ end
13
+
14
+ context 'not exactly equal' do
15
+ subject { Post.at_time(Time.zone.parse('2013-12-28 17:00:01')) }
16
+ it { expect(subject.count).to eql(0) }
17
+ end
18
+ end
19
+
20
+ context 'timespan object' do
21
+
22
+ context 'before start time' do
23
+ subject { Event.at_time(Time.zone.parse('2013-12-23 16:59:59')) }
24
+ it { expect(subject.count).to eql(2) }
25
+ end
26
+
27
+ context 'at start time' do
28
+ subject { Event.at_time(Time.zone.parse('2013-12-23 17:00:00')) }
29
+ it { expect(subject.count).to eql(3) }
30
+ end
31
+
32
+ context 'after start time' do
33
+ subject { Event.at_time(Time.zone.parse('2013-12-23 17:00:01')) }
34
+ it { expect(subject.count).to eql(3) }
35
+ end
36
+
37
+ context 'before end time' do
38
+ subject { Event.at_time(Time.zone.parse('2013-11-06 16:59:59')) }
39
+ it { expect(subject.count).to eql(1) }
40
+ end
41
+
42
+ context 'at end time' do
43
+ subject { Event.at_time(Time.zone.parse('2013-11-06 17:00:00')) }
44
+ it { expect(subject.count).to eql(0) }
45
+ end
46
+
47
+ context 'after end time' do
48
+ subject { Event.at_time(Time.zone.parse('2013-11-06 17:00:01')) }
49
+ it { expect(subject.count).to eql(0) }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'between_dates' do
4
+
5
+ describe '#between_dates' do
6
+ subject { Post.between_dates(Time.zone.parse('2014-01-01'), Time.zone.parse('2014-01-06')) }
7
+
8
+ if testing_active_record?
9
+ it { is_expected.to be_a(ActiveRecord::Relation) }
10
+ else testing_mongoid?
11
+ it { is_expected.to be_a(Mongoid::Criteria) }
12
+ end
13
+
14
+ it { expect(subject.count).to eql(3) }
15
+
16
+ context 'one-sided query' do
17
+
18
+ context 'point query' do
19
+
20
+ context 'only start time' do
21
+ subject { Post.between_dates(Time.zone.parse('2014-01-01'), nil) }
22
+ it { expect(subject.count).to eql(12) }
23
+ end
24
+
25
+ context 'only end time' do
26
+ subject { Post.between_dates(nil, Time.zone.parse('2014-01-01')) }
27
+ it { expect(subject.count).to eql(12) }
28
+
29
+ context 'neither start nor end time' do
30
+ subject { Post.between_dates(nil, nil) }
31
+ it { expect(subject.count).to eql(22) }
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'timespan loose query' do
37
+
38
+ context 'only start time' do
39
+ subject { Event.between_dates(Time.zone.parse('2014-01-01'), nil, strict: false) }
40
+ it { expect(subject.count).to eql(17) }
41
+ end
42
+
43
+ context 'only end time' do
44
+ subject { Event.between_dates(nil, Time.zone.parse('2014-01-01'), strict: false) }
45
+ it { expect(subject.count).to eql(13) }
46
+
47
+ context 'neither start nor end time' do
48
+ subject { Event.between_dates(nil, nil) }
49
+ it { expect(subject.count).to eql(30) }
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'timespan strict query' do
55
+
56
+ context 'only start time' do
57
+ subject { Event.between_dates(Time.zone.parse('2014-01-01'), nil) }
58
+ it { expect(subject.count).to eql(17) }
59
+ end
60
+
61
+ context 'only end time' do
62
+ subject { Event.between_dates(nil, Time.zone.parse('2014-01-01')) }
63
+ it { expect(subject.count).to eql(13) }
64
+
65
+ context 'neither start nor end time' do
66
+ subject { Event.between_dates(nil, nil) }
67
+ it { expect(subject.count).to eql(30) }
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'two-sided query' do
74
+ context 'DST starts (Sydney)', sydney: true do
75
+ context 'day before' do
76
+ subject { Event.between_dates(Date.parse('2020-04-04'), Date.parse('2020-04-04'), offset: 5.hours) }
77
+ it { expect(subject.count).to eql(3) }
78
+ end
79
+
80
+ context 'same day' do
81
+ subject { Event.between_dates(Date.parse('2020-04-05'), Date.parse('2020-04-05'), offset: 5.hours) }
82
+ it { expect(subject.count).to eql(1) }
83
+ end
84
+ end
85
+
86
+ context 'when DST ends (Sydney)', sydney: true do
87
+ context 'day before' do
88
+ subject { Event.between_dates(Date.parse('2020-10-03'), Date.parse('2020-10-03'), offset: 5.hours) }
89
+ it { expect(subject.count).to eql(1) }
90
+ end
91
+
92
+ context 'same day' do
93
+ subject { Event.between_dates(Date.parse('2020-10-04'), Date.parse('2020-10-04'), offset: 5.hours) }
94
+ it { expect(subject.count).to eql(3) }
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'between_times' do
4
+
5
+ describe '#between_times' do
6
+ subject { Post.between_times(Time.zone.parse('2014-01-01'), Time.zone.parse('2014-01-06')) }
7
+
8
+ if testing_active_record?
9
+ it { is_expected.to be_a(ActiveRecord::Relation) }
10
+ else testing_mongoid?
11
+ it { is_expected.to be_a(Mongoid::Criteria) }
12
+ end
13
+
14
+ it { expect(subject.count).to eql(3) }
15
+
16
+ context 'one-sided query' do
17
+
18
+ context 'point query' do
19
+
20
+ context 'only start time' do
21
+ subject { Post.between_times(Time.zone.parse('2014-01-01'), nil) }
22
+ it { expect(subject.count).to eql(12) }
23
+ end
24
+
25
+ context 'only end time' do
26
+ subject { Post.between_times(nil, Time.zone.parse('2014-01-01')) }
27
+ it { expect(subject.count).to eql(10) }
28
+
29
+ context 'neither start nor end time' do
30
+ subject { Post.between_times(nil, nil) }
31
+ it { expect(subject.count).to eql(22) }
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'timespan loose query' do
37
+
38
+ context 'only start time' do
39
+ subject { Event.between_times(Time.zone.parse('2014-01-01'), nil, strict: false) }
40
+ it { expect(subject.count).to eql(17) }
41
+ end
42
+
43
+ context 'only end time' do
44
+ subject { Event.between_times(nil, Time.zone.parse('2014-01-01'), strict: false) }
45
+ it { expect(subject.count).to eql(13) }
46
+
47
+ context 'neither start nor end time' do
48
+ subject { Event.between_times(nil, nil) }
49
+ it { expect(subject.count).to eql(30) }
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'timespan strict query' do
55
+
56
+ context 'only start time' do
57
+ subject { Event.between_times(Time.zone.parse('2014-01-01'), nil) }
58
+ it { expect(subject.count).to eql(17) }
59
+ end
60
+
61
+ context 'only end time' do
62
+ subject { Event.between_times(nil, Time.zone.parse('2014-01-01')) }
63
+ it { expect(subject.count).to eql(13) }
64
+
65
+ context 'neither start nor end time' do
66
+ subject { Event.between_times(nil, nil) }
67
+ it { expect(subject.count).to eql(30) }
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'two-sided query' do
74
+ context 'DST starts (Sydney)', sydney: true do
75
+ context 'day before' do
76
+ subject { Event.between_times(Date.parse('2020-04-04'), Date.parse('2020-04-04'), offset: 5.hours) }
77
+ it { expect(subject.count).to eql(3) }
78
+ end
79
+
80
+ context 'same day' do
81
+ subject { Event.between_times(Date.parse('2020-04-05'), Date.parse('2020-04-05'), offset: 5.hours) }
82
+ it { expect(subject.count).to eql(1) }
83
+ end
84
+ end
85
+
86
+ context 'when DST ends (Sydney)', sydney: true do
87
+ context 'day before' do
88
+ subject { Event.between_times(Date.parse('2020-10-03'), Date.parse('2020-10-03'), offset: 5.hours) }
89
+ it { expect(subject.count).to eql(1) }
90
+ end
91
+
92
+ context 'same day' do
93
+ subject { Event.between_times(Date.parse('2020-10-04'), Date.parse('2020-10-04'), offset: 5.hours) }
94
+ it { expect(subject.count).to eql(3) }
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -1,55 +1,55 @@
1
- require 'spec_helper'
2
-
3
- shared_examples_for 'by calendar month' do
4
-
5
- describe '#by_calendar_month' do
6
-
7
- context 'point-in-time' do
8
- subject { Post.by_calendar_month('Feb') }
9
- its(:count){ should eq 3 }
10
- end
11
-
12
- context 'timespan' do
13
- subject { Event.by_calendar_month(1) }
14
- its(:count){ should eq 10 }
15
- end
16
-
17
- context 'timespan strict' do
18
- subject { Event.by_calendar_month(Date.parse('2014-02-01'), strict: true) }
19
- its(:count){ should eq 2 }
20
- end
21
-
22
- context 'with :year option' do
23
-
24
- context 'point-in-time' do
25
- subject { Post.by_calendar_month(12, year: 2013) }
26
- its(:count){ should eq 12 }
27
- end
28
-
29
- context 'timespan' do
30
- subject { Event.by_calendar_month('December', year: 2013) }
31
- its(:count){ should eq 13 }
32
- end
33
-
34
- context 'timespan strict' do
35
- subject { Event.by_calendar_month('Dec', year: 2013, strict: true) }
36
- its(:count){ should eq 9 }
37
- end
38
- end
39
-
40
- it 'should raise an error when given an invalid argument' do
41
- ->{ Post.by_calendar_month(0) }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
42
- ->{ Post.by_calendar_month(13) }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
43
- ->{ Post.by_calendar_month('foobar') }.should raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
44
- end
45
-
46
- it 'should be able to use an alternative field' do
47
- Event.by_calendar_month(:field => 'end_time').count.should eq 9
48
- end
49
-
50
- context ':start_day option' do
51
- subject { Post.by_calendar_month(1, :start_day => :wednesday) }
52
- its(:count){ should eq 7 }
53
- end
54
- end
55
- end
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by calendar month' do
4
+
5
+ describe '#by_calendar_month' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_calendar_month('Feb') }
9
+ it { expect(subject.count).to eql(3) }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_calendar_month(1) }
14
+ it { expect(subject.count).to eql(10) }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_calendar_month(Date.parse('2014-02-01'), strict: true) }
19
+ it { expect(subject.count).to eql(2) }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_calendar_month(12, year: 2013) }
26
+ it { expect(subject.count).to eql(12) }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_calendar_month('December', year: 2013) }
31
+ it { expect(subject.count).to eql(13) }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_calendar_month('Dec', year: 2013, strict: true) }
36
+ it { expect(subject.count).to eql(9) }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ expect{ Post.by_calendar_month(0) }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
42
+ expect{ Post.by_calendar_month(13) }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
43
+ expect{ Post.by_calendar_month('foobar') }.to raise_error(ByStar::ParseError, 'Month must be a number between 1 and 12 or a month name')
44
+ end
45
+
46
+ it 'should be able to use an alternative field' do
47
+ expect(Event.by_calendar_month(field: 'end_time').count).to eql(9)
48
+ end
49
+
50
+ context ':start_day option' do
51
+ subject { Post.by_calendar_month(1, start_day: :wednesday) }
52
+ it{ expect(subject.count).to eql(7) }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for 'by cweek' do
4
+
5
+ describe '#by_cweek' do
6
+
7
+ context 'point-in-time' do
8
+ subject { Post.by_cweek('2014-01-02') }
9
+ it { expect(subject.count).to eql(4) }
10
+ end
11
+
12
+ context 'timespan' do
13
+ subject { Event.by_cweek(1) }
14
+ it { expect(subject.count).to eql(7) }
15
+ end
16
+
17
+ context 'timespan strict' do
18
+ subject { Event.by_cweek(Date.parse('2014-01-01'), strict: true) }
19
+ it { expect(subject.count).to eql(0) }
20
+ end
21
+
22
+ context 'with :year option' do
23
+
24
+ context 'point-in-time' do
25
+ subject { Post.by_cweek(53, year: 2013) }
26
+ it { expect(subject.count).to eql(4) }
27
+ end
28
+
29
+ context 'timespan' do
30
+ subject { Event.by_cweek(53, year: 2013) }
31
+ it { expect(subject.count).to eql(7) }
32
+ end
33
+
34
+ context 'timespan strict' do
35
+ subject { Event.by_cweek(53, year: 2013, strict: true) }
36
+ it { expect(subject.count).to eql(0) }
37
+ end
38
+ end
39
+
40
+ it 'should raise an error when given an invalid argument' do
41
+ expect { Post.by_cweek(0) }.to raise_error(ByStar::ParseError, 'cweek number must be between 1 and 53')
42
+ expect { Post.by_cweek(54) }.to raise_error(ByStar::ParseError, 'cweek number must be between 1 and 53')
43
+ end
44
+
45
+ it 'should be able to use an alternative field' do
46
+ expect(Event.by_cweek(field: 'end_time').count).to eq 3
47
+ end
48
+
49
+ context ':start_day option' do
50
+ subject { Post.by_cweek('2014-01-02', start_day: :thursday) }
51
+ it { expect(subject.count).to eql(1) }
52
+ end
53
+ end
54
+ end