fat_period 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/fat_period.gemspec +1 -1
- data/lib/fat_period/period.rb +53 -26
- data/lib/fat_period/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb2c6b600f446fa2bff8080b8056a32ceb3fda88be68c03db076591753c4992e
|
4
|
+
data.tar.gz: f4d65daa079f338c2a1a7ab5a0d326ae9af884d819a72077f1f24e8128ed70de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3059659aed2c2000401a6fceca177b9b7ea633adaaa1012bcb20db1069845bdc7dff35b97a53289a498399d1bc2217ceffd06c4dae94377ce7a47710c71ab2af
|
7
|
+
data.tar.gz: ab0e1a8aebca6fc8acf0e952e9a48956767b03e7cd9071d6487b52e0a544519e6801b927ae3b2f5021e1fc3ac8aeb95114488192fcd343b265b235efc38a6784
|
data/fat_period.gemspec
CHANGED
@@ -23,9 +23,9 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency 'bundler'
|
24
24
|
spec.add_development_dependency 'rake'
|
25
25
|
spec.add_development_dependency 'rspec'
|
26
|
+
spec.add_development_dependency 'debug', '>= 1.0.0'
|
26
27
|
spec.add_development_dependency 'pry'
|
27
28
|
spec.add_development_dependency 'pry-doc'
|
28
|
-
spec.add_development_dependency 'pry-byebug'
|
29
29
|
|
30
30
|
spec.add_runtime_dependency 'fat_core', '>= 4.8.3'
|
31
31
|
end
|
data/lib/fat_period/period.rb
CHANGED
@@ -70,35 +70,59 @@ class Period
|
|
70
70
|
Period.new(first, second) if first && second
|
71
71
|
end
|
72
72
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
73
|
+
PHRASE_RE = %r{\A
|
74
|
+
# One or both of from and to parts
|
75
|
+
((from\s+(?<from_part>[-_a-z0-9]+)\s*)?
|
76
|
+
(to\s+(?<to_part>[-_a-z0-9]+))?)
|
77
|
+
# Wholly optional chunk part
|
78
|
+
(\s+per\s+(?<chunk_part>\w+))?\z}xi
|
79
|
+
|
80
|
+
# Return an array of periods, either a single period as in `Period.parse`
|
81
|
+
# from a String phrase in which a `from spec` is introduced with 'from' and,
|
82
|
+
# optionally, a `to spec` is introduced with 'to', or a number of periods if
|
83
|
+
# there is a 'per <chunk>' modifier. A phrase with only a `to spec` is
|
84
|
+
# treated the same as one with only a from spec. If neither 'from' nor 'to'
|
85
|
+
# appear in phrase, treat the whole string as a from spec.
|
78
86
|
#
|
79
87
|
# @example
|
80
|
-
# Period.parse_phrase('from 2014-11 to 2015-3Q') #=> Period('2014-11-01..2015-09-30')
|
81
|
-
# Period.parse_phrase('from 2014-11') #=> Period('2014-11-01..2014-11-30')
|
82
|
-
# Period.parse_phrase('from 2015-3Q') #=> Period('2015-09-01..2015-12-31')
|
83
|
-
# Period.parse_phrase('to 2015-3Q') #=> Period('2015-09-01..2015-12-31')
|
84
|
-
# Period.parse_phrase('2015-3Q')
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
88
|
+
# Period.parse_phrase('from 2014-11 to 2015-3Q') #=> [Period('2014-11-01..2015-09-30')]
|
89
|
+
# Period.parse_phrase('from 2014-11') #=> [Period('2014-11-01..2014-11-30')]
|
90
|
+
# Period.parse_phrase('from 2015-3Q') #=> [Period('2015-09-01..2015-12-31')]
|
91
|
+
# Period.parse_phrase('to 2015-3Q') #=> [Period('2015-09-01..2015-12-31')]
|
92
|
+
# Period.parse_phrase('from 2015-3Q') #=> [Period('2015-09-01..2015-12-31')]
|
93
|
+
# Period.parse_phrase('from 2015 per month') #=> [
|
94
|
+
# Period('2015-01-01..2015-01-31'),
|
95
|
+
# Period('2015-02-01..2015-02-28'),
|
96
|
+
# ...
|
97
|
+
# Period('2015-12-01..2015-12-31')
|
98
|
+
# ]
|
99
|
+
#
|
100
|
+
# @param phrase [String] with 'from <spec> to <spec> [per <chunk>]'
|
101
|
+
# @return [Array<Period>] translated from phrase
|
102
|
+
def self.parse_phrase(phrase,
|
103
|
+
partial_first: false, partial_last: false, round_up_last: false)
|
104
|
+
phrase = phrase.downcase.clean
|
105
|
+
mm = phrase.match(PHRASE_RE)
|
106
|
+
raise ArgumentError, "invalid period phrase: `#{phrase}`" unless mm
|
107
|
+
|
108
|
+
if mm[:from_part] && mm[:to_part].nil?
|
109
|
+
from_part = mm[:from_part]
|
110
|
+
to_part = nil
|
111
|
+
elsif mm[:from_part].nil? && mm[:to_part]
|
112
|
+
from_part = mm[:to_part]
|
113
|
+
to_part = nil
|
97
114
|
else
|
98
|
-
|
99
|
-
|
115
|
+
from_part = mm[:from_part]
|
116
|
+
to_part = mm[:to_part]
|
117
|
+
end
|
118
|
+
|
119
|
+
whole_period = parse(from_part, to_part)
|
120
|
+
if mm[:chunk_part].nil?
|
121
|
+
[whole_period]
|
122
|
+
else
|
123
|
+
whole_period.chunks(size: mm[:chunk_part], partial_first: partial_first,
|
124
|
+
partial_last: partial_last, round_up_last: round_up_last)
|
100
125
|
end
|
101
|
-
parse(from_phrase, to_phrase)
|
102
126
|
end
|
103
127
|
|
104
128
|
# @group Conversion
|
@@ -606,6 +630,7 @@ class Period
|
|
606
630
|
return result
|
607
631
|
end
|
608
632
|
|
633
|
+
# The first chunk
|
609
634
|
chunk_start = first.dup
|
610
635
|
chunk_end = chunk_start.end_of_chunk(chunk_size)
|
611
636
|
if chunk_start.beginning_of_chunk?(chunk_size) || partial_first
|
@@ -614,14 +639,16 @@ class Period
|
|
614
639
|
end
|
615
640
|
chunk_start = chunk_end + 1.day
|
616
641
|
chunk_end = chunk_start.end_of_chunk(chunk_size)
|
642
|
+
|
617
643
|
# Add Whole chunks
|
618
644
|
while chunk_end <= last
|
619
645
|
result << Period.new(chunk_start, chunk_end)
|
620
646
|
chunk_start = chunk_end + 1.day
|
621
647
|
chunk_end = chunk_start.end_of_chunk(chunk_size)
|
622
648
|
end
|
649
|
+
|
623
650
|
# Possibly append the final chunk to result
|
624
|
-
if chunk_start
|
651
|
+
if chunk_start <= last
|
625
652
|
if round_up_last
|
626
653
|
result << Period.new(chunk_start, chunk_end)
|
627
654
|
elsif partial_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.
|
4
|
+
version: 1.3.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:
|
11
|
+
date: 2022-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,21 +53,21 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: debug
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 1.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name: pry
|
70
|
+
name: pry
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name: pry-
|
84
|
+
name: pry-doc
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|