active_date_range 0.3.0 → 0.3.2

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: d8f934208add7b15537a13d906fa465a3ae83626551f7b1a7c485c0383e32a10
4
- data.tar.gz: 7e1c52e9c9b1da73c8c1212892761ad314d6a7490dbd8f1e516fae3f5d6ab368
3
+ metadata.gz: 87427c85b995cb2a7425de3a4b41b0df366c0d622e54cd9cbddc0f74b3608eff
4
+ data.tar.gz: 7e962cc3041aefb3f231b55b439f86147a07979c86b906a1951fdb7dbf24a1fd
5
5
  SHA512:
6
- metadata.gz: 854500cb33e2a2bf026f29433eadca6619802db32086d33656262487d7da42735c63f7eddf22849587eae15948792362c6be18ab28bead739d508f8210ed60f7
7
- data.tar.gz: e806e16dd17491eb7a45518e428b5a64b42e3fefa774b10659528c9aa8afa4b0ab0c2f5b8d5416bfb140d448c8d4f52061d3db6b276611a468d498a897ac8e5f
6
+ metadata.gz: 1c8bc3b6b648b5947415152f8c2e6d17d85ad4f9f7ef1cee06ad4b9ab3702259c60c1c6d568721dfb0de1916e06f723ec091e9046d897b3c29afac29a192b401
7
+ data.tar.gz: 7e9534d0cb558c9b440b1a05aabcd1a584d27dbb066342dc82cbd2e4afb39b68519b8b6c518e96374ed8852ed8cb41ee3ab0859310f7ef0a84d8f1284c63e643
@@ -18,15 +18,12 @@ jobs:
18
18
  runs-on: ubuntu-latest
19
19
  strategy:
20
20
  matrix:
21
- ruby-version: ['2.7', '3.0']
21
+ ruby-version: ['2.7', '3.0', '3.2']
22
22
 
23
23
  steps:
24
24
  - uses: actions/checkout@v2
25
25
  - name: Set up Ruby
26
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
27
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
28
- # uses: ruby/setup-ruby@v1
29
- uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
26
+ uses: ruby/setup-ruby@v1
30
27
  with:
31
28
  ruby-version: ${{ matrix.ruby-version }}
32
29
  bundler-cache: true # runs 'bundle install' and caches installed gems automatically
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## 0.3.2
2
+
3
+ * Add this_month?, this_quarter? and this_year? to check if a period is the current month, quarter or year
4
+
5
+ *Edwin Vlieg*
6
+
7
+
8
+ ## 0.3.1
9
+
10
+ * Fix issue with `next` not returning a full year when leap years are in the range
11
+
12
+ *Edwin Vlieg*
13
+
1
14
  ## 0.3.0
2
15
 
3
16
  * `include?` now behaves like `cover?` for better performance
data/Gemfile CHANGED
@@ -6,8 +6,10 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 12.0"
9
- gem "railties", "~> 6.1"
9
+ gem "railties", "> 6.1"
10
10
  gem "minitest", "~> 5.0"
11
11
  gem "guard"
12
12
  gem "guard-minitest"
13
13
  gem "pry"
14
+ gem "benchmark-ips"
15
+ gem "memory_profiler"
data/Gemfile.lock CHANGED
@@ -1,141 +1,155 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_date_range (0.3.0)
5
- activesupport (~> 6.1)
4
+ active_date_range (0.3.2)
5
+ activesupport (> 6.1)
6
6
  i18n
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actionpack (6.1.3.2)
12
- actionview (= 6.1.3.2)
13
- activesupport (= 6.1.3.2)
14
- rack (~> 2.0, >= 2.0.9)
11
+ actionpack (7.0.8)
12
+ actionview (= 7.0.8)
13
+ activesupport (= 7.0.8)
14
+ rack (~> 2.0, >= 2.2.4)
15
15
  rack-test (>= 0.6.3)
16
16
  rails-dom-testing (~> 2.0)
17
17
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
- actionview (6.1.3.2)
19
- activesupport (= 6.1.3.2)
18
+ actionview (7.0.8)
19
+ activesupport (= 7.0.8)
20
20
  builder (~> 3.1)
21
21
  erubi (~> 1.4)
22
22
  rails-dom-testing (~> 2.0)
23
23
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
- activemodel (6.1.3.2)
25
- activesupport (= 6.1.3.2)
26
- activesupport (6.1.3.2)
24
+ activemodel (7.0.8)
25
+ activesupport (= 7.0.8)
26
+ activesupport (7.0.8)
27
27
  concurrent-ruby (~> 1.0, >= 1.0.2)
28
28
  i18n (>= 1.6, < 2)
29
29
  minitest (>= 5.1)
30
30
  tzinfo (~> 2.0)
31
- zeitwerk (~> 2.3)
32
31
  ast (2.4.2)
32
+ base64 (0.1.1)
33
+ benchmark-ips (2.12.0)
33
34
  builder (3.2.4)
34
35
  coderay (1.1.3)
35
- concurrent-ruby (1.1.8)
36
+ concurrent-ruby (1.2.2)
36
37
  crass (1.0.6)
37
- erubi (1.10.0)
38
- ffi (1.15.1)
39
- formatador (0.2.5)
40
- guard (2.17.0)
38
+ erubi (1.12.0)
39
+ ffi (1.16.2)
40
+ formatador (1.1.0)
41
+ guard (2.18.1)
41
42
  formatador (>= 0.2.4)
42
43
  listen (>= 2.7, < 4.0)
43
44
  lumberjack (>= 1.0.12, < 2.0)
44
45
  nenv (~> 0.1)
45
46
  notiffany (~> 0.0)
46
- pry (>= 0.9.12)
47
+ pry (>= 0.13.0)
47
48
  shellany (~> 0.0)
48
49
  thor (>= 0.18.1)
49
50
  guard-compat (1.2.1)
50
51
  guard-minitest (2.4.6)
51
52
  guard-compat (~> 1.2)
52
53
  minitest (>= 3.0)
53
- i18n (1.8.10)
54
+ i18n (1.14.1)
54
55
  concurrent-ruby (~> 1.0)
55
- listen (3.5.1)
56
+ json (2.6.3)
57
+ language_server-protocol (3.17.0.3)
58
+ listen (3.8.0)
56
59
  rb-fsevent (~> 0.10, >= 0.10.3)
57
60
  rb-inotify (~> 0.9, >= 0.9.10)
58
- loofah (2.9.1)
61
+ loofah (2.21.3)
59
62
  crass (~> 1.0.2)
60
- nokogiri (>= 1.5.9)
61
- lumberjack (1.2.8)
63
+ nokogiri (>= 1.12.0)
64
+ lumberjack (1.2.9)
65
+ memory_profiler (1.0.1)
62
66
  method_source (1.0.0)
63
- mini_portile2 (2.5.1)
64
- minitest (5.14.4)
67
+ mini_portile2 (2.8.4)
68
+ minitest (5.20.0)
65
69
  nenv (0.3.0)
66
- nokogiri (1.11.5)
67
- mini_portile2 (~> 2.5.0)
70
+ nokogiri (1.15.4)
71
+ mini_portile2 (~> 2.8.2)
68
72
  racc (~> 1.4)
69
73
  notiffany (0.1.3)
70
74
  nenv (~> 0.1)
71
75
  shellany (~> 0.0)
72
- parallel (1.20.1)
73
- parser (3.0.1.1)
76
+ parallel (1.23.0)
77
+ parser (3.2.2.3)
74
78
  ast (~> 2.4.1)
75
- pry (0.14.1)
79
+ racc
80
+ pry (0.14.2)
76
81
  coderay (~> 1.1)
77
82
  method_source (~> 1.0)
78
- racc (1.5.2)
79
- rack (2.2.3)
80
- rack-test (1.1.0)
81
- rack (>= 1.0, < 3)
82
- rails-dom-testing (2.0.3)
83
- activesupport (>= 4.2.0)
83
+ racc (1.7.1)
84
+ rack (2.2.8)
85
+ rack-test (2.1.0)
86
+ rack (>= 1.3)
87
+ rails-dom-testing (2.2.0)
88
+ activesupport (>= 5.0.0)
89
+ minitest
84
90
  nokogiri (>= 1.6)
85
- rails-html-sanitizer (1.3.0)
86
- loofah (~> 2.3)
87
- railties (6.1.3.2)
88
- actionpack (= 6.1.3.2)
89
- activesupport (= 6.1.3.2)
91
+ rails-html-sanitizer (1.6.0)
92
+ loofah (~> 2.21)
93
+ nokogiri (~> 1.14)
94
+ railties (7.0.8)
95
+ actionpack (= 7.0.8)
96
+ activesupport (= 7.0.8)
90
97
  method_source
91
- rake (>= 0.8.7)
98
+ rake (>= 12.2)
92
99
  thor (~> 1.0)
93
- rainbow (3.0.0)
100
+ zeitwerk (~> 2.5)
101
+ rainbow (3.1.1)
94
102
  rake (12.3.3)
95
- rb-fsevent (0.11.0)
103
+ rb-fsevent (0.11.2)
96
104
  rb-inotify (0.10.1)
97
105
  ffi (~> 1.0)
98
- regexp_parser (2.1.1)
99
- rexml (3.2.5)
100
- rubocop (1.15.0)
106
+ regexp_parser (2.8.1)
107
+ rexml (3.2.6)
108
+ rubocop (1.56.3)
109
+ base64 (~> 0.1.1)
110
+ json (~> 2.3)
111
+ language_server-protocol (>= 3.17.0)
101
112
  parallel (~> 1.10)
102
- parser (>= 3.0.0.0)
113
+ parser (>= 3.2.2.3)
103
114
  rainbow (>= 2.2.2, < 4.0)
104
115
  regexp_parser (>= 1.8, < 3.0)
105
- rexml
106
- rubocop-ast (>= 1.5.0, < 2.0)
116
+ rexml (>= 3.2.5, < 4.0)
117
+ rubocop-ast (>= 1.28.1, < 2.0)
107
118
  ruby-progressbar (~> 1.7)
108
- unicode-display_width (>= 1.4.0, < 3.0)
109
- rubocop-ast (1.5.0)
110
- parser (>= 3.0.1.1)
111
- rubocop-packaging (0.5.1)
112
- rubocop (>= 0.89, < 2.0)
113
- rubocop-performance (1.11.3)
119
+ unicode-display_width (>= 2.4.0, < 3.0)
120
+ rubocop-ast (1.29.0)
121
+ parser (>= 3.2.1.0)
122
+ rubocop-packaging (0.5.2)
123
+ rubocop (>= 1.33, < 2.0)
124
+ rubocop-performance (1.19.1)
114
125
  rubocop (>= 1.7.0, < 2.0)
115
126
  rubocop-ast (>= 0.4.0)
116
- rubocop-rails (2.10.1)
127
+ rubocop-rails (2.21.1)
117
128
  activesupport (>= 4.2.0)
118
129
  rack (>= 1.1)
119
- rubocop (>= 1.7.0, < 2.0)
120
- ruby-progressbar (1.11.0)
130
+ rubocop (>= 1.33.0, < 2.0)
131
+ ruby-progressbar (1.13.0)
121
132
  shellany (0.0.1)
122
- thor (1.1.0)
123
- tzinfo (2.0.4)
133
+ thor (1.2.2)
134
+ tzinfo (2.0.6)
124
135
  concurrent-ruby (~> 1.0)
125
- unicode-display_width (2.0.0)
126
- zeitwerk (2.4.2)
136
+ unicode-display_width (2.4.2)
137
+ zeitwerk (2.6.12)
127
138
 
128
139
  PLATFORMS
129
140
  ruby
141
+ x86_64-linux
130
142
 
131
143
  DEPENDENCIES
132
144
  active_date_range!
133
145
  activemodel
146
+ benchmark-ips
134
147
  guard
135
148
  guard-minitest
149
+ memory_profiler
136
150
  minitest (~> 5.0)
137
151
  pry
138
- railties (~> 6.1)
152
+ railties (> 6.1)
139
153
  rake (~> 12.0)
140
154
  rubocop
141
155
  rubocop-packaging
@@ -143,4 +157,4 @@ DEPENDENCIES
143
157
  rubocop-rails
144
158
 
145
159
  BUNDLED WITH
146
- 2.1.4
160
+ 2.3.5
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ["lib"]
28
28
 
29
- spec.add_dependency "activesupport", "~> 6.1"
29
+ spec.add_dependency "activesupport", "> 6.1"
30
30
  spec.add_dependency "i18n"
31
31
 
32
32
  spec.add_development_dependency "rubocop"
@@ -128,82 +128,129 @@ module ActiveDateRange
128
128
 
129
129
  # Returns true when begin of the range is at the beginning of the month
130
130
  def begin_at_beginning_of_month?
131
- self.begin.present? && self.begin.day == 1
131
+ memoize(:@begin_at_beginning_of_month) do
132
+ self.begin.present? && self.begin.day == 1
133
+ end
132
134
  end
133
135
 
134
136
  # Returns true when begin of the range is at the beginning of the quarter
135
137
  def begin_at_beginning_of_quarter?
136
- self.begin.present? && begin_at_beginning_of_month? && [1, 4, 7, 10].include?(self.begin.month)
138
+ memoize(:@begin_at_beginning_of_quarter) do
139
+ self.begin.present? && begin_at_beginning_of_month? && [1, 4, 7, 10].include?(self.begin.month)
140
+ end
137
141
  end
138
142
 
139
143
  # Returns true when begin of the range is at the beginning of the year
140
144
  def begin_at_beginning_of_year?
141
- self.begin.present? && begin_at_beginning_of_month? && self.begin.month == 1
145
+ memoize(:@begin_at_beginning_of_year) do
146
+ self.begin.present? && begin_at_beginning_of_month? && self.begin.month == 1
147
+ end
142
148
  end
143
149
 
144
150
  # Returns true when begin of the range is at the beginning of the week
145
151
  def begin_at_beginning_of_week?
146
- self.begin.present? && self.begin == self.begin.at_beginning_of_week
152
+ memoize(:@begin_at_beginning_of_week) do
153
+ self.begin.present? && self.begin == self.begin.at_beginning_of_week
154
+ end
147
155
  end
148
156
 
149
157
  # Returns true when the range is exactly one month long
150
158
  def one_month?
151
- (28..31).cover?(days) &&
152
- begin_at_beginning_of_month? &&
153
- self.end == self.begin.at_end_of_month
159
+ memoize(:@one_month) do
160
+ (28..31).cover?(days) &&
161
+ begin_at_beginning_of_month? &&
162
+ self.end == self.begin.at_end_of_month
163
+ end
154
164
  end
155
165
 
156
166
  # Returns true when the range is exactly one quarter long
157
167
  def one_quarter?
158
- (90..92).cover?(days) &&
159
- begin_at_beginning_of_quarter? &&
160
- self.end == self.begin.at_end_of_quarter
168
+ memoize(:@one_quarter) do
169
+ (90..92).cover?(days) &&
170
+ begin_at_beginning_of_quarter? &&
171
+ self.end == self.begin.at_end_of_quarter
172
+ end
161
173
  end
162
174
 
163
175
  # Returns true when the range is exactly one year long
164
176
  def one_year?
165
- (365..366).cover?(days) &&
166
- begin_at_beginning_of_year? &&
167
- self.end == self.begin.at_end_of_year
177
+ memoize(:@one_year) do
178
+ (365..366).cover?(days) &&
179
+ begin_at_beginning_of_year? &&
180
+ self.end == self.begin.at_end_of_year
181
+ end
168
182
  end
169
183
 
170
184
  def one_week?
171
- days == 7 &&
172
- begin_at_beginning_of_week? &&
173
- self.end == self.begin.at_end_of_week
185
+ memoize(:@one_week) do
186
+ days == 7 &&
187
+ begin_at_beginning_of_week? &&
188
+ self.end == self.begin.at_end_of_week
189
+ end
174
190
  end
175
191
 
176
192
  # Returns true when the range is exactly one or more months long
177
193
  def full_month?
178
- begin_at_beginning_of_month? && self.end.present? && self.end == self.end.at_end_of_month
194
+ memoize(:@full_month) do
195
+ begin_at_beginning_of_month? && self.end.present? && self.end == self.end.at_end_of_month
196
+ end
179
197
  end
180
198
 
181
199
  alias :full_months? :full_month?
182
200
 
183
201
  # Returns true when the range is exactly one or more quarters long
184
202
  def full_quarter?
185
- begin_at_beginning_of_quarter? && self.end.present? && self.end == self.end.at_end_of_quarter
203
+ memoize(:@full_quarter) do
204
+ begin_at_beginning_of_quarter? && self.end.present? && self.end == self.end.at_end_of_quarter
205
+ end
186
206
  end
187
207
 
188
208
  alias :full_quarters? :full_quarter?
189
209
 
190
210
  # Returns true when the range is exactly one or more years long
191
211
  def full_year?
192
- begin_at_beginning_of_year? && self.end.present? && self.end == self.end.at_end_of_year
212
+ memoize(:@full_year) do
213
+ begin_at_beginning_of_year? && self.end.present? && self.end == self.end.at_end_of_year
214
+ end
193
215
  end
194
216
 
195
217
  alias :full_years? :full_year?
196
218
 
197
219
  # Returns true when the range is exactly one or more weeks long
198
220
  def full_week?
199
- begin_at_beginning_of_week? && self.end.present? && self.end == self.end.at_end_of_week
221
+ memoize(:@full_week) do
222
+ begin_at_beginning_of_week? && self.end.present? && self.end == self.end.at_end_of_week
223
+ end
200
224
  end
201
225
 
202
226
  alias :full_weeks? :full_week?
203
227
 
204
228
  # Returns true when begin and end are in the same year
205
229
  def same_year?
206
- !boundless? && self.begin.year == self.end.year
230
+ memoize(:@same_year) do
231
+ !boundless? && self.begin.year == self.end.year
232
+ end
233
+ end
234
+
235
+ # Return true when the range is equal to the current month
236
+ def this_month?
237
+ memoize(:@this_month) do
238
+ self == DateRange.this_month
239
+ end
240
+ end
241
+
242
+ # Return true when the range is equal to the current quarter
243
+ def this_quarter?
244
+ memoize(:@this_quarter) do
245
+ self == DateRange.this_quarter
246
+ end
247
+ end
248
+
249
+ # Return true when the range is equal to the current year
250
+ def this_year?
251
+ memoize(:@this_year) do
252
+ self == DateRange.this_year
253
+ end
207
254
  end
208
255
 
209
256
  # Returns true when the date range is before the given date. Accepts both a <tt>Date</tt>
@@ -227,14 +274,16 @@ module ActiveDateRange
227
274
  # DateRange.this_quarter.granularity # => :quarter
228
275
  # DateRange.this_year.granularity # => :year
229
276
  def granularity
230
- if one_year?
231
- :year
232
- elsif one_quarter?
233
- :quarter
234
- elsif one_month?
235
- :month
236
- elsif one_week?
237
- :week
277
+ memoize(:@granularity) do
278
+ if one_year?
279
+ :year
280
+ elsif one_quarter?
281
+ :quarter
282
+ elsif one_month?
283
+ :month
284
+ elsif one_week?
285
+ :week
286
+ end
238
287
  end
239
288
  end
240
289
 
@@ -242,11 +291,13 @@ module ActiveDateRange
242
291
  # a range of 2021-01-01..2021-12-31 will return `this_year` when the current date
243
292
  # is somewhere in 2021.
244
293
  def relative_param
245
- @relative_param ||= SHORTHANDS
246
- .select { |key, _| key.end_with?(granularity.to_s) }
247
- .find { |key, range| self == range.call }
248
- &.first
249
- &.to_s
294
+ memoize(:@relative_param) do
295
+ SHORTHANDS
296
+ .select { |key, _| key.end_with?(granularity.to_s) }
297
+ .find { |key, range| self == range.call }
298
+ &.first
299
+ &.to_s
300
+ end
250
301
  end
251
302
 
252
303
  # Returns a param representation of the date range. When `relative` is true,
@@ -306,7 +357,13 @@ module ActiveDateRange
306
357
  def next(periods = 1)
307
358
  raise BoundlessRangeError, "Can't calculate next for boundless range" if boundless?
308
359
 
309
- end_date = self.end + (granularity ? periods.send(granularity) : days.days)
360
+ end_date = if granularity
361
+ self.end + periods.send(granularity)
362
+ elsif full_month?
363
+ in_groups_of(:month).last.next(periods * months).end
364
+ else
365
+ self.end + (periods * days).days
366
+ end
310
367
  end_date = end_date.at_end_of_month if full_month?
311
368
 
312
369
  DateRange.new(self.end + 1.day, end_date)
@@ -358,5 +415,13 @@ module ActiveDateRange
358
415
  .slice_before { |_, index| index % amount == 0 }
359
416
  .map { |group| group.map(&:first).inject(:+) }
360
417
  end
418
+
419
+ def memoize(name)
420
+ if instance_variable_defined?(name)
421
+ instance_variable_get(name)
422
+ else
423
+ instance_variable_set(name, yield)
424
+ end
425
+ end
361
426
  end
362
427
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDateRange
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.2"
5
5
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_date_range
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edwin Vlieg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-18 00:00:00.000000000 Z
11
+ date: 2023-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '6.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '6.1'
27
27
  - !ruby/object:Gem::Dependency
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  - !ruby/object:Gem::Version
161
161
  version: '0'
162
162
  requirements: []
163
- rubygems_version: 3.1.4
163
+ rubygems_version: 3.3.26
164
164
  signing_key:
165
165
  specification_version: 4
166
166
  summary: DateRange for ActiveSupport