by_star 2.2.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.gitignore +5 -5
- data/.travis.yml +61 -35
- data/CHANGELOG.md +44 -35
- data/Gemfile +18 -25
- data/MIT-LICENSE +20 -20
- data/README.md +577 -540
- data/Rakefile +18 -18
- data/UPGRADING +6 -12
- data/by_star.gemspec +34 -32
- data/cleaner.rb +24 -24
- data/lib/by_star.rb +17 -16
- data/lib/by_star/base.rb +68 -70
- data/lib/by_star/between.rb +156 -120
- data/lib/by_star/directional.rb +37 -33
- data/lib/by_star/kernel/date.rb +50 -19
- data/lib/by_star/kernel/time.rb +41 -41
- data/lib/by_star/normalization.rb +127 -118
- data/lib/by_star/orm/active_record/by_star.rb +61 -69
- data/lib/by_star/orm/mongoid/by_star.rb +76 -73
- data/lib/by_star/orm/mongoid/reorder.rb +23 -0
- data/lib/by_star/version.rb +3 -3
- data/spec/database.yml +15 -15
- data/spec/fixtures/active_record/models.rb +12 -10
- data/spec/fixtures/active_record/schema.rb +19 -19
- data/spec/fixtures/mongoid/models.rb +31 -29
- data/spec/fixtures/shared/seeds.rb +26 -26
- data/spec/gemfiles/Gemfile.master +6 -0
- data/spec/gemfiles/Gemfile.rails40 +7 -0
- data/spec/gemfiles/Gemfile.rails41 +7 -0
- data/spec/gemfiles/Gemfile.rails42 +7 -0
- data/spec/gemfiles/Gemfile.rails50 +10 -0
- data/spec/gemfiles/Gemfile.rails51 +10 -0
- data/spec/integration/active_record/active_record_spec.rb +38 -53
- data/spec/integration/mongoid/mongoid_spec.rb +37 -46
- data/spec/integration/shared/between_times.rb +82 -0
- data/spec/integration/shared/by_calendar_month.rb +55 -55
- data/spec/integration/shared/by_cweek.rb +54 -0
- data/spec/integration/shared/by_day.rb +96 -108
- data/spec/integration/shared/by_direction.rb +172 -153
- data/spec/integration/shared/by_fortnight.rb +48 -48
- data/spec/integration/shared/by_month.rb +50 -50
- data/spec/integration/shared/by_quarter.rb +49 -49
- data/spec/integration/shared/by_week.rb +54 -54
- data/spec/integration/shared/by_weekend.rb +49 -49
- data/spec/integration/shared/by_year.rb +48 -48
- data/spec/integration/shared/offset_parameter.rb +32 -31
- data/spec/integration/shared/order_parameter.rb +36 -0
- data/spec/integration/shared/relative.rb +174 -174
- data/spec/integration/shared/scope_parameter.rb +73 -72
- data/spec/spec_helper.rb +29 -29
- data/spec/unit/kernel_date_spec.rb +113 -60
- data/spec/unit/kernel_time_spec.rb +57 -57
- data/spec/unit/normalization_spec.rb +305 -255
- 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 }.
|
23
|
-
Post.create!(:
|
24
|
-
Appointment.create!(:
|
25
|
-
Event.create!(:
|
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
|
@@ -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'] ||
|
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 '
|
24
|
-
it_behaves_like '
|
25
|
-
it_behaves_like '
|
26
|
-
it_behaves_like '
|
27
|
-
it_behaves_like 'by
|
28
|
-
it_behaves_like 'by
|
29
|
-
it_behaves_like 'by
|
30
|
-
it_behaves_like 'by
|
31
|
-
it_behaves_like 'by
|
32
|
-
it_behaves_like '
|
33
|
-
it_behaves_like '
|
34
|
-
it_behaves_like '
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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 '
|
23
|
-
it_behaves_like '
|
24
|
-
it_behaves_like '
|
25
|
-
it_behaves_like '
|
26
|
-
it_behaves_like 'by
|
27
|
-
it_behaves_like 'by
|
28
|
-
it_behaves_like 'by
|
29
|
-
it_behaves_like 'by
|
30
|
-
it_behaves_like 'by
|
31
|
-
it_behaves_like '
|
32
|
-
it_behaves_like '
|
33
|
-
it_behaves_like '
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'timespan' do
|
13
|
-
subject { Event.by_calendar_month(1) }
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'timespan strict' do
|
18
|
-
subject { Event.by_calendar_month(Date.parse('2014-02-01'), strict: true) }
|
19
|
-
|
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
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'timespan' do
|
30
|
-
subject { Event.by_calendar_month('December', year: 2013) }
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'timespan strict' do
|
35
|
-
subject { Event.by_calendar_month('Dec', year: 2013, strict: true) }
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'should raise an error when given an invalid argument' do
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should be able to use an alternative field' do
|
47
|
-
Event.by_calendar_month(:
|
48
|
-
end
|
49
|
-
|
50
|
-
context ':start_day option' do
|
51
|
-
subject { Post.by_calendar_month(1, :
|
52
|
-
|
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
|