fat_period 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fat_period/period.rb +57 -22
- data/lib/fat_period/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 875ad1917477c27f17278fb0d5aad18423f4dcf52f32c9957651c829a1164f4b
|
4
|
+
data.tar.gz: 1495e04adb60e49dfc26f1b94c9699a129f30b7a15c36e9fa82d0e68d87ed183
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7e5ece151032b3ce11aad94225ef649c9e217a2d9239406f3ef030b335169cc33a130ff860b11b65a2e9d90ab4d9f900eb6df713f904beb9d841a7068ebcb96
|
7
|
+
data.tar.gz: 91a94d16666150d0eba4cd4bd5cb24813150d714f2e534a15746bd70119e87996d39940d8da40c5ab7589707975fe53f60a53cd4ae7d9bd58254b710d7d3c4ee
|
data/lib/fat_period/period.rb
CHANGED
@@ -30,9 +30,7 @@ class Period
|
|
30
30
|
@last = Date.ensure_date(last).freeze
|
31
31
|
freeze
|
32
32
|
|
33
|
-
|
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
|
70
|
-
#
|
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.
|
77
|
-
#
|
78
|
-
# Period.
|
79
|
-
#
|
80
|
-
#
|
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
|
-
# @
|
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
|
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
|
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
|
-
|
94
|
-
from_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 = {}
|
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: 2.
|
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-
|
11
|
+
date: 2024-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fat_core
|