fat_period 1.0.3 → 1.1.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: 8a2fde65d6d14d1a0c94d09b2097528ef2f23826388b1f3291ac7e7d0b3312af
4
- data.tar.gz: c14e872d1fb6abf9015a4acf3a3b5c21c23afe28e05f8f15ba7df6db921751d2
3
+ metadata.gz: 51ac3735bac6324561fcbab7045abba1faf5af75617b92fabe0f9b063824c7a8
4
+ data.tar.gz: f5e8fd4a93c5fb2f4aa878bb8e18a9d19e2142ce9505845be9500d6a0bb809b1
5
5
  SHA512:
6
- metadata.gz: 23ab6b2bc98abfc6d7609df56f93b7acf8683d29fdd13a54c7377b321ffc0a29d29c5498c327054ef5c970c5650b129757fca7a0b71db4132c4d5328a9b31c24
7
- data.tar.gz: d266e75e1c4caaecbd14952cad1ffbc6cea36e65c6d6b5264b353edc7c3b5604aa04709489c5f3ffa058f3d0d9a236ce1ca2d228283e907092166c19351ddc72
6
+ metadata.gz: 62aa2512063f7065e799b39c5ab96e110c648bfc0468e918e49f764b2d44d9d0950b52ad6c50c1780997a04f6d61295769e2732b64508e7605c5f2bcd3b49caa
7
+ data.tar.gz: ea600908598015344f193cd8679e37960be484dd60bd7d3d57e121d857a98bb27541421e3e439e1dd204c936e2799b79397f44e8c65e4361fbe2b9f509363fa3
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'
30
+ spec.add_runtime_dependency 'fat_core', '>= 4.8.1'
31
31
  end
@@ -29,37 +29,9 @@ class Period
29
29
  # @raise [ArgumentError] if first date is later than last date
30
30
  # @return [Period]
31
31
  def initialize(first, last)
32
- if first.is_a?(Date)
33
- @first = first
34
- elsif first.respond_to?(:to_s)
35
- begin
36
- @first = Date.parse(first.to_s)
37
- rescue ArgumentError => e
38
- if e.message =~ /invalid date/
39
- raise ArgumentError, "invalid date '#{first}'"
40
- end
41
-
42
- raise
43
- end
44
- else
45
- raise ArgumentError, 'use Date or String to initialize Period'
46
- end
47
-
48
- if last.is_a?(Date)
49
- @last = last
50
- elsif last.respond_to?(:to_s)
51
- begin
52
- @last = Date.parse(last.to_s)
53
- rescue ArgumentError => e
54
- if e.message =~ /invalid date/
55
- raise ArgumentError, "you gave an invalid date '#{last}'"
56
- end
32
+ @first = Date.ensure_date(first)
33
+ @last = Date.ensure_date(last)
57
34
 
58
- raise
59
- end
60
- else
61
- raise ArgumentError, 'use Date or String to initialize Period'
62
- end
63
35
  return unless @first > @last
64
36
 
65
37
  raise ArgumentError, "Period's first date is later than its last date"
@@ -583,74 +555,43 @@ class Period
583
555
  raise ArgumentError, "unknown chunk size '#{size}'"
584
556
  end
585
557
 
586
- if CHUNK_RANGE[size].first > length
587
- return [self] if partial_first || partial_last
588
-
589
- msg = "any #{size} is longer than this period's #{length} days"
590
- raise ArgumentError, msg
591
- end
592
-
593
558
  result = []
594
559
  chunk_start = first.dup
595
- while chunk_start <= last
596
- case size
597
- when :year
598
- unless partial_first
599
- chunk_start += 1.day until chunk_start.beginning_of_year?
600
- end
601
- chunk_end = chunk_start.end_of_year
602
- when :half
603
- unless partial_first
604
- chunk_start += 1.day until chunk_start.beginning_of_half?
605
- end
606
- chunk_end = chunk_start.end_of_half
607
- when :quarter
608
- unless partial_first
609
- chunk_start += 1.day until chunk_start.beginning_of_quarter?
610
- end
611
- chunk_end = chunk_start.end_of_quarter
612
- when :bimonth
613
- unless partial_first
614
- chunk_start += 1.day until chunk_start.beginning_of_bimonth?
615
- end
616
- chunk_end = (chunk_start.end_of_month + 1.day).end_of_month
617
- when :month
618
- unless partial_first
619
- chunk_start += 1.day until chunk_start.beginning_of_month?
620
- end
621
- chunk_end = chunk_start.end_of_month
622
- when :semimonth
623
- unless partial_first
624
- chunk_start += 1.day until chunk_start.beginning_of_semimonth?
625
- end
626
- chunk_end = chunk_start.end_of_semimonth
627
- when :biweek
628
- unless partial_first
629
- chunk_start += 1.day until chunk_start.beginning_of_biweek?
630
- end
631
- chunk_end = chunk_start.end_of_biweek
632
- when :week
633
- unless partial_first
634
- chunk_start += 1.day until chunk_start.beginning_of_week?
635
- end
636
- chunk_end = chunk_start.end_of_week
637
- when :day
638
- chunk_end = chunk_start
639
- else
640
- raise ArgumentError, "invalid chunk size '#{size}'"
641
- end
642
- if chunk_end <= last
643
- result << Period.new(chunk_start, chunk_end)
644
- elsif round_up_last
560
+ chunk_end = chunk_start.end_of_chunk(size)
561
+ if chunk_start.beginning_of_chunk?(size) || partial_first
562
+ # Keep the first chunk if it's whole or partials allowed
563
+ result << Period.new(chunk_start, chunk_end)
564
+ chunk_start = chunk_end + 1.day
565
+ chunk_end = chunk_start.end_of_chunk(size)
566
+ else
567
+ # Discard the partial first or move to next whole chunk
568
+ chunk_start = chunk_end + 1.day
569
+ chunk_end = chunk_start.end_of_chunk(size)
570
+ end
571
+ # Add Whole chunks
572
+ while chunk_end <= last
573
+ result << Period.new(chunk_start, chunk_end)
574
+ chunk_start = chunk_end + 1.day
575
+ chunk_end = chunk_start.end_of_chunk(size)
576
+ end
577
+ # Possibly append the final chunk to result
578
+ if chunk_start < last
579
+ if round_up_last
645
580
  result << Period.new(chunk_start, chunk_end)
646
581
  elsif partial_last
647
582
  result << Period.new(chunk_start, last)
648
583
  else
649
- break
584
+ result
650
585
  end
651
- chunk_start = result.last.last + 1.day
586
+ elsif partial_last
587
+ # Catch the case where the period is too small to make a whole chunk and
588
+ # partial_first is false, so it did not get included as the initial
589
+ # partial chunk, yet a partial_last is allowed, so include the whole
590
+ # period as a partial chunk.
591
+ result << Period.new(first, last)
592
+ else
593
+ result
652
594
  end
653
- result
654
595
  end
655
596
 
656
597
  # @group Set operations
@@ -1,3 +1,3 @@
1
1
  module FatPeriod
2
- VERSION = '1.0.3'.freeze
2
+ VERSION = '1.1.0'.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.0.3
4
+ version: 1.1.0
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-02-23 00:00:00.000000000 Z
11
+ date: 2020-03-08 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: '0'
103
+ version: 4.8.1
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: '0'
110
+ version: 4.8.1
111
111
  description:
112
112
  email:
113
113
  - ded-law@ddoherty.net