ta_by_star 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +59 -0
- data/Gemfile +18 -0
- data/MIT-LICENSE +20 -0
- data/README.md +616 -0
- data/Rakefile +18 -0
- data/UPGRADING +4 -0
- data/by_star.gemspec +34 -0
- data/cleaner.rb +25 -0
- data/lib/by_star/base.rb +76 -0
- data/lib/by_star/between.rb +190 -0
- data/lib/by_star/directional.rb +35 -0
- data/lib/by_star/kernel/date.rb +41 -0
- data/lib/by_star/kernel/in_time_zone.rb +20 -0
- data/lib/by_star/kernel/time.rb +41 -0
- data/lib/by_star/normalization.rb +156 -0
- data/lib/by_star/orm/active_record/by_star.rb +75 -0
- data/lib/by_star/orm/mongoid/by_star.rb +90 -0
- data/lib/by_star/orm/mongoid/reorder.rb +23 -0
- data/lib/by_star/version.rb +3 -0
- data/lib/by_star.rb +18 -0
- data/spec/database.yml +15 -0
- data/spec/fixtures/active_record/models.rb +12 -0
- data/spec/fixtures/active_record/schema.rb +19 -0
- data/spec/fixtures/mongoid/models.rb +31 -0
- data/spec/fixtures/shared/seeds.rb +36 -0
- data/spec/gemfiles/Gemfile.rails +5 -0
- data/spec/gemfiles/Gemfile.rails32 +7 -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 +7 -0
- data/spec/gemfiles/Gemfile.rails51 +7 -0
- data/spec/gemfiles/Gemfile.rails52 +7 -0
- data/spec/gemfiles/Gemfile.rails60 +7 -0
- data/spec/gemfiles/Gemfile.rails61 +7 -0
- data/spec/integration/active_record/active_record_spec.rb +41 -0
- data/spec/integration/mongoid/mongoid_spec.rb +39 -0
- data/spec/integration/shared/at_time.rb +53 -0
- data/spec/integration/shared/between_dates.rb +99 -0
- data/spec/integration/shared/between_times.rb +99 -0
- data/spec/integration/shared/by_calendar_month.rb +55 -0
- data/spec/integration/shared/by_cweek.rb +54 -0
- data/spec/integration/shared/by_day.rb +120 -0
- data/spec/integration/shared/by_direction.rb +126 -0
- data/spec/integration/shared/by_fortnight.rb +48 -0
- data/spec/integration/shared/by_month.rb +50 -0
- data/spec/integration/shared/by_quarter.rb +49 -0
- data/spec/integration/shared/by_week.rb +54 -0
- data/spec/integration/shared/by_weekend.rb +49 -0
- data/spec/integration/shared/by_year.rb +48 -0
- data/spec/integration/shared/index_scope_parameter.rb +111 -0
- data/spec/integration/shared/offset_parameter.rb +32 -0
- data/spec/integration/shared/order_parameter.rb +36 -0
- data/spec/integration/shared/relative.rb +174 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/unit/kernel_date_spec.rb +113 -0
- data/spec/unit/kernel_time_spec.rb +57 -0
- data/spec/unit/normalization_spec.rb +384 -0
- data/tmp/.gitignore +1 -0
- metadata +298 -0
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'index_scope parameter' do
|
4
|
+
|
5
|
+
describe ':scope' do
|
6
|
+
|
7
|
+
it 'should memoize the scope variable' do
|
8
|
+
expect(Event.instance_variable_get(:@by_star_index_scope)).to be_nil
|
9
|
+
expect(Post.instance_variable_get(:@by_star_index_scope)).to be_nil
|
10
|
+
expect(Appointment.instance_variable_get(:@by_star_index_scope)).to be_a Proc
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'between_times with index_scope' do
|
14
|
+
|
15
|
+
context 'nil' do
|
16
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: nil) }
|
17
|
+
it { expect(subject.count).to eql(16) }
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'false' do
|
21
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: false) }
|
22
|
+
it { expect(subject.count).to eql(16) }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'Time' do
|
26
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Time.zone.parse('2013-11-30 17:00:00')) }
|
27
|
+
it { expect(subject.count).to eql(14) }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'DateTime' do
|
31
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Time.zone.parse('2013-11-30 17:00:00').to_datetime) }
|
32
|
+
it { expect(subject.count).to eql(14) }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'Date' do
|
36
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Date.parse('2013-11-30')) }
|
37
|
+
it { expect(subject.count).to eql(14) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'ActiveSupport::Duration' do
|
41
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: 3.hours) }
|
42
|
+
it { expect(subject.count).to eql(13) }
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'Numeric' do
|
46
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: 3600) }
|
47
|
+
it { expect(subject.count).to eql(13) }
|
48
|
+
end
|
49
|
+
|
50
|
+
context ':beginning_of_day' do
|
51
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: :beginning_of_day) }
|
52
|
+
it { expect(subject.count).to eql(13) }
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'unsupported type' do
|
56
|
+
subject { Event.between_times(Date.parse('2013-12-01'), Date.parse('2014-01-31'), index_scope: Integer) }
|
57
|
+
it { expect{subject.count}.to raise_error(RuntimeError) }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'at_time with index_scope' do
|
62
|
+
|
63
|
+
context 'nil' do
|
64
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: nil) }
|
65
|
+
it { expect(subject.count).to eql(3) }
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'false' do
|
69
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: false) }
|
70
|
+
it { expect(subject.count).to eql(3) }
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'Time' do
|
74
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Time.zone.parse('2013-10-30 17:00:00')) }
|
75
|
+
it { expect(subject.count).to eql(3) }
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'DateTime' do
|
79
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Time.zone.parse('2013-11-30 17:00:00').to_datetime) }
|
80
|
+
it { expect(subject.count).to eql(1) }
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'Date' do
|
84
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Date.parse('2013-11-30')) }
|
85
|
+
it { expect(subject.count).to eql(1) }
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'ActiveSupport::Duration' do
|
89
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: 100.hours) }
|
90
|
+
it { expect(subject.count).to eql(1) }
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'Numeric' do
|
94
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: 60 * 60 * 1000) }
|
95
|
+
it { expect(subject.count).to eql(3) }
|
96
|
+
end
|
97
|
+
|
98
|
+
context ':beginning_of_day' do
|
99
|
+
let!(:custom_event){ t = Time.zone.parse('2013-12-30 17:00'); Event.create!(start_time: t - 1.hour, end_time: t + 1.hour) }
|
100
|
+
subject { Event.at_time(Time.zone.parse('2013-12-30 16:00'), index_scope: :beginning_of_day) }
|
101
|
+
it { expect(subject.count).to eql(1) }
|
102
|
+
after { custom_event.delete }
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'unsupported type' do
|
106
|
+
subject { Event.at_time(Time.zone.parse('2013-12-01 14:00'), index_scope: Integer) }
|
107
|
+
it { expect{subject.count}.to raise_error(RuntimeError) }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'offset parameter' do
|
4
|
+
|
5
|
+
describe ':offset' do
|
6
|
+
|
7
|
+
it 'should memoize the offset variable' do
|
8
|
+
expect(Event.instance_variable_get(:@by_star_offset)).to eq 3.hours
|
9
|
+
expect(Post.instance_variable_get(:@by_star_offset)).to be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'between_times with default offset' do
|
13
|
+
subject { Event.between_times(Time.zone.parse('2014-01-01'), Time.zone.parse('2014-01-10')) }
|
14
|
+
it { expect(subject.count).to eql(7) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'between_times with offset override' do
|
18
|
+
subject { Event.between_times(Time.zone.parse('2014-01-01')..Time.zone.parse('2014-01-10'), offset: 16.hours) }
|
19
|
+
it { expect(subject.count).to eql(7) }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'by_day with default offset' do
|
23
|
+
subject { Event.by_day(Time.zone.parse('2014-01-01')) }
|
24
|
+
it { expect(subject.count).to eql(5) }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'by_day with offset override' do
|
28
|
+
subject { Event.by_day(Time.zone.parse('2014-12-26'), field: :start_time, offset: 5.hours) }
|
29
|
+
it { expect(subject.count).to eql(0) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'order parameter' do
|
4
|
+
|
5
|
+
describe ':order' do
|
6
|
+
|
7
|
+
if testing_active_record?
|
8
|
+
|
9
|
+
it 'should be able to order the result set asc' do
|
10
|
+
scope = Post.by_year(Time.zone.now.year, order: 'created_at ASC')
|
11
|
+
expect(scope.order_values).to eq ['created_at ASC']
|
12
|
+
expect(scope.first.created_at).to eq Time.zone.parse('2014-01-01 17:00:00')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should be able to order the result set desc' do
|
16
|
+
scope = Post.by_year(Time.zone.now.year, order: 'created_at DESC')
|
17
|
+
expect(scope.order_values).to eq ['created_at DESC']
|
18
|
+
expect(scope.first.created_at).to eq Time.zone.parse('2014-04-15 17:00:00')
|
19
|
+
end
|
20
|
+
|
21
|
+
elsif testing_mongoid?
|
22
|
+
|
23
|
+
it 'should be able to order the result set asc' do
|
24
|
+
scope = Post.by_year(Time.zone.now.year, order: {created_at: :asc})
|
25
|
+
expect(scope.options[:sort]).to eq({'created_at' => 1})
|
26
|
+
expect(scope.first.created_at).to eq Time.zone.parse('2014-01-01 17:00:00')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should be able to order the result set desc' do
|
30
|
+
scope = Post.by_year(Time.zone.now.year, order: {created_at: :desc})
|
31
|
+
expect(scope.options[:sort]).to eq({'created_at' => -1})
|
32
|
+
expect(scope.first.created_at).to eq Time.zone.parse('2014-04-15 17:00:00')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'relative' do
|
4
|
+
|
5
|
+
describe '#past_day' do
|
6
|
+
context 'point-in-time' do
|
7
|
+
subject { Post.past_day }
|
8
|
+
it { expect(subject.count).to eql(1) }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'timespan' do
|
12
|
+
subject { Event.past_day }
|
13
|
+
it { expect(subject.count).to eql(5) }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'timespan strict' do
|
17
|
+
subject { Event.past_day(strict: true) }
|
18
|
+
it { expect(subject.count).to eql(0) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#past_week' do
|
23
|
+
context 'point-in-time' do
|
24
|
+
subject { Post.past_week }
|
25
|
+
it { expect(subject.count).to eql(3) }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'timespan' do
|
29
|
+
subject { Event.past_week }
|
30
|
+
it { expect(subject.count).to eql(7) }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'timespan strict' do
|
34
|
+
subject { Event.past_week(strict: true) }
|
35
|
+
it { expect(subject.count).to eql(0) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#past_fortnight' do
|
40
|
+
context 'point-in-time' do
|
41
|
+
subject { Post.past_fortnight }
|
42
|
+
it { expect(subject.count).to eql(4) }
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'timespan' do
|
46
|
+
subject { Event.past_fortnight }
|
47
|
+
it { expect(subject.count).to eql(8) }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'timespan strict' do
|
51
|
+
subject { Event.past_fortnight(strict: true) }
|
52
|
+
it { expect(subject.count).to eql(1) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#past_month' do
|
57
|
+
context 'point-in-time' do
|
58
|
+
subject { Post.past_month }
|
59
|
+
it { expect(subject.count).to eql(8) }
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'timespan' do
|
63
|
+
subject { Event.past_month }
|
64
|
+
it { expect(subject.count).to eql(12) }
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'timespan strict' do
|
68
|
+
subject { Event.past_month(strict: true) }
|
69
|
+
it { expect(subject.count).to eql(4) }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#past_year' do
|
74
|
+
context 'point-in-time' do
|
75
|
+
subject { Post.past_year }
|
76
|
+
it { expect(subject.count).to eql(10) }
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'timespan' do
|
80
|
+
subject { Event.past_year }
|
81
|
+
it { expect(subject.count).to eql(13) }
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'timespan strict' do
|
85
|
+
subject { Event.past_year(strict: true) }
|
86
|
+
it { expect(subject.count).to eql(8) }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#next_day' do
|
91
|
+
context 'point-in-time' do
|
92
|
+
subject { Post.next_day }
|
93
|
+
it { expect(subject.count).to eql(2) }
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'timespan' do
|
97
|
+
subject { Event.next_day }
|
98
|
+
it { expect(subject.count).to eql(5) }
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'timespan strict' do
|
102
|
+
subject { Event.next_day(strict: true) }
|
103
|
+
it { expect(subject.count).to eql(0) }
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#next_week' do
|
108
|
+
context 'point-in-time' do
|
109
|
+
subject { Post.next_week }
|
110
|
+
it { expect(subject.count).to eql(3) }
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'timespan' do
|
114
|
+
subject { Event.next_week }
|
115
|
+
it { expect(subject.count).to eql(7) }
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'timespan strict' do
|
119
|
+
subject { Event.next_week(strict: true) }
|
120
|
+
it { expect(subject.count).to eql(0) }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe '#next_fortnight' do
|
125
|
+
context 'point-in-time' do
|
126
|
+
subject { Post.next_fortnight }
|
127
|
+
it { expect(subject.count).to eql(5) }
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'timespan' do
|
131
|
+
subject { Event.next_fortnight }
|
132
|
+
it { expect(subject.count).to eql(7) }
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'timespan strict' do
|
136
|
+
subject { Event.next_fortnight(strict: true) }
|
137
|
+
it { expect(subject.count).to eql(0) }
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe '#next_month' do
|
142
|
+
context 'point-in-time' do
|
143
|
+
subject { Post.next_month }
|
144
|
+
it { expect(subject.count).to eql(6) }
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'timespan' do
|
148
|
+
subject { Event.next_month }
|
149
|
+
it { expect(subject.count).to eql(9) }
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'timespan strict' do
|
153
|
+
subject { Event.next_month(strict: true) }
|
154
|
+
it { expect(subject.count).to eql(3) }
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe '#next_year' do
|
159
|
+
context 'point-in-time' do
|
160
|
+
subject { Post.next_year }
|
161
|
+
it { expect(subject.count).to eql(12) }
|
162
|
+
end
|
163
|
+
|
164
|
+
context 'timespan' do
|
165
|
+
subject { Event.next_year }
|
166
|
+
it { expect(subject.count).to eql(14) }
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'timespan strict' do
|
170
|
+
subject { Event.next_year(strict: true) }
|
171
|
+
it { expect(subject.count).to eql(9) }
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup
|
4
|
+
require 'fileutils'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
FileUtils.mkdir_p(File.dirname(__FILE__) + "/tmp")
|
8
|
+
$:.unshift(File.join(File.dirname(__FILE__), "../lib"))
|
9
|
+
|
10
|
+
require 'active_record'
|
11
|
+
require 'mongoid'
|
12
|
+
require 'chronic'
|
13
|
+
require 'timecop'
|
14
|
+
require 'by_star'
|
15
|
+
|
16
|
+
# Specs should pass regardless of timezone
|
17
|
+
Time.zone = %w(Asia/Tokyo America/New_York Australia/Sydney UTC).sample
|
18
|
+
puts "Running specs in #{Time.zone} timezone..."
|
19
|
+
|
20
|
+
# Set Rails time to 2014-01-01 00:00:00
|
21
|
+
Timecop.travel(Time.zone.local(2014))
|
22
|
+
|
23
|
+
RSpec.configure do |c|
|
24
|
+
c.filter_run_excluding sydney: true unless Time.zone.name == 'Australia/Sydney'
|
25
|
+
end
|
26
|
+
|
27
|
+
def testing_mongoid?
|
28
|
+
ENV['DB'] == 'mongodb'
|
29
|
+
end
|
30
|
+
|
31
|
+
def testing_active_record?
|
32
|
+
!testing_mongoid?
|
33
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Date do
|
4
|
+
|
5
|
+
describe '#in_time_zone' do
|
6
|
+
|
7
|
+
subject { Date.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
stub_const('Date', Class.new)
|
11
|
+
allow_any_instance_of(Date).to receive(:to_time_in_current_zone)
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when #in_time_zone is already defined' do
|
15
|
+
before do
|
16
|
+
expect_any_instance_of(Date).to receive(:in_time_zone)
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when ByStar::Kernel::InTimeZone included' do
|
20
|
+
before do
|
21
|
+
::Date.__send__(:include, ByStar::Kernel::InTimeZone)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should not be aliased to #to_time_in_current_zone' do
|
25
|
+
expect(subject).not_to receive(:to_time_in_current_zone)
|
26
|
+
subject.in_time_zone
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when ByStar::Kernel::InTimeZone not included' do
|
31
|
+
|
32
|
+
it 'should not be aliased to #to_time_in_current_zone' do
|
33
|
+
expect(subject).not_to receive(:to_time_in_current_zone)
|
34
|
+
subject.in_time_zone
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'when #in_time_zone is not defined' do
|
40
|
+
|
41
|
+
context 'when ByStar::Kernel::InTimeZone included' do
|
42
|
+
before do
|
43
|
+
::Date.__send__(:include, ByStar::Kernel::InTimeZone)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should be aliased to #to_time_in_current_zone' do
|
47
|
+
expect(subject).to receive(:to_time_in_current_zone)
|
48
|
+
subject.in_time_zone
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when ByStar::Kernel::InTimeZone not included' do
|
53
|
+
|
54
|
+
it 'should raise NoMethodError' do
|
55
|
+
expect{ subject.in_time_zone }.to raise_error(NoMethodError)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'weekend' do
|
62
|
+
|
63
|
+
(0..6).each do |n|
|
64
|
+
context "Monday plus #{n} days" do
|
65
|
+
subject { Date.parse('2014-01-06') + n.days }
|
66
|
+
it { expect(subject.beginning_of_weekend).to eq Date.parse('2014-01-11') }
|
67
|
+
it { expect(subject.end_of_weekend).to eq Date.parse('2014-01-12') }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'fortnight' do
|
73
|
+
|
74
|
+
context 'first day of year' do
|
75
|
+
subject { Date.parse '2014-01-01' }
|
76
|
+
it { expect(subject.beginning_of_fortnight).to eq Date.parse('2014-01-01') }
|
77
|
+
it { expect(subject.end_of_fortnight).to eq Date.parse('2014-01-14') }
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'second fortnight of year' do
|
81
|
+
subject { Date.parse '2014-01-16' }
|
82
|
+
it { expect(subject.beginning_of_fortnight).to eq Date.parse('2014-01-15') }
|
83
|
+
it { expect(subject.end_of_fortnight).to eq Date.parse('2014-01-28') }
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'middle of year' do
|
87
|
+
subject { Date.parse '2014-06-13' }
|
88
|
+
it { expect(subject.beginning_of_fortnight).to eq Date.parse('2014-06-04') }
|
89
|
+
it { expect(subject.end_of_fortnight).to eq Date.parse('2014-06-17') }
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'last day of year' do
|
93
|
+
subject { Date.parse '2014-12-31' }
|
94
|
+
it { expect(subject.beginning_of_fortnight).to eq Date.parse('2014-12-31') }
|
95
|
+
it { expect(subject.end_of_fortnight).to eq Date.parse('2015-01-13') }
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe 'calendar_month' do
|
100
|
+
|
101
|
+
subject { Date.parse '2014-01-01' }
|
102
|
+
|
103
|
+
context 'week begins Monday' do
|
104
|
+
it { expect(subject.beginning_of_calendar_month).to eq Date.parse('2013-12-30') }
|
105
|
+
it { expect(subject.end_of_calendar_month).to eq Date.parse('2014-02-02') }
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'week begins Sunday' do
|
109
|
+
it { expect(subject.beginning_of_calendar_month(:sunday)).to eq Date.parse('2013-12-29') }
|
110
|
+
it { expect(subject.end_of_calendar_month(:sunday)).to eq Date.parse('2014-02-01') }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Time do
|
4
|
+
|
5
|
+
describe 'weekend' do
|
6
|
+
|
7
|
+
(0..6).each do |n|
|
8
|
+
context "Monday plus #{n} days" do
|
9
|
+
subject { Time.zone.parse('2014-01-06') + n.days }
|
10
|
+
it { expect(subject.beginning_of_weekend).to eq Time.zone.parse('2014-01-11') }
|
11
|
+
it { expect(subject.end_of_weekend).to eq Time.zone.parse('2014-01-12').end_of_day }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'fortnight' do
|
17
|
+
|
18
|
+
context 'first day of year' do
|
19
|
+
subject { Time.zone.parse '2014-01-01' }
|
20
|
+
it { expect(subject.beginning_of_fortnight).to eq Time.zone.parse('2014-01-01') }
|
21
|
+
it { expect(subject.end_of_fortnight).to eq Time.zone.parse('2014-01-14').end_of_day }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'second fortnight of year' do
|
25
|
+
subject { Time.zone.parse '2014-01-16' }
|
26
|
+
it { expect(subject.beginning_of_fortnight).to eq Time.zone.parse('2014-01-15') }
|
27
|
+
it { expect(subject.end_of_fortnight).to eq Time.zone.parse('2014-01-28').end_of_day }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'middle of year' do
|
31
|
+
subject { Time.zone.parse '2014-06-13' }
|
32
|
+
it { expect(subject.beginning_of_fortnight).to eq Time.zone.parse('2014-06-04') }
|
33
|
+
it { expect(subject.end_of_fortnight).to eq Time.zone.parse('2014-06-17').end_of_day }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'last day of year' do
|
37
|
+
subject { Time.zone.parse '2014-12-31' }
|
38
|
+
it { expect(subject.beginning_of_fortnight).to eq Time.zone.parse('2014-12-31') }
|
39
|
+
it { expect(subject.end_of_fortnight).to eq Time.zone.parse('2015-01-13').end_of_day }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'calendar_month' do
|
44
|
+
|
45
|
+
subject { Time.zone.parse '2014-01-01' }
|
46
|
+
|
47
|
+
context 'week begins Monday' do
|
48
|
+
it { expect(subject.beginning_of_calendar_month).to eq Time.zone.parse('2013-12-30') }
|
49
|
+
it { expect(subject.end_of_calendar_month).to eq Time.zone.parse('2014-02-02').end_of_day }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'week begins Sunday' do
|
53
|
+
it { expect(subject.beginning_of_calendar_month(:sunday)).to eq Time.zone.parse('2013-12-29') }
|
54
|
+
it { expect(subject.end_of_calendar_month(:sunday)).to eq Time.zone.parse('2014-02-01').end_of_day }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|