active_date_range 0.4.1 → 0.5.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 +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +35 -0
- data/Gemfile.lock +1 -4
- data/lib/active_date_range/date_range.rb +60 -0
- data/lib/active_date_range/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fc26289633430de8df95f11a9f5daae40517b28bd84659aaef39b98a3af14fa7
|
|
4
|
+
data.tar.gz: effca528bd820224332d65ba526d71632cb8663d167817748392c78ca7118d24
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c9d483ffcb44834773674421bb5a4fce07b6256ce6629e724e3c4180db96ab048da817d7fa644575c0abe7591e9c893874965ed1db9d1d136e383738c3fd700f
|
|
7
|
+
data.tar.gz: 82e99bbd1690fdd21fe74ea824b7ef072d6ff1f2ca9b9b0ab673ce0050af7336fe17ffa2bf2a8cf14ba43d6b0d6031585a8eb30ce88de07788e5514b7a9b7246
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
+
## 0.5.0
|
|
2
|
+
|
|
3
|
+
* Add `size` and `length` methods that return an `ActiveSupport::Duration`, compatible with `validates_length_of`:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
validates_length_of :period, maximum: 10.years
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
*Edwin Vlieg*
|
|
10
|
+
|
|
11
|
+
* Add `cap_end` and `cap_begin` to limit a range to a given duration:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
DateRange.parse("202101..202512").cap_end(2.years)
|
|
15
|
+
# => DateRange(2021-01-01..2022-12-31)
|
|
16
|
+
|
|
17
|
+
DateRange.parse("202101..202512").cap_begin(2.years)
|
|
18
|
+
# => DateRange(2024-01-01..2025-12-31)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
*Edwin Vlieg*
|
|
22
|
+
|
|
23
|
+
## 0.4.2
|
|
24
|
+
|
|
25
|
+
* Add shorthand class methods for creating DateRanges:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
DateRange.year(2026) # => DateRange(2026-01-01..2026-12-31)
|
|
29
|
+
DateRange.quarter(2026, 4) # => DateRange(2026-10-01..2026-12-31)
|
|
30
|
+
DateRange.month(2026, 1) # => DateRange(2026-01-01..2026-01-31)
|
|
31
|
+
DateRange.week(2026, 1) # => DateRange for ISO week 1 of 2026
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
*Edwin Vlieg*
|
|
35
|
+
|
|
1
36
|
## 0.4.1
|
|
2
37
|
|
|
3
38
|
* Fix dependency issues in gemspec
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
active_date_range (0.
|
|
4
|
+
active_date_range (0.5.0)
|
|
5
5
|
activesupport (~> 8.0)
|
|
6
6
|
i18n (~> 1.6)
|
|
7
7
|
|
|
@@ -54,7 +54,6 @@ GEM
|
|
|
54
54
|
erb (5.0.3)
|
|
55
55
|
erubi (1.13.1)
|
|
56
56
|
ffi (1.17.2)
|
|
57
|
-
ffi (1.17.2-x86_64-linux-gnu)
|
|
58
57
|
formatador (1.2.1)
|
|
59
58
|
reline
|
|
60
59
|
guard (2.19.1)
|
|
@@ -98,8 +97,6 @@ GEM
|
|
|
98
97
|
nokogiri (1.18.10)
|
|
99
98
|
mini_portile2 (~> 2.8.2)
|
|
100
99
|
racc (~> 1.4)
|
|
101
|
-
nokogiri (1.18.10-x86_64-linux-gnu)
|
|
102
|
-
racc (~> 1.4)
|
|
103
100
|
notiffany (0.1.3)
|
|
104
101
|
nenv (~> 0.1)
|
|
105
102
|
shellany (~> 0.0)
|
|
@@ -64,6 +64,36 @@ module ActiveDateRange
|
|
|
64
64
|
new(date, date + duration - 1.day)
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
+
# Creates a DateRange for a full year.
|
|
68
|
+
#
|
|
69
|
+
# DateRange.year(2026) # => DateRange(2026-01-01..2026-12-31)
|
|
70
|
+
def self.year(year)
|
|
71
|
+
new(Date.new(year, 1, 1).all_year)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Creates a DateRange for a specific month.
|
|
75
|
+
#
|
|
76
|
+
# DateRange.month(2026, 1) # => DateRange(2026-01-01..2026-01-31)
|
|
77
|
+
def self.month(year, month)
|
|
78
|
+
new(Date.new(year, month, 1).all_month)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Creates a DateRange for a specific quarter (1-4).
|
|
82
|
+
#
|
|
83
|
+
# DateRange.quarter(2026, 4) # => DateRange(2026-10-01..2026-12-31)
|
|
84
|
+
def self.quarter(year, quarter)
|
|
85
|
+
month = ((quarter - 1) * 3) + 1
|
|
86
|
+
new(Date.new(year, month, 1).all_quarter)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Creates a DateRange for a specific ISO week.
|
|
90
|
+
#
|
|
91
|
+
# DateRange.week(2026, 1) # => DateRange for ISO week 1 of 2026
|
|
92
|
+
def self.week(year, week)
|
|
93
|
+
date = Date.commercial(year, week, 1)
|
|
94
|
+
new(date.all_week)
|
|
95
|
+
end
|
|
96
|
+
|
|
67
97
|
private_class_method :parse_date
|
|
68
98
|
|
|
69
99
|
# Initializes a new DateRange. Accepts both a begin and end date or a range of dates.
|
|
@@ -104,6 +134,18 @@ module ActiveDateRange
|
|
|
104
134
|
@days ||= (self.end - self.begin).to_i + 1
|
|
105
135
|
end
|
|
106
136
|
|
|
137
|
+
# Returns the duration of the range as an ActiveSupport::Duration, compatible with
|
|
138
|
+
# validates_length_of. Use Duration values for the constraint:
|
|
139
|
+
#
|
|
140
|
+
# validates_length_of :period, maximum: 10.years
|
|
141
|
+
# validates_length_of :period, maximum: 6.months
|
|
142
|
+
# validates_length_of :period, maximum: 30.days
|
|
143
|
+
def size
|
|
144
|
+
days&.days
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
alias :length :size
|
|
148
|
+
|
|
107
149
|
# Returns the number of months in the range or nil when range is no full month
|
|
108
150
|
def months
|
|
109
151
|
return nil unless full_month?
|
|
@@ -411,6 +453,24 @@ module ActiveDateRange
|
|
|
411
453
|
self.days > limit.in_days.ceil
|
|
412
454
|
end
|
|
413
455
|
|
|
456
|
+
# Returns a new DateRange with the end date capped to the given duration from the begin date.
|
|
457
|
+
#
|
|
458
|
+
# DateRange.parse("202101..202512").cap_end(2.years) # => DateRange(2021-01-01..2022-12-31)
|
|
459
|
+
def cap_end(duration)
|
|
460
|
+
return self if boundless? || !exceeds?(duration)
|
|
461
|
+
|
|
462
|
+
DateRange.new(self.begin, self.begin + duration - 1.day)
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
# Returns a new DateRange with the begin date capped to the given duration from the end date.
|
|
466
|
+
#
|
|
467
|
+
# DateRange.parse("202101..202512").cap_begin(2.years) # => DateRange(2024-01-01..2025-12-31)
|
|
468
|
+
def cap_begin(duration)
|
|
469
|
+
return self if boundless? || !exceeds?(duration)
|
|
470
|
+
|
|
471
|
+
DateRange.new(self.end - duration + 1.day, self.end)
|
|
472
|
+
end
|
|
473
|
+
|
|
414
474
|
private
|
|
415
475
|
def grouped_collection(granularity, amount: 1)
|
|
416
476
|
raise UnknownGranularity, "Unknown granularity #{granularity}. Valid are: month, quarter and year" unless %w[month quarter year].include?(granularity.to_s)
|