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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51ac3735bac6324561fcbab7045abba1faf5af75617b92fabe0f9b063824c7a8
4
- data.tar.gz: f5e8fd4a93c5fb2f4aa878bb8e18a9d19e2142ce9505845be9500d6a0bb809b1
3
+ metadata.gz: 05402b5173907362901a6890276937450921c418596ebe53e7d3576bcebd3d7b
4
+ data.tar.gz: 4d78c11cb533cb6f45f8e6cd941887c8cc2f1645c4db5052ad543627228c3e73
5
5
  SHA512:
6
- metadata.gz: 62aa2512063f7065e799b39c5ab96e110c648bfc0468e918e49f764b2d44d9d0950b52ad6c50c1780997a04f6d61295769e2732b64508e7605c5f2bcd3b49caa
7
- data.tar.gz: ea600908598015344f193cd8679e37960be484dd60bd7d3d57e121d857a98bb27541421e3e439e1dd204c936e2799b79397f44e8c65e4361fbe2b9f509363fa3
6
+ metadata.gz: 5fcb751568bcf4c7f33ee03f4a60c55bbbf04def94475069383ed31400fc62dc8c46d70affa60374b0cace8016b1f60655275127a6cbbed36479f7cc86972879
7
+ data.tar.gz: 8b5120f531483283c834531b7b3a8ddfbc0e7f816087cbcaa50338c74a06990aa6bce5d619ac0331976e05608b645406d6ca6723a3cfec8bc936f2584a4ccc40
data/.travis.yml CHANGED
@@ -1,7 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.0
4
- - 2.4
5
3
  - 2.5
6
4
  - 2.6
7
5
  - 2.7
data/fat_period.gemspec CHANGED
@@ -27,5 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'pry-doc'
28
28
  spec.add_development_dependency 'pry-byebug'
29
29
 
30
- spec.add_runtime_dependency 'fat_core', '>= 4.8.1'
30
+ spec.add_runtime_dependency 'fat_core', '>= 4.8.3'
31
31
  end
@@ -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
- size = size.to_sym
554
- unless CHUNKS.include?(size)
555
- raise ArgumentError, "unknown chunk size '#{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(size)
561
- if chunk_start.beginning_of_chunk?(size) || partial_first
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(size)
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(size)
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(size)
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
@@ -1,3 +1,3 @@
1
1
  module FatPeriod
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.1.1'.freeze
3
3
  end
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.0
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-03-08 00:00:00.000000000 Z
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.1
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.1
110
+ version: 4.8.3
111
111
  description:
112
112
  email:
113
113
  - ded-law@ddoherty.net