fat_core 0.6.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/lib/fat_core/period.rb +26 -32
- data/lib/fat_core/version.rb +2 -2
- data/spec/lib/period_spec.rb +57 -58
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 159f439b8087d5ac894339f4d0abeb7776746576
|
4
|
+
data.tar.gz: 9412fdcf88d040555fde70a069d095f175e2b11b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5a2bd9cb94bdc02660b5ab2ff63d15f10edb70b72c2a8fcf247ee35bd6b4ce51afc1be964da1a7ca2362f97334f60ff72eb8d44bdcadd8e8ef333988a5f3536
|
7
|
+
data.tar.gz: dd375a32bf06b4d9951074e860176198df4c79903984c5fb90f8bc663c5d6cb02b3f979f35795670f1b2f0d94e984f1cdf25e414415c1525dc56d06068db32d2
|
data/.gitignore
CHANGED
data/lib/fat_core/period.rb
CHANGED
@@ -123,44 +123,38 @@ class Period
|
|
123
123
|
select(&:nyse_workday?)
|
124
124
|
end
|
125
125
|
|
126
|
-
# Return a period based on two date specs
|
127
|
-
# and a 'to' spec. If the to-spec is not given
|
128
|
-
# used for both the from- and to-spec.
|
129
|
-
#
|
130
|
-
|
126
|
+
# Return a period based on two date specs passed as strings (see
|
127
|
+
# Date.parse_spec), a '''from' and a 'to' spec. If the to-spec is not given
|
128
|
+
# or is nil, the from-spec is used for both the from- and to-spec.
|
129
|
+
#
|
130
|
+
# Period.parse('2014-11') => Period.new('2014-11-01', 2014-11-30')
|
131
|
+
# Period.parse('2014-11', '2015-3Q')
|
132
|
+
# => Period.new('2014-11-01', 2015-09-30')
|
133
|
+
def self.parse(from, to = nil)
|
134
|
+
raise ArgumentError, 'Period.parse missing argument' unless from
|
131
135
|
to ||= from
|
132
|
-
from ||= to
|
133
136
|
Period.new(Date.parse_spec(from, :from), Date.parse_spec(to, :to))
|
134
137
|
end
|
135
138
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
spec = spec.clean
|
152
|
-
if spec =~ /\Afrom (.*) to (.*)\z/
|
153
|
-
from_spec = $~[1]
|
154
|
-
to_spec = $~[2]
|
155
|
-
elsif spec =~ /\Afrom (.*)\z/
|
156
|
-
from_spec = $~[1]
|
157
|
-
to_spec = nil
|
158
|
-
elsif spec =~ /\Ato (.*)\z/
|
159
|
-
to_spec = $~[1]
|
139
|
+
# Return a period from a phrase in which the from date is introduced with
|
140
|
+
# 'from' and, optionally, the to-date is introduced with 'to'.
|
141
|
+
#
|
142
|
+
# Period.parse_phrase('from 2014-11 to 2015-3Q')
|
143
|
+
# => Period('2014-11-01', '2015-09-30')
|
144
|
+
def self.parse_phrase(phrase)
|
145
|
+
phrase = phrase.clean
|
146
|
+
if phrase =~ /\Afrom (.*) to (.*)\z/
|
147
|
+
from_phrase = $~[1]
|
148
|
+
to_phrase = $~[2]
|
149
|
+
elsif phrase =~ /\Afrom (.*)\z/
|
150
|
+
from_phrase = $~[1]
|
151
|
+
to_phrase = nil
|
152
|
+
elsif phrase =~ /\Ato (.*)\z/
|
153
|
+
from_phrase = $~[1]
|
160
154
|
else
|
161
|
-
|
155
|
+
from_phrase = phrase
|
162
156
|
end
|
163
|
-
|
157
|
+
parse(from_phrase, to_phrase)
|
164
158
|
end
|
165
159
|
|
166
160
|
# Possibly useful class method to take an array of periods and join all the
|
data/lib/fat_core/version.rb
CHANGED
data/spec/lib/period_spec.rb
CHANGED
@@ -42,77 +42,76 @@ describe Period do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
describe 'class methods' do
|
45
|
-
it 'should be able to parse a period
|
46
|
-
pd = Period.
|
45
|
+
it 'should be able to parse a period phrase' do
|
46
|
+
pd = Period.parse_phrase('from this_year')
|
47
47
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
48
48
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
49
49
|
|
50
|
-
pd = Period.
|
50
|
+
pd = Period.parse_phrase('from 2012-07 to 2012')
|
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.
|
54
|
+
pd = Period.parse_phrase('from 2Q')
|
55
55
|
expect(pd.first).to eq(Date.parse('2012-04-01'))
|
56
56
|
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
57
57
|
|
58
|
-
pd = Period.
|
58
|
+
pd = Period.parse_phrase('to 3Q')
|
59
59
|
expect(pd.first).to eq(Date.parse('2012-07-01'))
|
60
60
|
expect(pd.last).to eq(Date.parse('2012-09-30'))
|
61
61
|
|
62
|
-
pd = Period.
|
62
|
+
pd = Period.parse_phrase('to 2012-2Q')
|
63
63
|
expect(pd.first).to eq(Date.parse('2012-04-01'))
|
64
64
|
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
65
65
|
|
66
|
-
pd = Period.
|
66
|
+
pd = Period.parse_phrase('from 2012-1Q')
|
67
67
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
68
68
|
expect(pd.last).to eq(Date.parse('2012-03-31'))
|
69
69
|
|
70
|
-
pd = Period.
|
70
|
+
pd = Period.parse_phrase('from 2H')
|
71
71
|
expect(pd.first).to eq(Date.parse('2012-07-01'))
|
72
72
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
73
73
|
|
74
|
-
pd = Period.
|
74
|
+
pd = Period.parse_phrase('to 1H')
|
75
75
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
76
76
|
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
77
77
|
|
78
|
-
pd = Period.
|
78
|
+
pd = Period.parse_phrase('to 2012-2H')
|
79
79
|
expect(pd.first).to eq(Date.parse('2012-07-01'))
|
80
80
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
81
81
|
|
82
|
-
pd = Period.
|
82
|
+
pd = Period.parse_phrase('from 2012-1H')
|
83
83
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
84
84
|
expect(pd.last).to eq(Date.parse('2012-06-30'))
|
85
85
|
|
86
|
-
pd = Period.
|
86
|
+
pd = Period.parse_phrase('to 2012')
|
87
87
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
88
88
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
89
89
|
|
90
|
-
pd = Period.
|
90
|
+
pd = Period.parse_phrase('from 2012')
|
91
91
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
92
92
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
93
93
|
|
94
|
-
pd = Period.
|
94
|
+
pd = Period.parse_phrase('2012')
|
95
95
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
96
96
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
97
97
|
|
98
|
-
pd = Period.
|
98
|
+
pd = Period.parse_phrase('this_year')
|
99
99
|
expect(pd.first).to eq(Date.parse('2012-01-01'))
|
100
100
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
101
101
|
|
102
|
-
pd = Period.
|
102
|
+
pd = Period.parse_phrase('from last_year to this_year')
|
103
103
|
expect(pd.first).to eq(Date.parse('2011-01-01'))
|
104
104
|
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
105
105
|
|
106
|
-
pd = Period.
|
106
|
+
pd = Period.parse_phrase('from last_year to this_year')
|
107
107
|
expect(pd.first).to eq(Date.parse('2011-01-01'))
|
108
|
-
expect(pd.last).to eq(Date.parse('2012-
|
108
|
+
expect(pd.last).to eq(Date.parse('2012-12-31'))
|
109
109
|
end
|
110
110
|
|
111
111
|
it 'should know how to parse a pair of date specs' do
|
112
|
-
expect(Period.
|
113
|
-
expect(Period.
|
114
|
-
expect(Period.
|
115
|
-
expect(Period.parse_spec(nil, '2014-3Q').last).to eq Date.parse('2014-09-30')
|
112
|
+
expect(Period.parse('2014-3Q').first).to eq Date.parse('2014-07-01')
|
113
|
+
expect(Period.parse('2014-3Q').last).to eq Date.parse('2014-09-30')
|
114
|
+
expect(Period.parse('2014-3Q').last).to eq Date.parse('2014-09-30')
|
116
115
|
end
|
117
116
|
|
118
117
|
it 'should know what the valid chunk syms are' do
|
@@ -289,13 +288,13 @@ describe Period do
|
|
289
288
|
end
|
290
289
|
|
291
290
|
it "should be able to enumerate its days" do
|
292
|
-
Period.
|
291
|
+
Period.parse('2014-12').each do |dy|
|
293
292
|
expect(dy.class).to eq Date
|
294
293
|
end
|
295
294
|
end
|
296
295
|
|
297
296
|
it "should be able to return the trading days within period" do
|
298
|
-
tds = Period.
|
297
|
+
tds = Period.parse('2014-12').trading_days
|
299
298
|
expect(tds.count).to eq(22)
|
300
299
|
end
|
301
300
|
|
@@ -500,62 +499,62 @@ describe Period do
|
|
500
499
|
end
|
501
500
|
|
502
501
|
it "should know if it's a subset of another period" do
|
503
|
-
year = Period.
|
504
|
-
month = Period.
|
502
|
+
year = Period.parse('this_year')
|
503
|
+
month = Period.parse('this_month')
|
505
504
|
expect(month.subset_of?(year)).to be true
|
506
505
|
expect(year.subset_of?(year)).to be true
|
507
506
|
end
|
508
507
|
|
509
508
|
it "should know if it's a proper subset of another period" do
|
510
|
-
year = Period.
|
511
|
-
month = Period.
|
509
|
+
year = Period.parse('this_year')
|
510
|
+
month = Period.parse('this_month')
|
512
511
|
expect(month.proper_subset_of?(year)).to be true
|
513
512
|
expect(year.proper_subset_of?(year)).to be false
|
514
513
|
end
|
515
514
|
|
516
515
|
it "should know if it's a superset of another period" do
|
517
|
-
year = Period.
|
518
|
-
month = Period.
|
516
|
+
year = Period.parse('this_year')
|
517
|
+
month = Period.parse('this_month')
|
519
518
|
expect(year.superset_of?(month)).to be true
|
520
519
|
expect(year.superset_of?(year)).to be true
|
521
520
|
end
|
522
521
|
|
523
522
|
it "should know if it's a proper superset of another period" do
|
524
|
-
year = Period.
|
525
|
-
month = Period.
|
523
|
+
year = Period.parse('this_year')
|
524
|
+
month = Period.parse('this_month')
|
526
525
|
expect(year.proper_superset_of?(month)).to be true
|
527
526
|
expect(year.proper_superset_of?(year)).to be false
|
528
527
|
end
|
529
528
|
|
530
529
|
it "should know if it overlaps another period" do
|
531
|
-
period1 = Period.
|
532
|
-
period2 = Period.
|
533
|
-
period3 = Period.
|
530
|
+
period1 = Period.parse('2013')
|
531
|
+
period2 = Period.parse('2012-10', '2013-03')
|
532
|
+
period3 = Period.parse('2014')
|
534
533
|
expect(period1.overlaps?(period2)).to be true
|
535
534
|
expect(period2.overlaps?(period1)).to be true
|
536
535
|
expect(period1.overlaps?(period3)).to be false
|
537
536
|
end
|
538
537
|
|
539
538
|
it "should know whether an array of periods have overlaps within it" do
|
540
|
-
months = (1..12).to_a.map{ |k| Period.
|
541
|
-
year = Period.
|
539
|
+
months = (1..12).to_a.map{ |k| Period.parse("2013-#{k}") }
|
540
|
+
year = Period.parse("2013")
|
542
541
|
expect(year.has_overlaps_within?(months)).to be false
|
543
|
-
months << Period.
|
542
|
+
months << Period.parse("2013-09-15", "2013-10-02")
|
544
543
|
expect(year.has_overlaps_within?(months)).to be true
|
545
544
|
end
|
546
545
|
|
547
546
|
it "should know whether an array of periods span it" do
|
548
|
-
months = (1..12).to_a.map{ |k| Period.
|
549
|
-
year = Period.
|
547
|
+
months = (1..12).to_a.map{ |k| Period.parse("2013-#{k}") }
|
548
|
+
year = Period.parse("2013")
|
550
549
|
expect(year.spanned_by?(months)).to be true
|
551
550
|
|
552
|
-
months = (2..12).to_a.map{ |k| Period.
|
551
|
+
months = (2..12).to_a.map{ |k| Period.parse("2013-#{k}") }
|
553
552
|
expect(year.spanned_by?(months)).to be false
|
554
553
|
end
|
555
554
|
|
556
555
|
it "should know its intersection with other period" do
|
557
|
-
year = Period.
|
558
|
-
month = Period.
|
556
|
+
year = Period.parse('this_year')
|
557
|
+
month = Period.parse('this_month')
|
559
558
|
expect(year & month).to eq(month)
|
560
559
|
expect(month & year).to eq(month)
|
561
560
|
# It should return a Period, not a Range
|
@@ -563,7 +562,7 @@ describe Period do
|
|
563
562
|
end
|
564
563
|
|
565
564
|
it "should alias narrow_to to intersection" do
|
566
|
-
period1 = Period.
|
565
|
+
period1 = Period.parse('2014')
|
567
566
|
period2 = Period.new('2014-06-01', '2015-02-28')
|
568
567
|
period3 = period1.narrow_to(period2)
|
569
568
|
expect(period3.first).to eq(period2.first)
|
@@ -571,14 +570,14 @@ describe Period do
|
|
571
570
|
end
|
572
571
|
|
573
572
|
it "should return nil if no intersection" do
|
574
|
-
year = Period.
|
575
|
-
month = Period.
|
573
|
+
year = Period.parse('2014')
|
574
|
+
month = Period.parse('2013-05')
|
576
575
|
expect(year & month).to be_nil
|
577
576
|
end
|
578
577
|
|
579
578
|
it "should know its union with other period" do
|
580
|
-
last_month = Period.
|
581
|
-
month = Period.
|
579
|
+
last_month = Period.parse('last_month')
|
580
|
+
month = Period.parse('this_month')
|
582
581
|
expect((last_month + month).first).to eq(last_month.first)
|
583
582
|
expect((last_month + month).last).to eq(month.last)
|
584
583
|
# It should return a Period, not a Range
|
@@ -586,8 +585,8 @@ describe Period do
|
|
586
585
|
end
|
587
586
|
|
588
587
|
it "should know its differences with other period" do
|
589
|
-
year = Period.
|
590
|
-
month = Period.
|
588
|
+
year = Period.parse('this_year')
|
589
|
+
month = Period.parse('this_month')
|
591
590
|
# Note: the difference operator returns an Array of Periods resulting
|
592
591
|
# from removing other from self.
|
593
592
|
expect((year - month).first)
|
@@ -599,21 +598,21 @@ describe Period do
|
|
599
598
|
expect(p.class).to eq(Period)
|
600
599
|
end
|
601
600
|
|
602
|
-
last_year = Period.
|
603
|
-
month = Period.
|
601
|
+
last_year = Period.parse('last_year')
|
602
|
+
month = Period.parse('this_month')
|
604
603
|
expect(last_year - month).to eq([last_year])
|
605
604
|
end
|
606
605
|
|
607
606
|
it "should be able to find gaps from an array of periods" do
|
608
|
-
pp = Period.
|
607
|
+
pp = Period.parse('2014-2Q')
|
609
608
|
periods = [
|
610
|
-
Period.
|
611
|
-
Period.
|
609
|
+
Period.parse('2013-11', '2013-12-20'),
|
610
|
+
Period.parse('2014-01', '2014-04-20'),
|
612
611
|
# Gap 2014-04-21 to 2014-04-30
|
613
|
-
Period.
|
612
|
+
Period.parse('2014-05', '2014-05-11'),
|
614
613
|
# Gap 2014-05-12 to 2014-05-24
|
615
|
-
Period.
|
616
|
-
Period.
|
614
|
+
Period.parse('2014-05-25', '2014-07-11'),
|
615
|
+
Period.parse('2014-09')
|
617
616
|
]
|
618
617
|
gaps = pp.gaps(periods)
|
619
618
|
expect(gaps.size).to eq(2)
|
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: 1.0.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: 2016-
|
11
|
+
date: 2016-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|