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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39f9a839e43c1880dc6316e99c40e0c07ad5fd18de9cf19658737eeb7e584d7a
4
- data.tar.gz: 7d2827d72f24425b5ec13e2fe6a1783834e414da75f602a530bc3ff335842723
3
+ metadata.gz: fc26289633430de8df95f11a9f5daae40517b28bd84659aaef39b98a3af14fa7
4
+ data.tar.gz: effca528bd820224332d65ba526d71632cb8663d167817748392c78ca7118d24
5
5
  SHA512:
6
- metadata.gz: b3754ca3eda22da5b853ad54643a1842f97606b15318c472da86ea379da540dd026d1c2de201d5d1b0838d9275e1e197df1ac0cd742f33885b1d6593bc145341
7
- data.tar.gz: e4ccdd0cfbb3b39cb24b2f322ef2bf7be823793721f3bb00546f7054503649804f1045091d50b907b32fd75d410024bf5afbf801bf9190241f4cae811d1bfb19
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_date_range (0.4.2)
4
+ active_date_range (0.5.0)
5
5
  activesupport (~> 8.0)
6
6
  i18n (~> 1.6)
7
7
 
@@ -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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDateRange
4
- VERSION = "0.4.2"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_date_range
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edwin Vlieg