fat_period 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/fat_period.gemspec +1 -1
- data/lib/fat_period/period.rb +50 -8
- data/lib/fat_period/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05402b5173907362901a6890276937450921c418596ebe53e7d3576bcebd3d7b
|
4
|
+
data.tar.gz: 4d78c11cb533cb6f45f8e6cd941887c8cc2f1645c4db5052ad543627228c3e73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fcb751568bcf4c7f33ee03f4a60c55bbbf04def94475069383ed31400fc62dc8c46d70affa60374b0cace8016b1f60655275127a6cbbed36479f7cc86972879
|
7
|
+
data.tar.gz: 8b5120f531483283c834531b7b3a8ddfbc0e7f816087cbcaa50338c74a06990aa6bce5d619ac0331976e05608b645406d6ca6723a3cfec8bc936f2584a4ccc40
|
data/.travis.yml
CHANGED
data/fat_period.gemspec
CHANGED
data/lib/fat_period/period.rb
CHANGED
@@ -269,6 +269,12 @@ class Period
|
|
269
269
|
CHUNKS = %i[day week biweek semimonth month bimonth quarter
|
270
270
|
half year irregular].freeze
|
271
271
|
|
272
|
+
CHUNK_ORDER = {}
|
273
|
+
CHUNKS.each_with_index do |c, i|
|
274
|
+
CHUNK_ORDER[c] = i
|
275
|
+
end
|
276
|
+
CHUNK_ORDER.freeze
|
277
|
+
|
272
278
|
# An Array of Ranges for the number of days that can be covered by each chunk.
|
273
279
|
CHUNK_RANGE = {
|
274
280
|
day: (1..1), week: (7..7), biweek: (14..14), semimonth: (15..16),
|
@@ -276,6 +282,10 @@ class Period
|
|
276
282
|
half: (180..183), year: (365..366)
|
277
283
|
}.freeze
|
278
284
|
|
285
|
+
def self.chunk_cmp(chunk1, chunk2)
|
286
|
+
CHUNK_ORDER[chunk1] <=> CHUNK_ORDER[chunk2]
|
287
|
+
end
|
288
|
+
|
279
289
|
# Return a period representing a chunk containing a given Date.
|
280
290
|
def self.day_containing(date)
|
281
291
|
Period.new(date, date)
|
@@ -313,6 +323,19 @@ class Period
|
|
313
323
|
Period.new(date.beginning_of_year, date.end_of_year)
|
314
324
|
end
|
315
325
|
|
326
|
+
def self.chunk_containing(date, chunk)
|
327
|
+
raise ArgumentError, 'chunk is nil' unless chunk
|
328
|
+
|
329
|
+
chunk = chunk.to_sym
|
330
|
+
unless CHUNKS.include?(chunk)
|
331
|
+
raise ArgumentError, "unknown chunk name: #{chunk}"
|
332
|
+
end
|
333
|
+
|
334
|
+
date = Date.ensure_date(date)
|
335
|
+
method = "#{chunk}_containing".to_sym
|
336
|
+
send(method, date)
|
337
|
+
end
|
338
|
+
|
316
339
|
# Return a Period representing a chunk containing today.
|
317
340
|
def self.this_day
|
318
341
|
day_containing(Date.current)
|
@@ -550,29 +573,48 @@ class Period
|
|
550
573
|
# @return [Array<Period>] periods that subdivide self into chunks of size, `size`
|
551
574
|
def chunks(size: :month, partial_first: false, partial_last: false,
|
552
575
|
round_up_last: false)
|
553
|
-
|
554
|
-
unless CHUNKS.include?(
|
555
|
-
raise ArgumentError, "unknown chunk size '#{
|
576
|
+
chunk_size = size.to_sym
|
577
|
+
unless CHUNKS.include?(chunk_size)
|
578
|
+
raise ArgumentError, "unknown chunk size '#{chunk_size}'"
|
556
579
|
end
|
557
580
|
|
581
|
+
containing_period = Period.chunk_containing(first, chunk_size)
|
582
|
+
return [dup] if self == containing_period
|
583
|
+
|
584
|
+
# Period too small for even a single chunk and is wholly-contained by a
|
585
|
+
# single chunk.
|
558
586
|
result = []
|
587
|
+
if proper_subset_of?(containing_period)
|
588
|
+
result =
|
589
|
+
if partial_first || partial_last
|
590
|
+
if round_up_last
|
591
|
+
[containing_period]
|
592
|
+
else
|
593
|
+
[dup]
|
594
|
+
end
|
595
|
+
else
|
596
|
+
[]
|
597
|
+
end
|
598
|
+
return result
|
599
|
+
end
|
600
|
+
|
559
601
|
chunk_start = first.dup
|
560
|
-
chunk_end = chunk_start.end_of_chunk(
|
561
|
-
if chunk_start.beginning_of_chunk?(
|
602
|
+
chunk_end = chunk_start.end_of_chunk(chunk_size)
|
603
|
+
if chunk_start.beginning_of_chunk?(chunk_size) || partial_first
|
562
604
|
# Keep the first chunk if it's whole or partials allowed
|
563
605
|
result << Period.new(chunk_start, chunk_end)
|
564
606
|
chunk_start = chunk_end + 1.day
|
565
|
-
chunk_end = chunk_start.end_of_chunk(
|
607
|
+
chunk_end = chunk_start.end_of_chunk(chunk_size)
|
566
608
|
else
|
567
609
|
# Discard the partial first or move to next whole chunk
|
568
610
|
chunk_start = chunk_end + 1.day
|
569
|
-
chunk_end = chunk_start.end_of_chunk(
|
611
|
+
chunk_end = chunk_start.end_of_chunk(chunk_size)
|
570
612
|
end
|
571
613
|
# Add Whole chunks
|
572
614
|
while chunk_end <= last
|
573
615
|
result << Period.new(chunk_start, chunk_end)
|
574
616
|
chunk_start = chunk_end + 1.day
|
575
|
-
chunk_end = chunk_start.end_of_chunk(
|
617
|
+
chunk_end = chunk_start.end_of_chunk(chunk_size)
|
576
618
|
end
|
577
619
|
# Possibly append the final chunk to result
|
578
620
|
if chunk_start < last
|
data/lib/fat_period/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fat_period
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel E. Doherty
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 4.8.
|
103
|
+
version: 4.8.3
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: 4.8.
|
110
|
+
version: 4.8.3
|
111
111
|
description:
|
112
112
|
email:
|
113
113
|
- ded-law@ddoherty.net
|