fat_period 1.3.1 → 1.5.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: a1b3b656730fd4a24f6ec1ae3c14a2828aee91a6ddf18ce8a4481e31e2d55ad3
4
- data.tar.gz: eb16ac79598e7c0054bac2ba57a9a5d8abce25cb8e810cbea9ac59ea4c8ead33
3
+ metadata.gz: fe15db09e24f9ddf7264fdf8862ea51bacb0fb88da8b21727c44ce0c0a4f930c
4
+ data.tar.gz: 0ea19cc89dfa940268938ccf2d8f3abd949e2c1a405d158410c0f928c8c6d64a
5
5
  SHA512:
6
- metadata.gz: 82b0fb1e076cf644da168edbfadfe44a060dc8ebdc43cdf450280a32466fc31a33aee79591a209d480cf192373cfcd8fe808f46e70e0c8186ff9fb10bdf84c66
7
- data.tar.gz: d1a151a0b003ba1bcaa54562928ab42075044ae04bbabca202001c56b1fd697779666c870541f023dddf64e248dd1a1ee2e5eeea5484e2c6f0ee392af469e874
6
+ metadata.gz: e4270dcbd82522aeb3781205e4598b52f15cc2c3b352709b500ffb6db6cd80a5691416d3a3abed2cf132fc0de8d5b05a14e934d14100c984293893cc4b3de378
7
+ data.tar.gz: 82458a341811b97e30393360fc81b02acad7d4b8446ce616b7c84d237e11ec037fa85790d241c02c9874924733ac0895fd648f7b0ec3d83f8de04ccd9eaf1372
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ PATH_add .bundle/bin
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/.simplecov ADDED
@@ -0,0 +1,18 @@
1
+ # -*- mode: ruby -*-
2
+
3
+ SimpleCov.start do
4
+ # any custom configs like groups and filters can be here at a central place
5
+ add_filter '/spec/'
6
+ add_filter '/tmp/'
7
+ add_group "Models", "lib/fat_period"
8
+ # add_group "Core Extension", "lib/ext"
9
+ # After this many seconds between runs, old coverage stats are thrown out,
10
+ # so 3600 => 1 hour
11
+ merge_timeout 3600
12
+ # Make this true to merge rspec and cucumber coverage together
13
+ use_merging false
14
+ command_name 'Rspec'
15
+ nocov_token 'no_cover'
16
+ # Branch coverage
17
+ enable_coverage :branch
18
+ end
data/fat_period.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = 'fat_period'
8
8
  spec.version = FatPeriod::VERSION
9
9
  spec.authors = ['Daniel E. Doherty']
10
- spec.email = ['ded-law@ddoherty.net']
10
+ spec.email = ['ded@ddoherty.net']
11
11
 
12
12
  spec.summary = %q{Implements a Period class as a Range of Dates.}
13
13
  spec.homepage = 'https://github.com/ddoherty03/fat_period'
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'debug', '>= 1.0.0'
27
27
  spec.add_development_dependency 'pry'
28
28
  spec.add_development_dependency 'pry-doc'
29
+ spec.add_development_dependency 'simplecov'
29
30
 
30
- spec.add_runtime_dependency 'fat_core', '>= 4.8.3'
31
+ spec.add_runtime_dependency 'fat_core', '>= 5.1.0'
31
32
  end
@@ -20,6 +20,53 @@ module FatPeriod
20
20
  require 'fat_period'
21
21
  Period.new(beginning_of_chunk(chunk), end_of_chunk(chunk))
22
22
  end
23
+
24
+ # Return a period representing a chunk containing a given Date.
25
+ def day_containing
26
+ Period.new(self, self)
27
+ end
28
+
29
+ def week_containing
30
+ Period.new(self.beginning_of_week, self.end_of_week)
31
+ end
32
+
33
+ def biweek_containing
34
+ Period.new(self.beginning_of_biweek, self.end_of_biweek)
35
+ end
36
+
37
+ def semimonth_containing
38
+ Period.new(self.beginning_of_semimonth, self.end_of_semimonth)
39
+ end
40
+
41
+ def month_containing
42
+ Period.new(self.beginning_of_month, self.end_of_month)
43
+ end
44
+
45
+ def bimonth_containing
46
+ Period.new(self.beginning_of_bimonth, self.end_of_bimonth)
47
+ end
48
+
49
+ def quarter_containing
50
+ Period.new(self.beginning_of_quarter, self.end_of_quarter)
51
+ end
52
+
53
+ def half_containing
54
+ Period.new(self.beginning_of_half, self.end_of_half)
55
+ end
56
+
57
+ def year_containing
58
+ Period.new(self.beginning_of_year, self.end_of_year)
59
+ end
60
+
61
+ def chunk_containing(chunk)
62
+ raise ArgumentError, 'chunk is nil' unless chunk
63
+
64
+ chunk = chunk.to_sym
65
+ raise ArgumentError, "unknown chunk name: #{chunk}" unless CHUNKS.include?(chunk)
66
+
67
+ method = "#{chunk}_containing".to_sym
68
+ send(method, self)
69
+ end
23
70
  end
24
71
  end
25
72
 
@@ -27,13 +27,11 @@ class Period
27
27
  # @raise [ArgumentError] if first date is later than last date
28
28
  # @return [Period]
29
29
  def initialize(first, last)
30
- @first = Date.ensure_date(first).freeze
31
- @last = Date.ensure_date(last).freeze
30
+ @first = Date.ensure(first).freeze
31
+ @last = Date.ensure(last).freeze
32
32
  freeze
33
33
 
34
- return unless @first > @last
35
-
36
- raise ArgumentError, "Period's first date is later than its last date"
34
+ raise ArgumentError, "Period's first date is later than its last date" if @first > @last
37
35
  end
38
36
 
39
37
  # These need to come after initialize is defined
@@ -72,7 +70,7 @@ class Period
72
70
 
73
71
  PHRASE_RE = %r{\A
74
72
  # One or both of from and to parts
75
- ((from\s+(?<from_part>[-_a-z0-9]+)\s*)?
73
+ (((from)?\s*(?<from_part>[-_a-z0-9]+)\s*)?
76
74
  (to\s+(?<to_part>[-_a-z0-9]+))?)
77
75
  # Wholly optional chunk part
78
76
  (\s+per\s+(?<chunk_part>\w+))?\z}xi
@@ -168,6 +166,31 @@ class Period
168
166
  end
169
167
  end
170
168
 
169
+ # Convert a string or Period into a Period object, if the string is parsable
170
+ # as a period.
171
+ #
172
+ # @example
173
+ # Period.ensure('2023-1q') #=> Period 2023-01-01..2023-03-31
174
+ # pd = Period.new('2016-01-01', '2016-12-31')
175
+ # Period.ensure(pd) #=> pd # No effect
176
+ #
177
+ # @return [Period] either parsed String or same Period
178
+ def self.ensure(pd)
179
+ case pd
180
+ when Period
181
+ pd
182
+ when String
183
+ if pd.match?(/\s*from/i)
184
+ # Ignore any chunk modifier, 'per'
185
+ Period.parse_phrase(pd).first
186
+ else
187
+ Period.parse(pd)
188
+ end
189
+ else
190
+ raise UserError, "can't ensure `#{pd}` of #{pd.class} is a Period"
191
+ end
192
+ end
193
+
171
194
  # A concise way to print out Periods for inspection as
172
195
  # 'Period(YYYY-MM-DD..YYYY-MM-DD)'.
173
196
  #
@@ -373,7 +396,7 @@ class Period
373
396
  chunk = chunk.to_sym
374
397
  raise ArgumentError, "unknown chunk name: #{chunk}" unless CHUNKS.include?(chunk)
375
398
 
376
- date = Date.ensure_date(date)
399
+ date = Date.ensure(date)
377
400
  method = "#{chunk}_containing".to_sym
378
401
  send(method, date)
379
402
  end
@@ -1,3 +1,3 @@
1
1
  module FatPeriod
2
- VERSION = '1.3.1'.freeze
2
+ VERSION = '1.5.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.3.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel E. Doherty
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-12 00:00:00.000000000 Z
11
+ date: 2024-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,29 +94,46 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: fat_core
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - ">="
102
116
  - !ruby/object:Gem::Version
103
- version: 4.8.3
117
+ version: 5.1.0
104
118
  type: :runtime
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - ">="
109
123
  - !ruby/object:Gem::Version
110
- version: 4.8.3
111
- description:
124
+ version: 5.1.0
125
+ description:
112
126
  email:
113
- - ded-law@ddoherty.net
127
+ - ded@ddoherty.net
114
128
  executables: []
115
129
  extensions: []
116
130
  extra_rdoc_files: []
117
131
  files:
132
+ - ".envrc"
118
133
  - ".gitignore"
119
134
  - ".rspec"
135
+ - ".ruby-version"
136
+ - ".simplecov"
120
137
  - ".travis.yml"
121
138
  - Gemfile
122
139
  - LICENSE.txt
@@ -132,7 +149,7 @@ files:
132
149
  homepage: https://github.com/ddoherty03/fat_period
133
150
  licenses: []
134
151
  metadata: {}
135
- post_install_message:
152
+ post_install_message:
136
153
  rdoc_options: []
137
154
  require_paths:
138
155
  - lib
@@ -147,8 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
164
  - !ruby/object:Gem::Version
148
165
  version: '0'
149
166
  requirements: []
150
- rubygems_version: 3.3.3
151
- signing_key:
167
+ rubygems_version: 3.5.18
168
+ signing_key:
152
169
  specification_version: 4
153
170
  summary: Implements a Period class as a Range of Dates.
154
171
  test_files: []