active_date_range 0.4.2 → 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/CHANGELOG.md +22 -0
- data/Gemfile.lock +1 -1
- data/lib/active_date_range/date_range.rb +30 -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/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
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
|
+
|
|
1
23
|
## 0.4.2
|
|
2
24
|
|
|
3
25
|
* Add shorthand class methods for creating DateRanges:
|
data/Gemfile.lock
CHANGED
|
@@ -134,6 +134,18 @@ module ActiveDateRange
|
|
|
134
134
|
@days ||= (self.end - self.begin).to_i + 1
|
|
135
135
|
end
|
|
136
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
|
+
|
|
137
149
|
# Returns the number of months in the range or nil when range is no full month
|
|
138
150
|
def months
|
|
139
151
|
return nil unless full_month?
|
|
@@ -441,6 +453,24 @@ module ActiveDateRange
|
|
|
441
453
|
self.days > limit.in_days.ceil
|
|
442
454
|
end
|
|
443
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
|
+
|
|
444
474
|
private
|
|
445
475
|
def grouped_collection(granularity, amount: 1)
|
|
446
476
|
raise UnknownGranularity, "Unknown granularity #{granularity}. Valid are: month, quarter and year" unless %w[month quarter year].include?(granularity.to_s)
|