fat_core 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/fat_core/period.rb +6 -3
- data/lib/fat_core/range.rb +11 -8
- data/lib/fat_core/version.rb +1 -1
- data/spec/lib/period_spec.rb +37 -0
- data/spec/lib/range_spec.rb +5 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13a129f290fef37db1a33846fa4fa6aa183ed530
|
4
|
+
data.tar.gz: bd5f34570185d9b556a5dcf6647c6dca13b21cd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c737d705f3b176ae808319cd7dbb0870bc2aafd2685b0bc5c11e4594990f5395b1d5ae1efd0b1b18ad723bc7357af9205cbcbdbe5b75ea0b610206f1cd62f6c
|
7
|
+
data.tar.gz: 0cc5d64d64bc821ffbcb5f7ef4e13390d32788c4170f4eba7183ddbeb1e6f9f612e908d75e40f8669d326343df6e50e9617e34b0fc4bcb42dc6eff1ba9884d12
|
data/lib/fat_core/period.rb
CHANGED
@@ -252,17 +252,20 @@ class Period
|
|
252
252
|
end
|
253
253
|
|
254
254
|
def intersection(other)
|
255
|
-
self.to_range.intersection(other.to_range)
|
255
|
+
result = self.to_range.intersection(other.to_range)
|
256
|
+
Period.new(result.first, result.last)
|
256
257
|
end
|
257
258
|
alias_method :&, :intersection
|
258
259
|
|
259
260
|
def union(other)
|
260
|
-
self.to_range.union(other.to_range)
|
261
|
+
result = self.to_range.union(other.to_range)
|
262
|
+
Period.new(result.first, result.last)
|
261
263
|
end
|
262
264
|
alias_method :+, :union
|
263
265
|
|
264
266
|
def difference(other)
|
265
|
-
self.to_range.difference(other.to_range)
|
267
|
+
ranges = self.to_range.difference(other.to_range)
|
268
|
+
ranges.each.map{ |r| Period.new(r.first, r.last) }
|
266
269
|
end
|
267
270
|
alias_method :-, :difference
|
268
271
|
|
data/lib/fat_core/range.rb
CHANGED
@@ -61,7 +61,7 @@ class Range
|
|
61
61
|
alias_method :&, :intersection
|
62
62
|
|
63
63
|
def union(other)
|
64
|
-
return nil unless self.overlaps?(other)
|
64
|
+
return nil unless self.overlaps?(other) || self.contiguous?(other)
|
65
65
|
([self.min, other.min].min..[self.max, other.max].max)
|
66
66
|
end
|
67
67
|
alias_method :+, :union
|
@@ -70,24 +70,27 @@ class Range
|
|
70
70
|
# argument from self. Because in the case where self is a superset of the
|
71
71
|
# other range, this will result in the difference being two non-contiguous
|
72
72
|
# ranges, this returns an array of ranges. If there is no overlap or if
|
73
|
-
# self is a subset of the other range, return an
|
73
|
+
# self is a subset of the other range, return an array of self
|
74
74
|
def difference(other)
|
75
75
|
unless max.respond_to?(:succ) && min.respond_to?(:pred) &&
|
76
76
|
other.max.respond_to?(:succ) && other.min.respond_to?(:pred)
|
77
77
|
raise "Range difference operation requires objects have pred and succ methods"
|
78
78
|
end
|
79
|
-
# return [] unless self.overlaps?(other)
|
80
|
-
if
|
81
|
-
|
82
|
-
(other.max.succ..max)]
|
83
|
-
elsif subset_of?(other)
|
79
|
+
# return [self] unless self.overlaps?(other)
|
80
|
+
if subset_of?(other)
|
81
|
+
# (4..7) - (0..10)
|
84
82
|
[]
|
83
|
+
elsif proper_superset_of?(other)
|
84
|
+
# (4..7) - (5..5) -> [(4..4), (6..7)]
|
85
|
+
[(min..other.min.pred), (other.max.succ..max)]
|
85
86
|
elsif overlaps?(other) && other.min <= min
|
87
|
+
# (4..7) - (2..5) -> (6..7)
|
86
88
|
[(other.max.succ .. max)]
|
87
89
|
elsif overlaps?(other) && other.max >= max
|
90
|
+
# (4..7) - (6..10) -> (4..5)
|
88
91
|
[(min .. other.min.pred)]
|
89
92
|
else
|
90
|
-
[]
|
93
|
+
[ self ]
|
91
94
|
end
|
92
95
|
end
|
93
96
|
alias_method :-, :difference
|
data/lib/fat_core/version.rb
CHANGED
data/spec/lib/period_spec.rb
CHANGED
@@ -305,5 +305,42 @@ describe Period do
|
|
305
305
|
|
306
306
|
expect(Period.new('2013-11-02', '2013-12-16').chunk_sym).to eq(:irregular)
|
307
307
|
end
|
308
|
+
|
309
|
+
it "should know its intersection with other period" do
|
310
|
+
year = Period.parse_spec('this_year')
|
311
|
+
month = Period.parse_spec('this_month')
|
312
|
+
expect(year & month).to eq(month)
|
313
|
+
expect(month & year).to eq(month)
|
314
|
+
# It should return a Period, not a Range
|
315
|
+
expect((month & year).class).to eq(Period)
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should know its union with other period" do
|
319
|
+
last_month = Period.parse_spec('last_month')
|
320
|
+
month = Period.parse_spec('this_month')
|
321
|
+
expect((last_month + month).first).to eq(last_month.first)
|
322
|
+
expect((last_month + month).last).to eq(month.last)
|
323
|
+
# It should return a Period, not a Range
|
324
|
+
expect((last_month + month).class).to eq(Period)
|
325
|
+
end
|
326
|
+
|
327
|
+
it "should know its differences with other period" do
|
328
|
+
year = Period.parse_spec('this_year')
|
329
|
+
month = Period.parse_spec('this_month')
|
330
|
+
# Note: the difference operator returns an Array of Periods resulting
|
331
|
+
# from removing other from self.
|
332
|
+
expect((year - month).first)
|
333
|
+
.to eq(Period.new(year.first, month.first - 1.day))
|
334
|
+
expect((year - month).last)
|
335
|
+
.to eq(Period.new(month.last + 1.day, year.last))
|
336
|
+
# It should return an Array of Periods, not a Ranges
|
337
|
+
(year - month).each do |p|
|
338
|
+
expect(p.class).to eq(Period)
|
339
|
+
end
|
340
|
+
|
341
|
+
last_year = Period.parse_spec('last_year')
|
342
|
+
month = Period.parse_spec('this_month')
|
343
|
+
expect(last_year - month).to eq([last_year])
|
344
|
+
end
|
308
345
|
end
|
309
346
|
end
|
data/spec/lib/range_spec.rb
CHANGED
@@ -60,6 +60,9 @@ describe Range do
|
|
60
60
|
expect(((0..10) + (5..20))).to eq((0..20))
|
61
61
|
expect(((0..10) + (5..20))).to eq((5..20) + (0..10))
|
62
62
|
expect(((0..10) + (10..20))).to eq((0..20))
|
63
|
+
|
64
|
+
# For discrete values, union should work on contiguous ranges
|
65
|
+
expect(((0..5) + (6..20))).to eq((0..20))
|
63
66
|
end
|
64
67
|
|
65
68
|
it "union should return nil if there is no overlap" do
|
@@ -93,8 +96,8 @@ describe Range do
|
|
93
96
|
expect(((4..10) - (7..10)).last).to eq((4..6))
|
94
97
|
|
95
98
|
# Other does not overlap
|
96
|
-
expect((4..10) - (13..20)).to
|
97
|
-
expect((4..10) - (1..3)).to
|
99
|
+
expect((4..10) - (13..20)).to eq([(4..10)])
|
100
|
+
expect((4..10) - (1..3)).to eq([(4..10)])
|
98
101
|
end
|
99
102
|
end
|
100
103
|
|