fat_period 2.0.0 → 2.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: 5742b6c1b70e7477cb2b8e077c3b1e5d96fc417858f618a12ad64a457df692e4
4
- data.tar.gz: 7965192d8da3ea88002089c52dc9344cae1f75e7fab28739185b5f290c3f1bc9
3
+ metadata.gz: 875ad1917477c27f17278fb0d5aad18423f4dcf52f32c9957651c829a1164f4b
4
+ data.tar.gz: 1495e04adb60e49dfc26f1b94c9699a129f30b7a15c36e9fa82d0e68d87ed183
5
5
  SHA512:
6
- metadata.gz: 78bf26849daf793f3b2115e136599178817fd43a1a03bc2882eea408bd7995c44feb94998f22bb220afeb3c2b8270770aa7dc586736849af5f5506f6f7e1123b
7
- data.tar.gz: dc0b08cb99a577a8fe11827e96ef1c9473a4536fe4ae0e7dd483fc7a1d1d0493a333eecbbcc8a2cd9f710d03489bc32aa4c8695b9787007dc51efddb9805e14b
6
+ metadata.gz: f7e5ece151032b3ce11aad94225ef649c9e217a2d9239406f3ef030b335169cc33a130ff860b11b65a2e9d90ab4d9f900eb6df713f904beb9d841a7068ebcb96
7
+ data.tar.gz: 91a94d16666150d0eba4cd4bd5cb24813150d714f2e534a15746bd70119e87996d39940d8da40c5ab7589707975fe53f60a53cd4ae7d9bd58254b710d7d3c4ee
@@ -30,9 +30,7 @@ class Period
30
30
  @last = Date.ensure_date(last).freeze
31
31
  freeze
32
32
 
33
- return unless @first > @last
34
-
35
- raise ArgumentError, "Period's first date is later than its last date"
33
+ raise ArgumentError, "Period's first date is later than its last date" if @first > @last
36
34
  end
37
35
 
38
36
  # These need to come after initialize is defined
@@ -66,35 +64,73 @@ class Period
66
64
  Period.new(first, second) if first && second
67
65
  end
68
66
 
69
- # Return a period as in `Period.parse` from a String phrase in which the from
70
- # spec is introduced with 'from' and, optionally, the to spec is introduced
71
- # with 'to'. A phrase with only a to spec is treated the same as one with
72
- # only a from spec. If neither 'from' nor 'to' appear in phrase, treat the
73
- # whole string as a from spec.
67
+ # Return a Period either from a given String or other type that can
68
+ # reasonably converted to a Period.
74
69
  #
75
70
  # @example
76
- # Period.parse_phrase('from 2014-11 to 2015-3Q') #=> Period('2014-11-01..2015-09-30')
77
- # Period.parse_phrase('from 2014-11') #=> Period('2014-11-01..2014-11-30')
78
- # Period.parse_phrase('from 2015-3Q') #=> Period('2015-09-01..2015-12-31')
79
- # Period.parse_phrase('to 2015-3Q') #=> Period('2015-09-01..2015-12-31')
80
- # Period.parse_phrase('2015-3Q') #=> Period('2015-09-01..2015-12-31')
71
+ # Period.ensure('2014-11').inspect #=> Period('2014-11-01..2014-11-30')
72
+ # pd = Period.parse('2011')
73
+ # Period.ensure(pd).inspect #=> Period('2011-01-01..2011-12-31')
74
+ #
75
+ # @param prd [String|Period] or any candidate for conversion to Period
76
+ # @return Period correspondign to prd parameter
77
+ def self.ensure(prd)
78
+ prd.to_period if prd.respond_to?(:to_period)
79
+ case prd
80
+ when String
81
+ if prd.match(/from|to/i)
82
+ Period.parse_phrase(prd).first
83
+ else
84
+ Period.parse(prd)
85
+ end
86
+ when Period
87
+ prd
88
+ end
89
+ end
90
+
91
+ # Return an Array of Periods from a String phrase in which the from spec is
92
+ # introduced with 'from' and, optionally, the to spec is introduced with
93
+ # 'to' and optionally a 'per' clause is introduced by 'per'. A phrase with
94
+ # only a to spec is treated the same as one with only a from spec. If
95
+ # neither 'from' nor 'to' appear in phrase, treat the string before any
96
+ # per-clause as a from spec.
81
97
  #
82
- # @param phrase [String] with 'from <spec> to <spec>'
98
+ # @example
99
+ # Period.parse_phrase('from 2014-11 to 2015-3Q') #=> [Period('2014-11-01..2015-09-30')]
100
+ # Period.parse_phrase('from 2014-11') #=> [Period('2014-11-01..2014-11-30')]
101
+ # Period.parse_phrase('from 2015-3Q') #=> [Period('2015-09-01..2015-12-31')]
102
+ # Period.parse_phrase('to 2015-3Q') #=> [Period('2015-09-01..2015-12-31')]
103
+ # Period.parse_phrase('2015-3Q') #=> [Period('2015-09-01..2015-12-31')]
104
+ # Period.parse_phrase('to 2015-3Q per week') #=> [Period('2015-09-01..2015-09-04')...]
105
+ # Period.parse_phrase('2015-3Q per month') #=> [Period('2015-09-01..2015-09-30')...]
106
+ #
107
+ # @param phrase [String] with 'from <spec> [to <spec>] [per chunk]'
83
108
  # @return [Period] translated from phrase
84
- def self.parse_phrase(phrase)
109
+ def self.parse_phrase(phrase, partial_first: true, partial_last: true, round_up_last: false)
85
110
  phrase = phrase.clean
86
111
  case phrase
87
- when /\Afrom (.*) to (.*)\z/
112
+ when /\Afrom\s+([^\s]+)\s+to\s+([^\s]+)(\s+per\s+[^\s]+)?\z/i
88
113
  from_phrase = $1
89
114
  to_phrase = $2
90
- when /\Afrom (.*)\z/, /\Ato (.*)\z/
115
+ when /\Afrom\s+([^\s]+)(\s+per\s+[^\s]+)?\z/, /\Ato\s+([^\s]+)(\s+per\s+[^\s]+)?\z/i
91
116
  from_phrase = $1
92
117
  to_phrase = nil
93
- else
94
- from_phrase = phrase
118
+ when /\A([^\s]+)(\s+per\s+[^\s]+)?\z/
119
+ from_phrase = $1
95
120
  to_phrase = nil
121
+ else
122
+ raise ArgumentError, "unintelligible period phrase: '#{phrase}''"
123
+ end
124
+ # Return an Array of periods divided by chunks if any.
125
+ whole_period = parse(from_phrase, to_phrase)
126
+ if phrase =~ /per\s+(?<chunk>[a-z_]+)/i
127
+ chunk_size = Regexp.last_match[:chunk].downcase.to_sym
128
+ raise ArgumentError, "invalid chunk size #{chunk_size}" unless CHUNKS.include?(chunk_size)
129
+
130
+ whole_period.chunks(size: chunk_size, partial_first:, partial_last:, round_up_last:)
131
+ else
132
+ [whole_period]
96
133
  end
97
- parse(from_phrase, to_phrase)
98
134
  end
99
135
 
100
136
  # @group Conversion
@@ -183,7 +219,7 @@ class Period
183
219
  end
184
220
 
185
221
  def eql?(other)
186
- return unless other.is_a?(Period)
222
+ return false unless other.is_a?(Period)
187
223
 
188
224
  hash == other.hash
189
225
  end
@@ -284,7 +320,6 @@ class Period
284
320
  quarter
285
321
  half
286
322
  year
287
- irregular
288
323
  ].freeze
289
324
 
290
325
  CHUNK_ORDER = {}
@@ -1,3 +1,3 @@
1
1
  module FatPeriod
2
- VERSION = '2.0.0'.freeze
2
+ VERSION = '2.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: 2.0.0
4
+ version: 2.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: 2024-11-12 00:00:00.000000000 Z
11
+ date: 2024-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fat_core