by_star 2.2.1 → 3.0.0

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.
Files changed (55) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +5 -5
  3. data/.travis.yml +61 -35
  4. data/CHANGELOG.md +44 -35
  5. data/Gemfile +18 -25
  6. data/MIT-LICENSE +20 -20
  7. data/README.md +577 -540
  8. data/Rakefile +18 -18
  9. data/UPGRADING +6 -12
  10. data/by_star.gemspec +34 -32
  11. data/cleaner.rb +24 -24
  12. data/lib/by_star.rb +17 -16
  13. data/lib/by_star/base.rb +68 -70
  14. data/lib/by_star/between.rb +156 -120
  15. data/lib/by_star/directional.rb +37 -33
  16. data/lib/by_star/kernel/date.rb +50 -19
  17. data/lib/by_star/kernel/time.rb +41 -41
  18. data/lib/by_star/normalization.rb +127 -118
  19. data/lib/by_star/orm/active_record/by_star.rb +61 -69
  20. data/lib/by_star/orm/mongoid/by_star.rb +76 -73
  21. data/lib/by_star/orm/mongoid/reorder.rb +23 -0
  22. data/lib/by_star/version.rb +3 -3
  23. data/spec/database.yml +15 -15
  24. data/spec/fixtures/active_record/models.rb +12 -10
  25. data/spec/fixtures/active_record/schema.rb +19 -19
  26. data/spec/fixtures/mongoid/models.rb +31 -29
  27. data/spec/fixtures/shared/seeds.rb +26 -26
  28. data/spec/gemfiles/Gemfile.master +6 -0
  29. data/spec/gemfiles/Gemfile.rails40 +7 -0
  30. data/spec/gemfiles/Gemfile.rails41 +7 -0
  31. data/spec/gemfiles/Gemfile.rails42 +7 -0
  32. data/spec/gemfiles/Gemfile.rails50 +10 -0
  33. data/spec/gemfiles/Gemfile.rails51 +10 -0
  34. data/spec/integration/active_record/active_record_spec.rb +38 -53
  35. data/spec/integration/mongoid/mongoid_spec.rb +37 -46
  36. data/spec/integration/shared/between_times.rb +82 -0
  37. data/spec/integration/shared/by_calendar_month.rb +55 -55
  38. data/spec/integration/shared/by_cweek.rb +54 -0
  39. data/spec/integration/shared/by_day.rb +96 -108
  40. data/spec/integration/shared/by_direction.rb +172 -153
  41. data/spec/integration/shared/by_fortnight.rb +48 -48
  42. data/spec/integration/shared/by_month.rb +50 -50
  43. data/spec/integration/shared/by_quarter.rb +49 -49
  44. data/spec/integration/shared/by_week.rb +54 -54
  45. data/spec/integration/shared/by_weekend.rb +49 -49
  46. data/spec/integration/shared/by_year.rb +48 -48
  47. data/spec/integration/shared/offset_parameter.rb +32 -31
  48. data/spec/integration/shared/order_parameter.rb +36 -0
  49. data/spec/integration/shared/relative.rb +174 -174
  50. data/spec/integration/shared/scope_parameter.rb +73 -72
  51. data/spec/spec_helper.rb +29 -29
  52. data/spec/unit/kernel_date_spec.rb +113 -60
  53. data/spec/unit/kernel_time_spec.rb +57 -57
  54. data/spec/unit/normalization_spec.rb +305 -255
  55. metadata +61 -62
@@ -1,26 +1,26 @@
1
- %w(2013-11-01
2
- 2013-11-30
3
- 2013-12-01
4
- 2013-12-05
5
- 2013-12-08
6
- 2013-12-16
7
- 2013-12-22
8
- 2013-12-25
9
- 2013-12-28
10
- 2013-12-31
11
- 2014-01-01
12
- 2014-01-01
13
- 2014-01-05
14
- 2014-01-10
15
- 2014-01-12
16
- 2014-01-20
17
- 2014-02-01
18
- 2014-02-15
19
- 2014-03-01
20
- 2014-03-15
21
- 2014-04-01
22
- 2014-04-15).map{|d| Time.zone.parse(d) + 17.hours }.each do |d|
23
- Post.create!(:created_at => d, day_of_month: d.day)
24
- Appointment.create!(:created_at => d, day_of_month: d.day)
25
- Event.create!(:created_at => d, :start_time => d - 5.days, :end_time => d + 5.days, day_of_month: d.day)
26
- end
1
+ %w(2013-11-01
2
+ 2013-11-30
3
+ 2013-12-01
4
+ 2013-12-05
5
+ 2013-12-08
6
+ 2013-12-16
7
+ 2013-12-22
8
+ 2013-12-25
9
+ 2013-12-28
10
+ 2013-12-31
11
+ 2014-01-01
12
+ 2014-01-01
13
+ 2014-01-05
14
+ 2014-01-10
15
+ 2014-01-12
16
+ 2014-01-20
17
+ 2014-02-01
18
+ 2014-02-15
19
+ 2014-03-01
20
+ 2014-03-15
21
+ 2014-04-01
22
+ 2014-04-15).map{|d| Time.zone.parse(d) + 17.hours }.each_with_index do |d, index|
23
+ Post.create!(created_at: d, updated_at: d + index.days, day_of_month: d.day)
24
+ Appointment.create!(created_at: d, day_of_month: d.day)
25
+ Event.create!(created_at: d, start_time: d - 5.days, end_time: d + 5.days, day_of_month: d.day)
26
+ end
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', github: 'rails'
6
+ gem 'mongoid', github: 'mongoid'
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', '~> 4.0.0'
6
+ gem 'pg', '~> 0.11'
7
+ gem 'mongoid', '~> 4.0.0'
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', '~> 4.1.0'
6
+ gem 'pg', '~> 0.11'
7
+ gem 'mongoid', '~> 4.0.0'
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'mongoid', '~> 4.0.0'
6
+ gem 'pg', '~> 0.15'
7
+ gem 'activerecord', '~> 4.2.0'
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', '~> 5.0.0'
6
+ gem 'pg', '~> 0.18'
7
+ gem 'mongoid', '~> 6.0'
8
+ # Due to weird connection issues in 2.5.0
9
+ # TODO: double check this for non 2.5.0 versions
10
+ gem 'mongo', '~> 2.4.3'
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activerecord', '~> 5.1.0'
6
+ gem 'pg', '~> 0.18'
7
+ gem 'mongoid', '~> 6.0'
8
+ # Due to weird connection issues in 2.5.0
9
+ # TODO: double check this for non 2.5.0 versions
10
+ gem 'mongo', '~> 2.4.3'
@@ -1,53 +1,38 @@
1
- require 'spec_helper'
2
- Dir[File.dirname(__FILE__) + '/../shared/*.rb'].each {|file| require file }
3
-
4
- describe ActiveRecord do
5
- before(:all) do
6
- ActiveRecord::Base.default_timezone = :utc
7
- # ActiveRecord::Base.logger = Logger.new(STDOUT)
8
-
9
- db_config = YAML::load_file(File.dirname(__FILE__) + '/../../database.yml')
10
- if db_config.has_key?('sqlite') && db_config['sqlite'].has_key?('database')
11
- db_config['sqlite']['database'] = File.dirname(__FILE__) + '/../../tmp/' + db_config['sqlite']['database']
12
- end
13
-
14
- ActiveRecord::Base.configurations = db_config
15
- ActiveRecord::Base.establish_connection(ENV['DB'] || 'sqlite')
16
- load File.dirname(__FILE__) + '/../../fixtures/active_record/schema.rb'
17
- load File.dirname(__FILE__) + '/../../fixtures/active_record/models.rb'
18
- load File.dirname(__FILE__) + '/../../fixtures/shared/seeds.rb'
19
-
20
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/../../tmp/activerecord.log')
21
- end
22
-
23
- it_behaves_like 'by day'
24
- it_behaves_like 'by direction'
25
- it_behaves_like 'by fortnight'
26
- it_behaves_like 'by month'
27
- it_behaves_like 'by calendar month'
28
- it_behaves_like 'by quarter'
29
- it_behaves_like 'by week'
30
- it_behaves_like 'by weekend'
31
- it_behaves_like 'by year'
32
- it_behaves_like 'relative'
33
- it_behaves_like 'offset parameter'
34
- it_behaves_like 'scope parameter'
35
-
36
- it 'should be able to order the result set' do
37
- scope = Post.by_year(Time.zone.now.year, :order => 'created_at DESC')
38
- scope.order_values.should == ['created_at DESC']
39
- end
40
-
41
- describe '#between_times' do
42
- subject { Post.between_times(Time.parse('2014-01-01'), Time.parse('2014-01-06')) }
43
- it { should be_a(ActiveRecord::Relation) }
44
- its(:count) { should eq 3 }
45
- end
46
-
47
- describe '#between' do
48
- subject { Post.between(Time.parse('2014-01-01'), Time.parse('2014-01-06')) }
49
- it 'should be an alias of #between_times' do
50
- subject.count.should eq 3
51
- end
52
- end
53
- end if testing_active_record?
1
+ require 'spec_helper'
2
+ Dir[File.dirname(__FILE__) + '/../shared/*.rb'].each {|file| require file }
3
+
4
+ describe ActiveRecord do
5
+ before(:all) do
6
+ ActiveRecord::Base.default_timezone = :utc
7
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
8
+
9
+ db_config = YAML::load_file(File.dirname(__FILE__) + '/../../database.yml')
10
+ if db_config.has_key?('sqlite') && db_config['sqlite'].has_key?('database')
11
+ db_config['sqlite']['database'] = File.dirname(__FILE__) + '/../../tmp/' + db_config['sqlite']['database']
12
+ end
13
+
14
+ ActiveRecord::Base.configurations = db_config
15
+ ActiveRecord::Base.establish_connection(ENV['DB'].try(:to_sym) || :sqlite)
16
+ load File.dirname(__FILE__) + '/../../fixtures/active_record/schema.rb'
17
+ load File.dirname(__FILE__) + '/../../fixtures/active_record/models.rb'
18
+ load File.dirname(__FILE__) + '/../../fixtures/shared/seeds.rb'
19
+
20
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/../../tmp/activerecord.log')
21
+ end
22
+
23
+ it_behaves_like 'between_times'
24
+ it_behaves_like 'offset parameter'
25
+ it_behaves_like 'order parameter'
26
+ it_behaves_like 'scope parameter'
27
+ it_behaves_like 'by day'
28
+ it_behaves_like 'by direction'
29
+ it_behaves_like 'by fortnight'
30
+ it_behaves_like 'by month'
31
+ it_behaves_like 'by calendar month'
32
+ it_behaves_like 'by quarter'
33
+ it_behaves_like 'by week'
34
+ it_behaves_like 'by cweek'
35
+ it_behaves_like 'by weekend'
36
+ it_behaves_like 'by year'
37
+ it_behaves_like 'relative'
38
+ end if testing_active_record?
@@ -1,46 +1,37 @@
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 'offset parameter'
24
+ it_behaves_like 'order parameter'
25
+ it_behaves_like 'scope parameter'
26
+ it_behaves_like 'by day'
27
+ it_behaves_like 'by direction'
28
+ it_behaves_like 'by fortnight'
29
+ it_behaves_like 'by month'
30
+ it_behaves_like 'by calendar month'
31
+ it_behaves_like 'by quarter'
32
+ it_behaves_like 'by week'
33
+ it_behaves_like 'by cweek'
34
+ it_behaves_like 'by weekend'
35
+ it_behaves_like 'by year'
36
+ it_behaves_like 'relative'
37
+ end if testing_mongoid?
@@ -0,0 +1,82 @@
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(9) }
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(22) }
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(9) }
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(22) }
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ if testing_mongoid?
75
+ describe '#between' do
76
+ it 'should not override Mongoid between method' do
77
+ posts = Post.between(created_at: Time.zone.parse('2014-01-01')..Time.zone.parse('2014-01-06'))
78
+ expect(posts.count).to eql(3)
79
+ end
80
+ end
81
+ end
82
+ 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