month_range 0.1.2 → 0.1.3
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/month_range/collection.rb +3 -7
- data/lib/month_range/m_range.rb +16 -47
- data/lib/month_range/month.rb +78 -2
- data/lib/month_range/service.rb +6 -6
- data/lib/month_range/version.rb +1 -1
- data/month_range.gemspec +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80d382540a11a08d9b7e42ccbf10572420d191006f80726880eded045f5c5246
|
4
|
+
data.tar.gz: 8064de1488e93416960a054a87f4244eae46954f1bb9cd5301601b00c66aa4b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed1ea3c8932e644096d8f3701c7ce3664354f47520253f4904bde95584a0312ffb0bf1dfe8556d9b22d8d7ebe02a9b9afce16ca3fabdd04400781b8ce389d190
|
7
|
+
data.tar.gz: daf57ae12b1681146daa7dec5af6448c606be15640d831ed8cbc002ad03031990990428c378c4292b2082890008256c30a8b1828b9483acf50a4ed96434f2d88
|
@@ -8,7 +8,7 @@ class MonthRange::Collection
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_a
|
11
|
-
@collection_ranges.map { |
|
11
|
+
@collection_ranges.map { |collection_range| [collection_range.start_month.to_date, collection_range.end_month.to_date] }
|
12
12
|
end
|
13
13
|
|
14
14
|
def add(range)
|
@@ -60,11 +60,7 @@ class MonthRange::Collection
|
|
60
60
|
|
61
61
|
def merge_range(ranges, range)
|
62
62
|
start_month = (ranges + [range]).map(&:start_month).min { |a, b| a <=> b }
|
63
|
-
end_month =
|
64
|
-
nil
|
65
|
-
else
|
66
|
-
(ranges + [range]).map(&:end_month).max { |a, b| a <=> b }
|
67
|
-
end
|
63
|
+
end_month = (ranges + [range]).map(&:end_month).max { |a, b| a <=> b }
|
68
64
|
MonthRange::MRange.new(start_month, end_month)
|
69
65
|
end
|
70
66
|
|
@@ -104,7 +100,7 @@ class MonthRange::Collection
|
|
104
100
|
def continuous?(range, next_range)
|
105
101
|
return false if range.nil? || next_range.nil?
|
106
102
|
raise if range.start_month >= next_range.start_month
|
107
|
-
return false if range.end_month.
|
103
|
+
return false if range.end_month.infinite?
|
108
104
|
|
109
105
|
range.end_month == next_range.just_before
|
110
106
|
end
|
data/lib/month_range/m_range.rb
CHANGED
@@ -4,56 +4,42 @@ require 'date'
|
|
4
4
|
require_relative '../month_range/error'
|
5
5
|
|
6
6
|
class MonthRange::MRange < Range
|
7
|
-
|
8
|
-
|
9
|
-
class InvalidEndMonth < MonthRange::Error
|
10
|
-
end
|
11
|
-
class InvalidStartEndRelation < MonthRange::Error
|
7
|
+
attr_reader :start_month, :end_month
|
8
|
+
class InvalidStartEnd < MonthRange::Error
|
12
9
|
end
|
13
10
|
|
14
11
|
def initialize(start_month, end_month)
|
15
|
-
raise
|
16
|
-
raise
|
17
|
-
raise
|
12
|
+
raise InvalidStartEnd unless start_month.is_a?(MonthRange::Month)
|
13
|
+
raise InvalidStartEnd unless end_month.is_a?(MonthRange::Month) || end_month.is_a?(MonthRange::Month::Infinity)
|
14
|
+
raise InvalidStartEnd [start_month, end_month] unless valid_start_end_relation?(start_month, end_month)
|
18
15
|
|
19
16
|
super(start_month, end_month)
|
17
|
+
@start_month = start_month
|
18
|
+
@end_month = end_month
|
20
19
|
end
|
21
20
|
|
22
21
|
def overlap?(range)
|
23
22
|
raise "#{range} must be MRange." unless range.is_a?(MonthRange::MRange)
|
24
|
-
return true if cover?(range.start_month)
|
25
23
|
|
26
|
-
if range.
|
27
|
-
|
28
|
-
else
|
29
|
-
cover?(range.end_month)
|
24
|
+
if cover?(range.start_month) || cover?(range.end_month) || (range.start_month <= start_month && end_month <= range.end_month)
|
25
|
+
true
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
33
|
-
def
|
34
|
-
|
29
|
+
def non_terminated?
|
30
|
+
end_month.infinite?
|
35
31
|
end
|
36
32
|
|
37
|
-
def
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
def non_terminated?
|
42
|
-
count == Float::INFINITY
|
33
|
+
def terminated?
|
34
|
+
!end_month.infinite?
|
43
35
|
end
|
44
36
|
|
45
37
|
def subtract(range)
|
46
38
|
return self unless overlap?(range)
|
47
39
|
|
48
40
|
output_range = []
|
49
|
-
if cover?(range.
|
50
|
-
|
51
|
-
output_range
|
52
|
-
end
|
53
|
-
|
54
|
-
if !range.non_terminated? && cover?(range.just_after)
|
55
|
-
output_range << MonthRange::MRange.new(range.just_after, end_month)
|
56
|
-
end
|
41
|
+
output_range << MonthRange::MRange.new(start_month, range.just_before) if cover?(range.start_month)
|
42
|
+
output_range << MonthRange::MRange.new(range.just_after, end_month) if cover?(range.end_month) && range.terminated?
|
57
43
|
output_range.flatten.compact
|
58
44
|
end
|
59
45
|
|
@@ -62,31 +48,14 @@ class MonthRange::MRange < Range
|
|
62
48
|
end
|
63
49
|
|
64
50
|
def just_after
|
65
|
-
non_terminated? ?
|
51
|
+
non_terminated? ? end_month : end_month.next_month(1)
|
66
52
|
end
|
67
53
|
|
68
54
|
private
|
69
55
|
|
70
|
-
def valid_start_month?(start_month)
|
71
|
-
return true if start_month.is_a?(Date) && beginning_of_month?(start_month)
|
72
|
-
|
73
|
-
false
|
74
|
-
end
|
75
|
-
|
76
|
-
def valid_end_month?(end_month)
|
77
|
-
return true if end_month.nil?
|
78
|
-
return true if end_month.is_a?(Date) && beginning_of_month?(end_month)
|
79
|
-
|
80
|
-
false
|
81
|
-
end
|
82
|
-
|
83
56
|
def valid_start_end_relation?(start_month, end_month)
|
84
57
|
return true if end_month.nil?
|
85
58
|
|
86
59
|
start_month <= end_month
|
87
60
|
end
|
88
|
-
|
89
|
-
def beginning_of_month?(date)
|
90
|
-
date.mday == 1
|
91
|
-
end
|
92
61
|
end
|
data/lib/month_range/month.rb
CHANGED
@@ -1,9 +1,85 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'date'
|
4
|
+
require_relative '../month_range/error'
|
4
5
|
|
5
6
|
class MonthRange::Month < Date
|
6
|
-
|
7
|
-
|
7
|
+
class InvalidMonthFormat < MonthRange::Error
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.create(date)
|
11
|
+
return MonthRange::Month::Infinity.new if date.nil?
|
12
|
+
raise InvalidMonthFormat unless date.is_a?(Date)
|
13
|
+
raise InvalidMonthFormat unless date.mday == 1
|
14
|
+
|
15
|
+
new(date.year, date.month, date.mday)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_date
|
19
|
+
Date.new(year, month, mday)
|
20
|
+
end
|
21
|
+
|
22
|
+
class Infinity < Numeric
|
23
|
+
def initialize(d = 1)
|
24
|
+
@d = d <=> 0
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :d
|
28
|
+
|
29
|
+
def zero?
|
30
|
+
false
|
31
|
+
end
|
32
|
+
|
33
|
+
def finite?
|
34
|
+
false
|
35
|
+
end
|
36
|
+
|
37
|
+
def infinite?
|
38
|
+
d.nonzero?
|
39
|
+
end
|
40
|
+
|
41
|
+
def <(_other)
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
def <=(other)
|
46
|
+
other.infinite?
|
47
|
+
end
|
48
|
+
|
49
|
+
def >(_other)
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
def >=(other)
|
54
|
+
other.infinite?
|
55
|
+
end
|
56
|
+
|
57
|
+
def ==(other)
|
58
|
+
other.infinite?
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_date
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
|
65
|
+
def <=>(other)
|
66
|
+
case other
|
67
|
+
when Infinity
|
68
|
+
return d <=> other.d
|
69
|
+
when MonthRange::Month
|
70
|
+
return d
|
71
|
+
end
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_f
|
76
|
+
return 0 if @d.zero?
|
77
|
+
|
78
|
+
if @d.positive?
|
79
|
+
Float::INFINITY
|
80
|
+
else
|
81
|
+
-Float::INFINITY
|
82
|
+
end
|
83
|
+
end
|
8
84
|
end
|
9
85
|
end
|
data/lib/month_range/service.rb
CHANGED
@@ -32,8 +32,8 @@ class MonthRange::Service
|
|
32
32
|
# #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>]]
|
33
33
|
def self.subtraction(range_array, from_range_arrays)
|
34
34
|
collection = range_arrays_to_collection(from_range_arrays)
|
35
|
-
start_month = range_array[0]
|
36
|
-
end_month = range_array[1]
|
35
|
+
start_month = MonthRange::Month.create(range_array[0])
|
36
|
+
end_month = MonthRange::Month.create(range_array[1])
|
37
37
|
m_range = MonthRange::MRange.new(start_month, end_month)
|
38
38
|
collection.subtract(m_range)
|
39
39
|
collection.to_a
|
@@ -71,8 +71,8 @@ class MonthRange::Service
|
|
71
71
|
# #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, nil]]
|
72
72
|
def self.add(range_array, from_range_arrays)
|
73
73
|
collection = range_arrays_to_collection(from_range_arrays)
|
74
|
-
start_month = range_array[0]
|
75
|
-
end_month = range_array[1]
|
74
|
+
start_month = MonthRange::Month.create(range_array[0])
|
75
|
+
end_month = MonthRange::Month.create(range_array[1])
|
76
76
|
m_range = MonthRange::MRange.new(start_month, end_month)
|
77
77
|
collection.add(m_range)
|
78
78
|
collection.to_a
|
@@ -84,8 +84,8 @@ class MonthRange::Service
|
|
84
84
|
def self.range_arrays_to_collection(range_arrays)
|
85
85
|
collection = MonthRange::Collection.new
|
86
86
|
range_arrays.each do |range|
|
87
|
-
start_month = range[0]
|
88
|
-
end_month = range[1]
|
87
|
+
start_month = MonthRange::Month.create(range[0])
|
88
|
+
end_month = MonthRange::Month.create(range[1])
|
89
89
|
m_range = MonthRange::MRange.new(start_month, end_month)
|
90
90
|
collection.add(m_range)
|
91
91
|
end
|
data/lib/month_range/version.rb
CHANGED
data/month_range.gemspec
CHANGED
@@ -12,7 +12,6 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.license = 'MIT'
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
|
14
14
|
|
15
|
-
|
16
15
|
spec.metadata['homepage_uri'] = spec.homepage
|
17
16
|
spec.metadata['source_code_uri'] = spec.homepage
|
18
17
|
spec.metadata['changelog_uri'] = spec.homepage
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: month_range
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jungo araki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|