fat_core 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/fat_core.gemspec +1 -1
- data/lib/fat_core/date.rb +67 -0
- data/lib/fat_core/version.rb +2 -2
- data/spec/lib/date_spec.rb +38 -0
- data/spec/lib/period_spec.rb +33 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66c342d6384a15e67820f179df257c234d77810d
|
4
|
+
data.tar.gz: 23a1da18e31c2f77adbd4455206dfe1b54326bb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d91ffaf07b1f4353dead0e81b03da11cd311a1a95fc3a0ed92f488336c85b4cbbfd92ee40d4db05ab165f93a7e0b6c6655373bd5963ecc00fbce90a6bac34fb5
|
7
|
+
data.tar.gz: 2f8d2eff64ae74ad5d51f2d2cdc3493a514f21464b4efd5d33881566f8003df7fb48e121b78055f7c995e2696fe565a193fa6cf4dcd40336a53e718b20eaace8
|
data/fat_core.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "fat_core"
|
8
8
|
spec.version = FatCore::VERSION
|
9
9
|
spec.authors = ["Daniel E. Doherty"]
|
10
|
-
spec.email = ["ded
|
10
|
+
spec.email = ["ded@ddoherty.net"]
|
11
11
|
spec.summary = %q{fat_core provides some useful core extensions}
|
12
12
|
spec.description = %q{Write a longer description. Optional.}
|
13
13
|
spec.homepage = ""
|
data/lib/fat_core/date.rb
CHANGED
@@ -104,6 +104,22 @@ class Date
|
|
104
104
|
quarter = $1.to_i
|
105
105
|
date = Date.new(this_year, quarter * 3, 15)
|
106
106
|
spec_type == :from ? date.beginning_of_quarter : date.end_of_quarter
|
107
|
+
when /^(\d\d\d\d)-(\d)[Hh]$/, /^(\d\d\d\d)-[Hh](\d)$/
|
108
|
+
# Year-Half
|
109
|
+
year = $1.to_i
|
110
|
+
half = $2.to_i
|
111
|
+
unless [1, 2].include?(half)
|
112
|
+
raise ArgumentError, "bad date format: #{spec}"
|
113
|
+
end
|
114
|
+
month = half * 6
|
115
|
+
spec_type == :from ? Date.new(year, month, 15).beginning_of_half :
|
116
|
+
Date.new(year, month, 1).end_of_half
|
117
|
+
when /^([12])[hH]$/, /^[hH]([12])$/
|
118
|
+
# Half only
|
119
|
+
this_year = today.year
|
120
|
+
half = $1.to_i
|
121
|
+
date = Date.new(this_year, half * 6, 15)
|
122
|
+
spec_type == :from ? date.beginning_of_half : date.end_of_half
|
107
123
|
when /^(\d\d\d\d)-(\d\d?)*$/
|
108
124
|
# Year-Month only
|
109
125
|
spec_type == :from ? Date.new($1.to_i, $2.to_i, 1) :
|
@@ -149,6 +165,11 @@ class Date
|
|
149
165
|
when /^last_?quarter/
|
150
166
|
spec_type == :from ? (today - 3.months).beginning_of_quarter :
|
151
167
|
(today - 3.months).end_of_quarter
|
168
|
+
when /^(this_?)?half/
|
169
|
+
spec_type == :from ? today.beginning_of_half : today.end_of_half
|
170
|
+
when /^last_?half/
|
171
|
+
spec_type == :from ? (today - 6.months).beginning_of_half :
|
172
|
+
(today - 6.months).end_of_half
|
152
173
|
when /^(this_?)?year/
|
153
174
|
spec_type == :from ? today.beginning_of_year : today.end_of_year
|
154
175
|
when /^last_?year/
|
@@ -216,6 +237,16 @@ class Date
|
|
216
237
|
!weekend?
|
217
238
|
end
|
218
239
|
|
240
|
+
# Self's calendar half: 1 or 2
|
241
|
+
def half
|
242
|
+
case month
|
243
|
+
when (1..6)
|
244
|
+
1
|
245
|
+
when (7..12)
|
246
|
+
2
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
219
250
|
# Self's calendar quarter: 1, 2, 3, or 4
|
220
251
|
def quarter
|
221
252
|
case month
|
@@ -230,6 +261,28 @@ class Date
|
|
230
261
|
end
|
231
262
|
end
|
232
263
|
|
264
|
+
# The date that is the first day of the half-year in which self falls.
|
265
|
+
def beginning_of_half
|
266
|
+
if month > 9
|
267
|
+
(beginning_of_quarter - 15).beginning_of_quarter
|
268
|
+
elsif month > 6
|
269
|
+
beginning_of_quarter
|
270
|
+
else
|
271
|
+
beginning_of_year
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
# The date that is the last day of the half-year in which self falls.
|
276
|
+
def end_of_half
|
277
|
+
if month < 4
|
278
|
+
(end_of_quarter + 15).end_of_quarter
|
279
|
+
elsif month < 7
|
280
|
+
end_of_quarter
|
281
|
+
else
|
282
|
+
end_of_year
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
233
286
|
# The date that is the first day of the bimonth in which self
|
234
287
|
# falls. A 'bimonth' is a two-month calendar period beginning on the
|
235
288
|
# first day of the odd-numbered months. E.g., 2014-01-01 to
|
@@ -304,6 +357,14 @@ class Date
|
|
304
357
|
end_of_year == self
|
305
358
|
end
|
306
359
|
|
360
|
+
def beginning_of_half?
|
361
|
+
beginning_of_half == self
|
362
|
+
end
|
363
|
+
|
364
|
+
def end_of_half?
|
365
|
+
end_of_half == self
|
366
|
+
end
|
367
|
+
|
307
368
|
def beginning_of_quarter?
|
308
369
|
beginning_of_quarter == self
|
309
370
|
end
|
@@ -363,6 +424,8 @@ class Date
|
|
363
424
|
case chunk
|
364
425
|
when :year
|
365
426
|
next_year
|
427
|
+
when :half
|
428
|
+
next_month(6)
|
366
429
|
when :quarter
|
367
430
|
next_month(3)
|
368
431
|
when :bimonth
|
@@ -386,6 +449,8 @@ class Date
|
|
386
449
|
case sym
|
387
450
|
when :year
|
388
451
|
beginning_of_year
|
452
|
+
when :half
|
453
|
+
beginning_of_half
|
389
454
|
when :quarter
|
390
455
|
beginning_of_quarter
|
391
456
|
when :bimonth
|
@@ -409,6 +474,8 @@ class Date
|
|
409
474
|
case sym
|
410
475
|
when :year
|
411
476
|
end_of_year
|
477
|
+
when :half
|
478
|
+
end_of_half
|
412
479
|
when :quarter
|
413
480
|
end_of_quarter
|
414
481
|
when :bimonth
|
data/lib/fat_core/version.rb
CHANGED
data/spec/lib/date_spec.rb
CHANGED
@@ -237,6 +237,22 @@ describe Date do
|
|
237
237
|
}.to raise_error
|
238
238
|
end
|
239
239
|
|
240
|
+
it "should parse year-half specs such as YYYY-NH or YYYY-HN" do
|
241
|
+
expect(Date.parse_spec('2011-2H', :from)).to eq Date.parse('2011-07-01')
|
242
|
+
expect(Date.parse_spec('2011-2H', :to)).to eq Date.parse('2011-12-31')
|
243
|
+
expect(Date.parse_spec('2011-H1', :from)).to eq Date.parse('2011-01-01')
|
244
|
+
expect(Date.parse_spec('2011-H1', :to)).to eq Date.parse('2011-06-30')
|
245
|
+
expect { Date.parse_spec('2011-3H') }.to raise_error
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should parse half-only specs such as NQ or QN" do
|
249
|
+
expect(Date.parse_spec('2H', :from)).to eq Date.parse('2012-07-01')
|
250
|
+
expect(Date.parse_spec('H2', :to)).to eq Date.parse('2012-12-31')
|
251
|
+
expect(Date.parse_spec('1H', :from)).to eq Date.parse('2012-01-01')
|
252
|
+
expect(Date.parse_spec('H1', :to)).to eq Date.parse('2012-06-30')
|
253
|
+
expect { Date.parse_spec('8H') }.to raise_error
|
254
|
+
end
|
255
|
+
|
240
256
|
it "should parse year-quarter specs such as YYYY-NQ or YYYY-QN" do
|
241
257
|
expect(Date.parse_spec('2011-4Q', :from)).to eq Date.parse('2011-10-01')
|
242
258
|
expect(Date.parse_spec('2011-4Q', :to)).to eq Date.parse('2011-12-31')
|
@@ -333,6 +349,14 @@ describe Date do
|
|
333
349
|
expect(Date.parse_spec('last_quarter', :to)).to eq Date.parse('2012-06-30')
|
334
350
|
end
|
335
351
|
|
352
|
+
# Today is set to '2012-07-18'
|
353
|
+
it "should parse relative halves: this_half, last_half" do
|
354
|
+
expect(Date.parse_spec('this_half')).to eq Date.parse('2012-07-01')
|
355
|
+
expect(Date.parse_spec('this_half', :to)).to eq Date.parse('2012-12-31')
|
356
|
+
expect(Date.parse_spec('last_half')).to eq Date.parse('2012-01-01')
|
357
|
+
expect(Date.parse_spec('last_half', :to)).to eq Date.parse('2012-06-30')
|
358
|
+
end
|
359
|
+
|
336
360
|
it "should parse relative years: this_year, last_year" do
|
337
361
|
expect(Date.parse_spec('this_year')).to eq Date.parse('2012-01-01')
|
338
362
|
expect(Date.parse_spec('this_year', :to)).to eq Date.parse('2012-12-31')
|
@@ -426,6 +450,16 @@ describe Date do
|
|
426
450
|
expect(Date.parse('2013-12-30')).to_not be_end_of_year
|
427
451
|
end
|
428
452
|
|
453
|
+
it "should know about halves" do
|
454
|
+
expect(Date.parse('2013-01-01')).to be_beginning_of_half
|
455
|
+
expect(Date.parse('2013-12-31')).to be_end_of_half
|
456
|
+
expect(Date.parse('2013-07-01')).to be_beginning_of_half
|
457
|
+
expect(Date.parse('2013-06-30')).to be_end_of_half
|
458
|
+
expect(Date.parse('2013-05-01')).to_not be_beginning_of_half
|
459
|
+
expect(Date.parse('2013-05-01').half).to eq(1)
|
460
|
+
expect(Date.parse('2013-07-31').half).to eq(2)
|
461
|
+
end
|
462
|
+
|
429
463
|
it "should know about quarters" do
|
430
464
|
expect(Date.parse('2013-01-01')).to be_beginning_of_quarter
|
431
465
|
expect(Date.parse('2013-12-31')).to be_end_of_quarter
|
@@ -486,6 +520,7 @@ describe Date do
|
|
486
520
|
|
487
521
|
it "should know the beginning of chunks" do
|
488
522
|
expect(Date.parse('2013-11-04').beginning_of_chunk(:year)).to eq Date.parse('2013-01-01')
|
523
|
+
expect(Date.parse('2013-11-04').beginning_of_chunk(:half)).to eq Date.parse('2013-07-01')
|
489
524
|
expect(Date.parse('2013-11-04').beginning_of_chunk(:quarter)).to eq Date.parse('2013-10-01')
|
490
525
|
expect(Date.parse('2013-12-04').beginning_of_chunk(:bimonth)).to eq Date.parse('2013-11-01')
|
491
526
|
expect(Date.parse('2013-11-04').beginning_of_chunk(:month)).to eq Date.parse('2013-11-01')
|
@@ -501,6 +536,7 @@ describe Date do
|
|
501
536
|
it "should be able to add a chuck sym to itself" do
|
502
537
|
# Date.today is '2012-07-18'
|
503
538
|
expect(Date.today.add_chunk(:year)).to eq(Date.parse('2013-07-18'))
|
539
|
+
expect(Date.today.add_chunk(:half)).to eq(Date.parse('2013-01-18'))
|
504
540
|
expect(Date.today.add_chunk(:quarter)).to eq(Date.parse('2012-10-18'))
|
505
541
|
expect(Date.today.add_chunk(:bimonth)).to eq(Date.parse('2012-09-18'))
|
506
542
|
expect(Date.today.add_chunk(:month)).to eq(Date.parse('2012-08-18'))
|
@@ -516,6 +552,7 @@ describe Date do
|
|
516
552
|
|
517
553
|
it "should know the end of chunks" do
|
518
554
|
expect(Date.parse('2013-07-04').end_of_chunk(:year)).to eq Date.parse('2013-12-31')
|
555
|
+
expect(Date.parse('2013-05-04').end_of_chunk(:half)).to eq Date.parse('2013-06-30')
|
519
556
|
expect(Date.parse('2013-07-04').end_of_chunk(:quarter)).to eq Date.parse('2013-09-30')
|
520
557
|
expect(Date.parse('2013-12-04').end_of_chunk(:bimonth)).to eq Date.parse('2013-12-31')
|
521
558
|
expect(Date.parse('2013-07-04').end_of_chunk(:month)).to eq Date.parse('2013-07-31')
|
@@ -530,6 +567,7 @@ describe Date do
|
|
530
567
|
|
531
568
|
it "should know how to expand to chunk periods" do
|
532
569
|
expect(Date.parse('2013-07-04').expand_to_period(:year)).to eq Period.new('2013-01-01', '2013-12-31')
|
570
|
+
expect(Date.parse('2013-07-04').expand_to_period(:half)).to eq Period.new('2013-07-01', '2013-12-31')
|
533
571
|
expect(Date.parse('2013-07-04').expand_to_period(:quarter)).to eq Period.new('2013-07-01', '2013-09-30')
|
534
572
|
expect(Date.parse('2013-07-04').expand_to_period(:bimonth)).to eq Period.new('2013-07-01', '2013-08-31')
|
535
573
|
expect(Date.parse('2013-07-04').expand_to_period(:month)).to eq Period.new('2013-07-01', '2013-07-31')
|
data/spec/lib/period_spec.rb
CHANGED
@@ -41,7 +41,7 @@ describe Period do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
describe
|
44
|
+
describe 'class methods' do
|
45
45
|
it 'should be able to parse a period spec' do
|
46
46
|
pd = Period.parse
|
47
47
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
@@ -51,6 +51,38 @@ describe Period do
|
|
51
51
|
expect(pd.first).to eq(Date.parse('2012-07-01'))
|
52
52
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
53
53
|
|
54
|
+
pd = Period.parse('from 2Q')
|
55
|
+
expect(pd.first).to eq(Date.parse('2012-04-01'))
|
56
|
+
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
57
|
+
|
58
|
+
pd = Period.parse('to 3Q')
|
59
|
+
expect(pd.first).to eq(Date.parse('2012-07-01'))
|
60
|
+
expect(pd.last).to eq(Date.parse('2012-09-30'))
|
61
|
+
|
62
|
+
pd = Period.parse('to 2012-2Q')
|
63
|
+
expect(pd.first).to eq(Date.parse('2012-04-01'))
|
64
|
+
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
65
|
+
|
66
|
+
pd = Period.parse('from 2012-1Q')
|
67
|
+
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
68
|
+
expect(pd.last).to eq(Date.parse('2012-03-31'))
|
69
|
+
|
70
|
+
pd = Period.parse('from 2H')
|
71
|
+
expect(pd.first).to eq(Date.parse('2012-07-01'))
|
72
|
+
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
73
|
+
|
74
|
+
pd = Period.parse('to 1H')
|
75
|
+
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
76
|
+
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
77
|
+
|
78
|
+
pd = Period.parse('to 2012-2H')
|
79
|
+
expect(pd.first).to eq(Date.parse('2012-07-01'))
|
80
|
+
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
81
|
+
|
82
|
+
pd = Period.parse('from 2012-1H')
|
83
|
+
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
84
|
+
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
85
|
+
|
54
86
|
pd = Period.parse('to 2012')
|
55
87
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
56
88
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fat_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.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: 2016-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|
@@ -166,7 +166,7 @@ dependencies:
|
|
166
166
|
version: '0'
|
167
167
|
description: Write a longer description. Optional.
|
168
168
|
email:
|
169
|
-
- ded
|
169
|
+
- ded@ddoherty.net
|
170
170
|
executables:
|
171
171
|
- easters
|
172
172
|
extensions: []
|