timely 0.7.0 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23be3863188d0d617f9cb4c6d7aca28605d52e9e3a62cadb2935253395210f1c
4
- data.tar.gz: 5b161da1238c424053b255cfb96e28dd3c9bd52e3846f1aa2ee602ccad52371d
3
+ metadata.gz: f77d2dfe978ebc2b98ce38619976758ffd6bf58cbe5ca72e04f74de5e7518a2b
4
+ data.tar.gz: 4474ac4c99ed979e16692e919cf0ef53ab9f02955c0eab8471bbc238633c7132
5
5
  SHA512:
6
- metadata.gz: 97e1202d4c04236c82e445c1cab715a96b68f862370d89eefb542789d621e7cf2249389a47d2a195df412fc328c97ef1e35ec5d77532549d3ec6152d77e47d5a
7
- data.tar.gz: 5de7656ed5ecb847307412497c7149696d7e349120c933a3749ebffb9462e9bccc08573ddc62fd039f650d7d6993ef998274ce783ad75215eb12493fa31eeac0
6
+ metadata.gz: 26ea6163458e19c7971828725d620c320fff0bfca048ce22083f5582465e26ac6211191fc2a3dab02b9bd9f35268b823dfe4e6e3f824b82f7cc1da9d5b987ada
7
+ data.tar.gz: 7257fb9a2e598833115f7b3edf49c6f1c7f980ec45137c21f7cc1f74fd22639401151990dd48b63cf2e0d9500dfb1438e74b972590d17ce1bf6182c7bca7df58
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.8.0
4
+
5
+ * [TT-6441] Turns out we don't actually need time difference in QT
6
+ * [TT-6661] Fix issue when detecting intersecting date groups
7
+
3
8
  ## 0.7.0
4
9
 
5
10
  * [TT-6441] Due to TimeDifference being unmaintained bring it into the timely library
@@ -12,4 +12,3 @@ require 'timely/rails/date_time'
12
12
  require 'timely/rails/date'
13
13
  require 'timely/rails/period'
14
14
  require 'timely/rails/time'
15
- require 'timely/rails/time_difference'
@@ -18,9 +18,7 @@ module Timely
18
18
  scope :within_range, lambda { |date_range|
19
19
  # IMPORTANT: Required for correctness in case of string param.
20
20
  dates = Array(date_range)
21
- scope = covering_date(dates.first)
22
- scope = scope.or(covering_date(dates.last)) if dates.first != dates.last
23
- scope
21
+ where(arel_table[:start_date].lteq(dates.last)).where(arel_table[:end_date].gteq(dates.first))
24
22
  }
25
23
 
26
24
  scope :for_any_weekdays, lambda { |weekdays_int|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Timely
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
5
5
  end
@@ -33,6 +33,44 @@ RSpec.describe Timely::DateGroup do
33
33
  end
34
34
  end
35
35
 
36
+ RSpec.describe Timely::DateGroup, 'Timely::DateGroup.applying_for_duration' do
37
+ let!(:date_group_a) { Timely::DateGroup.create!(
38
+ start_date: '2020-01-01', end_date: '2020-04-04', weekdays: %w[1 1 1 1 1 1 1]
39
+ ) }
40
+
41
+ let!(:date_group_b) { Timely::DateGroup.create!(
42
+ start_date: '2020-04-02', end_date: '2020-04-09', weekdays: %w[1 1 1 1 1 1 1]
43
+ ) }
44
+
45
+ subject {
46
+ Timely::DateGroup.applying_for_duration(Timely::DateRange.new(start_date, end_date)).to_a
47
+ }
48
+
49
+ context 'intersecting dates (inside first)' do
50
+ let(:start_date) { '2020-03-29'.to_date }
51
+ let(:end_date) { '2020-04-13'.to_date }
52
+ it { is_expected.to eq([date_group_a, date_group_b]) }
53
+ end
54
+
55
+ context 'intersecting dates full range' do
56
+ let(:start_date) { '2020-01-01'.to_date }
57
+ let(:end_date) { '2020-04-09'.to_date }
58
+ it { is_expected.to eq([date_group_a, date_group_b]) }
59
+ end
60
+
61
+ context 'touching end of range' do
62
+ let(:start_date) { '2020-04-09'.to_date }
63
+ let(:end_date) { '2020-04-09'.to_date }
64
+ it { is_expected.to eq([date_group_b]) }
65
+ end
66
+
67
+ context 'touching start of range' do
68
+ let(:start_date) { '2020-04-01'.to_date }
69
+ let(:end_date) { '2020-04-01'.to_date }
70
+ it { is_expected.to eq([date_group_a]) }
71
+ end
72
+ end
73
+
36
74
  RSpec.describe Timely::DateGroup, 'without weekdays' do
37
75
  subject { Timely::DateGroup.new(start_date: Date.current, end_date: Date.current) }
38
76
  it { is_expected.to be_valid }
@@ -11,6 +11,8 @@ require 'rubygems'
11
11
  require 'bundler/setup'
12
12
  require 'rspec/its'
13
13
  require 'active_record'
14
+ require 'pry'
15
+ require 'database_cleaner'
14
16
 
15
17
  require 'support/coverage_loader'
16
18
 
@@ -30,4 +32,15 @@ load('spec/schema.rb')
30
32
  RSpec.configure do |config|
31
33
  config.run_all_when_everything_filtered = true
32
34
  config.filter_run :focus
35
+
36
+ config.before(:suite) do
37
+ DatabaseCleaner.strategy = :transaction
38
+ DatabaseCleaner.clean_with(:truncation, except: %w(ar_internal_metadata))
39
+ end
40
+
41
+ config.around(:each) do |example|
42
+ DatabaseCleaner.cleaning do
43
+ example.run
44
+ end
45
+ end
33
46
  end
@@ -3,4 +3,4 @@
3
3
  require 'simplecov-rcov'
4
4
  require 'coveralls'
5
5
  require 'coverage/kit'
6
- Coverage::Kit.setup(minimum_coverage: 72.0)
6
+ Coverage::Kit.setup(minimum_coverage: 69.95)
@@ -29,10 +29,12 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency 'rake'
30
30
  spec.add_development_dependency 'rspec'
31
31
  spec.add_development_dependency 'rspec-its'
32
+ spec.add_development_dependency 'database_cleaner'
32
33
  spec.add_development_dependency 'rubocop'
33
34
  spec.add_development_dependency 'rubocop-rails'
34
35
  spec.add_development_dependency 'simplecov-rcov'
35
36
  spec.add_development_dependency 'sqlite3'
36
37
  spec.add_development_dependency 'timecop'
37
38
  spec.add_development_dependency 'travis'
39
+ spec.add_development_dependency 'pry'
38
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Noack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-27 00:00:00.000000000 Z
11
+ date: 2020-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: database_cleaner
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: rubocop
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -220,6 +234,20 @@ dependencies:
220
234
  - - ">="
221
235
  - !ruby/object:Gem::Version
222
236
  version: '0'
237
+ - !ruby/object:Gem::Dependency
238
+ name: pry
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - ">="
242
+ - !ruby/object:Gem::Version
243
+ version: '0'
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - ">="
249
+ - !ruby/object:Gem::Version
250
+ version: '0'
223
251
  description: Set of time, date, weekday related methods.
224
252
  email:
225
253
  - support@travellink.com.au
@@ -254,7 +282,6 @@ files:
254
282
  - lib/timely/rails/period.rb
255
283
  - lib/timely/rails/season.rb
256
284
  - lib/timely/rails/time.rb
257
- - lib/timely/rails/time_difference.rb
258
285
  - lib/timely/railtie.rb
259
286
  - lib/timely/range.rb
260
287
  - lib/timely/string.rb
@@ -277,7 +304,6 @@ files:
277
304
  - spec/rails/date_spec.rb
278
305
  - spec/rails/date_time_spec.rb
279
306
  - spec/rails/period_spec.rb
280
- - spec/rails/time_difference_spec.rb
281
307
  - spec/rails/time_spec.rb
282
308
  - spec/schema.rb
283
309
  - spec/season_spec.rb
@@ -322,7 +348,6 @@ test_files:
322
348
  - spec/rails/date_spec.rb
323
349
  - spec/rails/date_time_spec.rb
324
350
  - spec/rails/period_spec.rb
325
- - spec/rails/time_difference_spec.rb
326
351
  - spec/rails/time_spec.rb
327
352
  - spec/schema.rb
328
353
  - spec/season_spec.rb
@@ -1,100 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubygems'
4
- require 'active_support/all'
5
-
6
- module Timely
7
- class TimeDifference
8
- private_class_method :new
9
-
10
- TIME_COMPONENTS = %i[years months weeks days hours minutes seconds].freeze
11
-
12
- def self.between(start_time, end_time)
13
- new(start_time, end_time)
14
- end
15
-
16
- def in_years
17
- in_component(:years)
18
- end
19
-
20
- def in_months
21
- (@time_diff / (1.day * 30.42)).round(2)
22
- end
23
-
24
- def in_weeks
25
- in_component(:weeks)
26
- end
27
-
28
- def in_days
29
- in_component(:days)
30
- end
31
-
32
- def in_hours
33
- in_component(:hours)
34
- end
35
-
36
- def in_minutes
37
- in_component(:minutes)
38
- end
39
-
40
- def in_seconds
41
- @time_diff
42
- end
43
-
44
- def in_each_component
45
- Hash[TIME_COMPONENTS.map do |time_component|
46
- [time_component, public_send("in_#{time_component}")]
47
- end]
48
- end
49
-
50
- def in_general
51
- remaining = @time_diff
52
- Hash[TIME_COMPONENTS.map do |time_component|
53
- if remaining > 0
54
- rounded_time_component = (remaining / 1.send(time_component).seconds).round(2).floor
55
- remaining -= rounded_time_component.send(time_component)
56
- [time_component, rounded_time_component]
57
- else
58
- [time_component, 0]
59
- end
60
- end]
61
- end
62
-
63
- def humanize
64
- diff_parts = []
65
- in_general.each do |part, quantity|
66
- next if quantity <= 0
67
-
68
- part = part.to_s.humanize
69
-
70
- part = part.singularize if quantity <= 1
71
-
72
- diff_parts << "#{quantity} #{part}"
73
- end
74
-
75
- last_part = diff_parts.pop
76
- if diff_parts.empty?
77
- last_part
78
- else
79
- [diff_parts.join(', '), last_part].join(' and ')
80
- end
81
- end
82
-
83
- private
84
-
85
- def initialize(start_time, end_time)
86
- start_time = time_in_seconds(start_time)
87
- end_time = time_in_seconds(end_time)
88
-
89
- @time_diff = (end_time - start_time).abs
90
- end
91
-
92
- def time_in_seconds(time)
93
- time.to_time.to_f
94
- end
95
-
96
- def in_component(component)
97
- (@time_diff / 1.send(component)).round(2)
98
- end
99
- end
100
- end
@@ -1,185 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Timely::TimeDifference do
6
- def self.with_each_class(&block)
7
- classes = [Time, Date, DateTime]
8
-
9
- classes.each do |clazz|
10
- context "with a #{clazz.name} class" do
11
- instance_exec clazz, &block
12
- end
13
- end
14
- end
15
-
16
- describe '.between' do
17
- with_each_class do |clazz|
18
- it 'returns a new TimeDifference instance in each component' do
19
- start_time = clazz.new(2011, 1)
20
- end_time = clazz.new(2011, 12)
21
-
22
- expect(Timely::TimeDifference.between(start_time, end_time)).to be_a(Timely::TimeDifference)
23
- end
24
- end
25
- end
26
-
27
- describe '#in_each_component' do
28
- with_each_class do |clazz|
29
- it 'returns time difference in each component' do
30
- start_time = clazz.new(2011, 1)
31
- end_time = clazz.new(2011, 12)
32
-
33
- expect(Timely::TimeDifference.between(start_time, end_time).in_each_component).to eql(years: 0.91, months: 10.98, weeks: 47.71, days: 334.0, hours: 8016.0, minutes: 480_960.0, seconds: 28_857_600.0)
34
- end
35
- end
36
- end
37
-
38
- describe '#in_general' do
39
- with_each_class do |clazz|
40
- it 'returns time difference in general that matches the total seconds' do
41
- start_time = clazz.new(2009, 11)
42
- end_time = clazz.new(2011, 1)
43
-
44
- expect(Timely::TimeDifference.between(start_time, end_time).in_general).to eql(years: 1, months: 2, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0)
45
- end
46
- end
47
- end
48
-
49
- describe '#humanize' do
50
- with_each_class do |clazz|
51
- it 'returns a string representing the time difference from in_general' do
52
- start_time = clazz.new(2009, 11)
53
- end_time = clazz.new(2011, 1)
54
-
55
- expect(Timely::TimeDifference.between(start_time, end_time).humanize).to eql('1 Year and 2 Months')
56
- end
57
- end
58
- end
59
-
60
- describe '#in_years' do
61
- with_each_class do |clazz|
62
- it 'returns time difference in years based on Wolfram Alpha' do
63
- start_time = clazz.new(2011, 1)
64
- end_time = clazz.new(2011, 12)
65
-
66
- expect(Timely::TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
67
- end
68
-
69
- it 'returns an absolute difference' do
70
- start_time = clazz.new(2011, 12)
71
- end_time = clazz.new(2011, 1)
72
-
73
- expect(Timely::TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
74
- end
75
- end
76
- end
77
-
78
- describe '#in_months' do
79
- with_each_class do |clazz|
80
- it 'returns time difference in months based on Wolfram Alpha' do
81
- start_time = clazz.new(2011, 1)
82
- end_time = clazz.new(2011, 12)
83
-
84
- expect(Timely::TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
85
- end
86
-
87
- it 'returns an absolute difference' do
88
- start_time = clazz.new(2011, 12)
89
- end_time = clazz.new(2011, 1)
90
-
91
- expect(Timely::TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
92
- end
93
- end
94
- end
95
-
96
- describe '#in_weeks' do
97
- with_each_class do |clazz|
98
- it 'returns time difference in weeks based on Wolfram Alpha' do
99
- start_time = clazz.new(2011, 1)
100
- end_time = clazz.new(2011, 12)
101
-
102
- expect(Timely::TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
103
- end
104
-
105
- it 'returns an absolute difference' do
106
- start_time = clazz.new(2011, 12)
107
- end_time = clazz.new(2011, 1)
108
-
109
- expect(Timely::TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
110
- end
111
- end
112
- end
113
-
114
- describe '#in_days' do
115
- with_each_class do |clazz|
116
- it 'returns time difference in weeks based on Wolfram Alpha' do
117
- start_time = clazz.new(2011, 1)
118
- end_time = clazz.new(2011, 12)
119
-
120
- expect(Timely::TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
121
- end
122
-
123
- it 'returns an absolute difference' do
124
- start_time = clazz.new(2011, 12)
125
- end_time = clazz.new(2011, 1)
126
-
127
- expect(Timely::TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
128
- end
129
- end
130
- end
131
-
132
- describe '#in_hours' do
133
- with_each_class do |clazz|
134
- it 'returns time difference in hours based on Wolfram Alpha' do
135
- start_time = clazz.new(2011, 1)
136
- end_time = clazz.new(2011, 12)
137
-
138
- expect(Timely::TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
139
- end
140
-
141
- it 'returns an absolute difference' do
142
- start_time = clazz.new(2011, 12)
143
- end_time = clazz.new(2011, 1)
144
-
145
- expect(Timely::TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
146
- end
147
- end
148
- end
149
-
150
- describe '#in_minutes' do
151
- with_each_class do |clazz|
152
- it 'returns time difference in minutes based on Wolfram Alpha' do
153
- start_time = clazz.new(2011, 1)
154
- end_time = clazz.new(2011, 12)
155
-
156
- expect(Timely::TimeDifference.between(start_time, end_time).in_minutes).to eql(480_960.0)
157
- end
158
-
159
- it 'returns an absolute difference' do
160
- start_time = clazz.new(2011, 12)
161
- end_time = clazz.new(2011, 1)
162
-
163
- expect(Timely::TimeDifference.between(start_time, end_time).in_minutes).to eql(480_960.0)
164
- end
165
- end
166
- end
167
-
168
- describe '#in_seconds' do
169
- with_each_class do |clazz|
170
- it 'returns time difference in seconds based on Wolfram Alpha' do
171
- start_time = clazz.new(2011, 1)
172
- end_time = clazz.new(2011, 12)
173
-
174
- expect(Timely::TimeDifference.between(start_time, end_time).in_seconds).to eql(28_857_600.0)
175
- end
176
-
177
- it 'returns an absolute difference' do
178
- start_time = clazz.new(2011, 12)
179
- end_time = clazz.new(2011, 1)
180
-
181
- expect(Timely::TimeDifference.between(start_time, end_time).in_seconds).to eql(28_857_600.0)
182
- end
183
- end
184
- end
185
- end