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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 609274b0338a91caf02d5fbf0ed37b4d2621dea51e93a393f9bde86e5287b0ad
4
- data.tar.gz: cddf0aa8c7087d0705917319090dab8fea710579b697cc79d231f0257a80271d
3
+ metadata.gz: cb2c6b600f446fa2bff8080b8056a32ceb3fda88be68c03db076591753c4992e
4
+ data.tar.gz: f4d65daa079f338c2a1a7ab5a0d326ae9af884d819a72077f1f24e8128ed70de
5
5
  SHA512:
6
- metadata.gz: 3da42e489a2dc3a1d8b42191e589e675a71466048a70b1b2d04f430b2f3f34685f0c137194952a7c4ec33c117cf227b1143670409353351af9b8531492efc93b
7
- data.tar.gz: e91123fa17ff8a9f47553652a302316418aaf2f5fe5eee587b45ecb9b27799ad20b39b9e1af350e4453ee69f19f4a8cd6a595ed82df2247c5bc8084c664768de
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
@@ -70,35 +70,59 @@ class Period
70
70
  Period.new(first, second) if first && second
71
71
  end
72
72
 
73
- # Return a period as in `Period.parse` from a String phrase in which the from
74
- # spec is introduced with 'from' and, optionally, the to spec is introduced
75
- # with 'to'. A phrase with only a to spec is treated the same as one with
76
- # only a from spec. If neither 'from' nor 'to' appear in phrase, treat the
77
- # whole string as a from spec.
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') #=> Period('2015-09-01..2015-12-31')
85
- #
86
- # @param phrase [String] with 'from <spec> to <spec>'
87
- # @return [Period] translated from phrase
88
- def self.parse_phrase(phrase)
89
- phrase = phrase.clean
90
- case phrase
91
- when /\Afrom (.*) to (.*)\z/
92
- from_phrase = $1
93
- to_phrase = $2
94
- when /\Afrom (.*)\z/, /\Ato (.*)\z/
95
- from_phrase = $1
96
- to_phrase = nil
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
- from_phrase = phrase
99
- to_phrase = nil
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 < last
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
@@ -1,3 +1,3 @@
1
1
  module FatPeriod
2
- VERSION = '1.2.1'.freeze
2
+ VERSION = '1.3.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.2.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: 2021-12-27 00:00:00.000000000 Z
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: pry
56
+ name: debug
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
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: '0'
68
+ version: 1.0.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: pry-doc
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-byebug
84
+ name: pry-doc
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="