fat_period 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -73,6 +73,64 @@ class Period
73
73
  # Period from commercial beginning of time to commercial end of time.
74
74
  FOREVER = Period.new(Date::BOT, Date::EOT)
75
75
 
76
+ # @group Parsing
77
+ #
78
+ # Return a period based on two date specs passed as strings (see
79
+ # `FatCore::Date.parse_spec`), a 'from' and a 'to' spec. The returned period
80
+ # begins on the first day of the period given as the `from` spec and ends on
81
+ # the last day given as the `to` spec. If the to spec is not given or is nil,
82
+ # the from spec is used for both the from- and to-spec.
83
+ #
84
+ # @example
85
+ # Period.parse('2014-11').inspect #=> Period('2014-11-01..2014-11-30')
86
+ # Period.parse('2014-11', '2015-3Q').inspect #=> Period('2014-11-01..2015-09-30')
87
+ # # Assuming this executes in December, 2014
88
+ # Period.parse('last_month', 'this_month').inspect #=> Period('2014-11-01..2014-12-31')
89
+ #
90
+ # @param from [String] spec ala FatCore::Date.parse_spec
91
+ # @param to [String] spec ala FatCore::Date.parse_spec
92
+ # @return [Period] from beginning of `from` to end of `to`
93
+ def self.parse(from, to = nil)
94
+ raise ArgumentError, 'Period.parse missing argument' unless from
95
+
96
+ to ||= from
97
+ first = Date.parse_spec(from, :from)
98
+ second = Date.parse_spec(to, :to)
99
+ Period.new(first, second) if first && second
100
+ end
101
+
102
+ # Return a period as in `Period.parse` from a String phrase in which the from
103
+ # spec is introduced with 'from' and, optionally, the to spec is introduced
104
+ # with 'to'. A phrase with only a to spec is treated the same as one with
105
+ # only a from spec. If neither 'from' nor 'to' appear in phrase, treat the
106
+ # whole string as a from spec.
107
+ #
108
+ # @example
109
+ # Period.parse_phrase('from 2014-11 to 2015-3Q') #=> Period('2014-11-01..2015-09-30')
110
+ # Period.parse_phrase('from 2014-11') #=> Period('2014-11-01..2014-11-30')
111
+ # Period.parse_phrase('from 2015-3Q') #=> Period('2015-09-01..2015-12-31')
112
+ # Period.parse_phrase('to 2015-3Q') #=> Period('2015-09-01..2015-12-31')
113
+ # Period.parse_phrase('2015-3Q') #=> Period('2015-09-01..2015-12-31')
114
+ #
115
+ # @param phrase [String] with 'from <spec> to <spec>'
116
+ # @return [Period] translated from phrase
117
+ def self.parse_phrase(phrase)
118
+ phrase = phrase.clean
119
+ if phrase =~ /\Afrom (.*) to (.*)\z/
120
+ from_phrase = $1
121
+ to_phrase = $2
122
+ elsif phrase =~ /\Afrom (.*)\z/
123
+ from_phrase = $1
124
+ to_phrase = nil
125
+ elsif phrase =~ /\Ato (.*)\z/
126
+ from_phrase = $1
127
+ else
128
+ from_phrase = phrase
129
+ to_phrase = nil
130
+ end
131
+ parse(from_phrase, to_phrase)
132
+ end
133
+
76
134
  # @group Conversion
77
135
 
78
136
  # Convert this Period to a Range.
@@ -206,64 +264,6 @@ class Period
206
264
  (days / days_in_year.to_f).to_f
207
265
  end
208
266
 
209
- # @group Parsing
210
- #
211
- # Return a period based on two date specs passed as strings (see
212
- # `FatCore::Date.parse_spec`), a 'from' and a 'to' spec. The returned period
213
- # begins on the first day of the period given as the `from` spec and ends on
214
- # the last day given as the `to` spec. If the to spec is not given or is nil,
215
- # the from spec is used for both the from- and to-spec.
216
- #
217
- # @example
218
- # Period.parse('2014-11').inspect #=> Period('2014-11-01..2014-11-30')
219
- # Period.parse('2014-11', '2015-3Q').inspect #=> Period('2014-11-01..2015-09-30')
220
- # # Assuming this executes in December, 2014
221
- # Period.parse('last_month', 'this_month').inspect #=> Period('2014-11-01..2014-12-31')
222
- #
223
- # @param from [String] spec ala FatCore::Date.parse_spec
224
- # @param to [String] spec ala FatCore::Date.parse_spec
225
- # @return [Period] from beginning of `from` to end of `to`
226
- def self.parse(from, to = nil)
227
- raise ArgumentError, 'Period.parse missing argument' unless from
228
-
229
- to ||= from
230
- first = Date.parse_spec(from, :from)
231
- second = Date.parse_spec(to, :to)
232
- Period.new(first, second) if first && second
233
- end
234
-
235
- # Return a period as in `Period.parse` from a String phrase in which the from
236
- # spec is introduced with 'from' and, optionally, the to spec is introduced
237
- # with 'to'. A phrase with only a to spec is treated the same as one with
238
- # only a from spec. If neither 'from' nor 'to' appear in phrase, treat the
239
- # whole string as a from spec.
240
- #
241
- # @example
242
- # Period.parse_phrase('from 2014-11 to 2015-3Q') #=> Period('2014-11-01..2015-09-30')
243
- # Period.parse_phrase('from 2014-11') #=> Period('2014-11-01..2014-11-30')
244
- # Period.parse_phrase('from 2015-3Q') #=> Period('2015-09-01..2015-12-31')
245
- # Period.parse_phrase('to 2015-3Q') #=> Period('2015-09-01..2015-12-31')
246
- # Period.parse_phrase('2015-3Q') #=> Period('2015-09-01..2015-12-31')
247
- #
248
- # @param phrase [String] with 'from <spec> to <spec>'
249
- # @return [Period] translated from phrase
250
- def self.parse_phrase(phrase)
251
- phrase = phrase.clean
252
- if phrase =~ /\Afrom (.*) to (.*)\z/
253
- from_phrase = $1
254
- to_phrase = $2
255
- elsif phrase =~ /\Afrom (.*)\z/
256
- from_phrase = $1
257
- to_phrase = nil
258
- elsif phrase =~ /\Ato (.*)\z/
259
- from_phrase = $1
260
- else
261
- from_phrase = phrase
262
- to_phrase = nil
263
- end
264
- parse(from_phrase, to_phrase)
265
- end
266
-
267
267
  # Possibly useful class method to take an array of periods and join all the
268
268
  # contiguous ones, then return an array of the disjoint periods not
269
269
  # contiguous to one another. An array of periods with no gaps should return
@@ -304,6 +304,80 @@ class Period
304
304
  half: (180..183), year: (365..366)
305
305
  }.freeze
306
306
 
307
+ # Return a period representing a chunk containing a given Date.
308
+ def self.day_containing(date)
309
+ Period.new(date, date)
310
+ end
311
+
312
+ def self.week_containing(date)
313
+ Period.new(date.beginning_of_week, date.end_of_week)
314
+ end
315
+
316
+ def self.biweek_containing(date)
317
+ Period.new(date.beginning_of_biweek, date.end_of_biweek)
318
+ end
319
+
320
+ def self.semimonth_containing(date)
321
+ Period.new(date.beginning_of_semimonth, date.end_of_semimonth)
322
+ end
323
+
324
+ def self.month_containing(date)
325
+ Period.new(date.beginning_of_month, date.end_of_month)
326
+ end
327
+
328
+ def self.bimonth_containing(date)
329
+ Period.new(date.beginning_of_bimonth, date.end_of_bimonth)
330
+ end
331
+
332
+ def self.quarter_containing(date)
333
+ Period.new(date.beginning_of_quarter, date.end_of_quarter)
334
+ end
335
+
336
+ def self.half_containing(date)
337
+ Period.new(date.beginning_of_half, date.end_of_half)
338
+ end
339
+
340
+ def self.year_containing(date)
341
+ Period.new(date.beginning_of_year, date.end_of_year)
342
+ end
343
+
344
+ # Return a Period representing a chunk containing today.
345
+ def self.this_day
346
+ day_containing(Date.current)
347
+ end
348
+
349
+ def self.this_week
350
+ week_containing(Date.current)
351
+ end
352
+
353
+ def self.this_biweek
354
+ biweek_containing(Date.current)
355
+ end
356
+
357
+ def self.this_semimonth
358
+ semimonth_containing(Date.current)
359
+ end
360
+
361
+ def self.this_month
362
+ month_containing(Date.current)
363
+ end
364
+
365
+ def self.this_bimonth
366
+ bimonth_containing(Date.current)
367
+ end
368
+
369
+ def self.this_quarter
370
+ quarter_containing(Date.current)
371
+ end
372
+
373
+ def self.this_half
374
+ half_containing(Date.current)
375
+ end
376
+
377
+ def self.this_year
378
+ year_containing(Date.current)
379
+ end
380
+
307
381
  # Return the chunk symbol represented by this period if it covers a single
308
382
  # calendar period; otherwise return :irregular.
309
383
  #
@@ -1,3 +1,3 @@
1
1
  module FatPeriod
2
- VERSION = '1.0.2'.freeze
2
+ VERSION = '1.0.3'.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.0.2
4
+ version: 1.0.3
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: 2019-12-29 00:00:00.000000000 Z
11
+ date: 2020-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  - !ruby/object:Gem::Version
148
148
  version: '0'
149
149
  requirements: []
150
- rubygems_version: 3.1.2
150
+ rubygems_version: 3.0.3
151
151
  signing_key:
152
152
  specification_version: 4
153
153
  summary: Implements a Period class as a Range of Dates.