month_range 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 668013602f4cd9be1239f8dd3195a22dc3bc3e211297077f287c9195422f2c31
4
+ data.tar.gz: d86e45758b7347038faf1ec70437d2d85d54bca28ff4e37cd21b1fed3ce6d01e
5
+ SHA512:
6
+ metadata.gz: fbdbd8f617c64b26c195b641e06bef8e0b5dd12e5aa5983ac1a0dc45555e39b3fc5bb7c51bca3431b2a1ef7a834591130e9f55ab0128fa581c2baaa6c9cb2c5a
7
+ data.tar.gz: cec517f459484adbc546b58f012fd95e2a017e651d6168b37f1d70cf392b61e8ed06d5365ff2395fc8797a6b3c7482bc2b49a8b8f2aeb22217882a90d4a20a0e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ inherit_from:
2
+ - .rubocop_todo.yml
3
+
4
+ Style/ClassAndModuleChildren:
5
+ Enabled: false
6
+ Style/AsciiComments:
7
+ Enabled: false
8
+ Layout/LineLength:
9
+ Max: 120
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ AllCops:
14
+ Exclude:
15
+ - tmp/**/*
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,31 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2020-03-31 00:06:07 +0900 using RuboCop version 0.80.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 6
10
+ # Cop supports --auto-correct.
11
+ # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
12
+ # URISchemes: http, https
13
+ Layout/LineLength:
14
+ Max: 393
15
+
16
+ # Offense count: 2
17
+ Metrics/AbcSize:
18
+ Max: 23
19
+
20
+ # Offense count: 1
21
+ Metrics/CyclomaticComplexity:
22
+ Max: 8
23
+
24
+ # Offense count: 1
25
+ # Configuration parameters: CountComments, ExcludedMethods.
26
+ Metrics/MethodLength:
27
+ Max: 20
28
+
29
+ # Offense count: 1
30
+ Metrics/PerceivedComplexity:
31
+ Max: 9
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jun5araki@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in month_range.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'rubocop'
11
+ gem 'yard'
12
+ gem 'zeitwerk'
13
+
14
+ gem 'byebug', '~> 11.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ month_range (0.1.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ byebug (11.1.1)
11
+ diff-lcs (1.3)
12
+ jaro_winkler (1.5.4)
13
+ parallel (1.19.1)
14
+ parser (2.7.0.5)
15
+ ast (~> 2.4.0)
16
+ rainbow (3.0.0)
17
+ rake (12.3.3)
18
+ rexml (3.2.4)
19
+ rspec (3.9.0)
20
+ rspec-core (~> 3.9.0)
21
+ rspec-expectations (~> 3.9.0)
22
+ rspec-mocks (~> 3.9.0)
23
+ rspec-core (3.9.1)
24
+ rspec-support (~> 3.9.1)
25
+ rspec-expectations (3.9.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-mocks (3.9.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-support (3.9.2)
32
+ rubocop (0.80.1)
33
+ jaro_winkler (~> 1.5.1)
34
+ parallel (~> 1.10)
35
+ parser (>= 2.7.0.1)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ rexml
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 1.7)
40
+ ruby-progressbar (1.10.1)
41
+ unicode-display_width (1.6.1)
42
+ yard (0.9.24)
43
+ zeitwerk (2.3.0)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ byebug (~> 11.1)
50
+ month_range!
51
+ rake (~> 12.0)
52
+ rspec (~> 3.0)
53
+ rubocop
54
+ yard
55
+ zeitwerk
56
+
57
+ BUNDLED WITH
58
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 junara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # MonthRange
2
+ Calculate month range union and difference including unterminated end.
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'month_range'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ ## Usage
18
+ * Month object is `MonthRange::Month` object. It is special Date object which day is beginning of month. Like this.
19
+ ```ruby
20
+ Date.parse('2020-01-01') is valid.
21
+ Date.parse('2020-01-02') is not valid.
22
+ ```
23
+
24
+ * range start (called as start_month) is only Month.
25
+ * range end is month or nil is valid.
26
+
27
+ ### Example
28
+
29
+ ```ruby
30
+ require 'month_range'
31
+
32
+ # 和
33
+
34
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
35
+ range_array = [Date.parse('2020-03-01'), Date.parse('2020-05-01')]
36
+ MonthRange::Service.add(range_array, from_range_arrays)
37
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-05-01 ((2458971j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
38
+
39
+
40
+ # 和(例:複数期間にまたがる)
41
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
42
+ range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
43
+ MonthRange::Service.add(range_array, from_range_arrays)
44
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
45
+
46
+
47
+ # 和(例:隣り合った期間を連結)
48
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
49
+ range_array = [Date.parse('2020-05-01'), Date.parse('2020-06-01')]
50
+ MonthRange::Service.add(range_array, from_range_arrays)
51
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
52
+
53
+
54
+ # 和(例:終端なし)
55
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
56
+ range_array = [Date.parse('2020-05-01'), nil]
57
+ MonthRange::Service.add(range_array, from_range_arrays)
58
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, nil]]
59
+
60
+
61
+ # 差
62
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
63
+ range_array = [Date.parse('2020-03-01'), Date.parse('2020-05-01')]
64
+ MonthRange::Service.subtraction(range_array, from_range_arrays)
65
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-02-01 ((2458881j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
66
+
67
+
68
+ # 差(例:複数期間にまたがる)
69
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
70
+ range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
71
+ MonthRange::Service.subtraction(range_array, from_range_arrays)
72
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-02-01 ((2458881j,0s,0n),+0s,2299161j)>], [#<Date: 2020-09-01 ((2459094j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
73
+
74
+
75
+
76
+ # 差(例:期間内)
77
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
78
+ range_array = [Date.parse('2020-02-01'), Date.parse('2020-03-01')]
79
+ MonthRange::Service.subtraction(range_array, from_range_arrays)
80
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>], [#<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
81
+
82
+
83
+
84
+ # 差(例:終端なし)
85
+ from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
86
+ range_array = [Date.parse('2020-05-01'), nil]
87
+ MonthRange::Service.subtraction(range_array, from_range_arrays)
88
+ # => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>]]
89
+
90
+
91
+ ```
92
+
93
+ ## Document
94
+ https://rubydoc.info/gems/month_range
95
+
96
+
97
+ ## Development
98
+
99
+ TODO
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/month_range. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/month_range/blob/master/CODE_OF_CONDUCT.md).
104
+
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
109
+
110
+ ## Code of Conduct
111
+
112
+ Everyone interacting in the MonthRange project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/month_range/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'month_range'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MonthRange::Collection
4
+ attr_reader :collection_ranges
5
+
6
+ def initialize
7
+ @collection_ranges = []
8
+ end
9
+
10
+ def to_a
11
+ @collection_ranges.map { |collection_rang| [collection_rang.start_month, collection_rang.end_month] }
12
+ end
13
+
14
+ def add(range)
15
+ raise MonthRange::Error, 'Need MonthRange::MRange' unless range.is_a?(MonthRange::MRange)
16
+ return @collection_ranges << range if @collection_ranges.empty?
17
+
18
+ overlapped_collection_ranges = @collection_ranges.select { |collection_range| collection_range.overlap?(range) }
19
+ return @collection_ranges if overlapped_collection_ranges.nil?
20
+
21
+ update_collection_ranges(overlapped_collection_ranges, range) { |rs, r| [merge_range(rs, r)] }
22
+ end
23
+
24
+ def subtract(range)
25
+ raise MonthRange::Error, 'Need MonthRange::MRange' unless range.is_a?(MonthRange::MRange)
26
+ raise if @collection_ranges.empty?
27
+
28
+ overlapped_collection_ranges = @collection_ranges.select { |collection_range| collection_range.overlap?(range) }
29
+ return @collection_ranges if overlapped_collection_ranges.nil?
30
+
31
+ update_collection_ranges(overlapped_collection_ranges, range) { |rs, r| subtract_range(rs, r) }
32
+ end
33
+
34
+ def self.new_from_date_range_array(date_range_array)
35
+ collection = new
36
+ date_range_array.each do |range|
37
+ start_month = range[0]
38
+ end_month = range[1]
39
+ m_range = MonthRange::MRange.new(start_month, end_month)
40
+ collection.add(m_range)
41
+ end
42
+ collection
43
+ end
44
+
45
+ private
46
+
47
+ def update_collection_ranges(overlapped_collection_ranges, range)
48
+ @collection_ranges -= overlapped_collection_ranges
49
+ @collection_ranges += yield(overlapped_collection_ranges, range)
50
+ @collection_ranges = sort_ranges_asc(@collection_ranges.flatten.compact)
51
+ @collection_ranges = combine_continuous_range(@collection_ranges)
52
+ @collection_ranges
53
+ end
54
+
55
+ def sort_ranges_asc(ranges)
56
+ return ranges if ranges.size <= 1
57
+
58
+ ranges.sort { |a, b| a.start_month <=> b.start_month }
59
+ end
60
+
61
+ def merge_range(ranges, range)
62
+ start_month = (ranges + [range]).map(&:start_month).min { |a, b| a <=> b }
63
+ end_month = if (ranges + [range]).any?(&:non_terminated?)
64
+ nil
65
+ else
66
+ (ranges + [range]).map(&:end_month).max { |a, b| a <=> b }
67
+ end
68
+ MonthRange::MRange.new(start_month, end_month)
69
+ end
70
+
71
+ def subtract_range(ranges, range)
72
+ output_range = []
73
+ ranges.each do |r|
74
+ output_range << r.subtract(range)
75
+ end
76
+ output_range.flatten.compact
77
+ end
78
+
79
+ def combine_continuous_range(ranges)
80
+ return ranges if ranges.count <= 1
81
+
82
+ temp_range = nil
83
+ output_ranges = []
84
+ ranges.each_with_index do |range, idx|
85
+ (temp_range = range) && next if idx.zero?
86
+
87
+ if idx == ranges.size - 1 && continuous?(temp_range, range)
88
+ output_ranges << MonthRange::MRange.new(temp_range.start_month, range.end_month)
89
+ next
90
+ elsif idx == ranges.size - 1
91
+ output_ranges << temp_range
92
+ output_ranges << range
93
+ next
94
+ elsif continuous?(temp_range, range)
95
+ temp_range = MonthRange::MRange.new(temp_range.start_month, range.end_month)
96
+ next
97
+ end
98
+ output_ranges << temp_range
99
+ temp_range = range
100
+ end
101
+ output_ranges
102
+ end
103
+
104
+ def continuous?(range, next_range)
105
+ return false if range.nil? || next_range.nil?
106
+ raise if range.start_month >= next_range.start_month
107
+ return false if range.end_month.nil?
108
+
109
+ range.end_month == next_range.just_before
110
+ end
111
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MonthRange
4
+ class Error < StandardError
5
+ end
6
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require_relative '../month_range/error'
5
+
6
+ class MonthRange::MRange < Range
7
+ class InvalidStartMonth < MonthRange::Error
8
+ end
9
+ class InvalidEndMonth < MonthRange::Error
10
+ end
11
+ class InvalidStartEndRelation < MonthRange::Error
12
+ end
13
+
14
+ def initialize(start_month, end_month)
15
+ raise InvalidStartMonth, start_month unless valid_start_month?(start_month)
16
+ raise InvalidEndMonth, end_month unless valid_end_month?(end_month)
17
+ raise InvalidStartEndRelation, [start_month, end_month] unless valid_start_end_relation?(start_month, end_month)
18
+
19
+ super(start_month, end_month)
20
+ end
21
+
22
+ def overlap?(range)
23
+ raise "#{range} must be MRange." unless range.is_a?(MonthRange::MRange)
24
+ return true if cover?(range.start_month)
25
+
26
+ if range.non_terminated?
27
+ range.start_month <= start_month
28
+ else
29
+ cover?(range.end_month)
30
+ end
31
+ end
32
+
33
+ def start_month
34
+ first
35
+ end
36
+
37
+ def end_month
38
+ non_terminated? ? nil : last
39
+ end
40
+
41
+ def non_terminated?
42
+ count == Float::INFINITY
43
+ end
44
+
45
+ def subtract(range)
46
+ return self unless overlap?(range)
47
+
48
+ output_range = []
49
+ if cover?(range.just_before)
50
+ output_range << MonthRange::MRange.new(start_month, range.just_before)
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
57
+ output_range.flatten.compact
58
+ end
59
+
60
+ def just_before
61
+ start_month.prev_month(1)
62
+ end
63
+
64
+ def just_after
65
+ non_terminated? ? nil : end_month.next_month(1)
66
+ end
67
+
68
+ private
69
+
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
+ def valid_start_end_relation?(start_month, end_month)
84
+ return true if end_month.nil?
85
+
86
+ start_month <= end_month
87
+ end
88
+
89
+ def beginning_of_month?(date)
90
+ date.mday == 1
91
+ end
92
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ class MonthRange::Month < Date
6
+ def initialize
7
+ super
8
+ end
9
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MonthRange::Service
4
+ # Subtract range
5
+ # @param [MonthRange::MRange] range_array
6
+ # @param [Array] from_range_arrays
7
+ # @example from_range_arrays
8
+ # from_range_arrays = [
9
+ # [Date.parse('2020-01-01'), Date.parse('2020-04-01')],
10
+ # [Date.parse('2020-07-01'), Date.parse('2020-10-01')],
11
+ # [Date.parse('2020-12-01'), nil]
12
+ # ]
13
+ # @example Simple
14
+ # range_array = [Date.parse('2020-02-01'), Date.parse('2020-03-01')]
15
+ # MonthRange::Service.add(range_array, from_range_arrays)
16
+ #
17
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>], [#<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
18
+ # @example 複数期間にまたがる
19
+ # range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
20
+ # MonthRange::Service.add(range_array, from_range_arrays)
21
+ #
22
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-02-01 ((2458881j,0s,0n),+0s,2299161j)>], [#<Date: 2020-09-01 ((2459094j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
23
+ # @example 期間内
24
+ # range_array = [Date.parse('2020-02-01'), Date.parse('2020-03-01')]
25
+ # MonthRange::Service.add(range_array, from_range_arrays)
26
+ #
27
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>], [#<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
28
+ # @example 終端なし
29
+ # range_array = [Date.parse('2020-05-01'), nil]
30
+ # MonthRange::Service.add(range_array, from_range_arrays)
31
+ #
32
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>]]
33
+ def self.subtraction(range_array, from_range_arrays)
34
+ collection = range_arrays_to_collection(from_range_arrays)
35
+ start_month = range_array[0]
36
+ end_month = range_array[1]
37
+ m_range = MonthRange::MRange.new(start_month, end_month)
38
+ collection.subtract(m_range)
39
+ collection.to_a
40
+ end
41
+
42
+ # Union range
43
+ # @param [MonthRange::MRange] range_array
44
+ # @param [Array of MonthRange::MRange] from_range_arrays
45
+ # @return [Array of MonthRange::MRange]
46
+ # @example from_range_arrays
47
+ # from_range_arrays = [
48
+ # [Date.parse('2020-01-01'), Date.parse('2020-04-01')],
49
+ # [Date.parse('2020-07-01'), Date.parse('2020-10-01')],
50
+ # [Date.parse('2020-12-01'), nil]
51
+ # ]
52
+ # @example Simple
53
+ # range_array = [Date.parse('2020-03-01'), Date.parse('2020-05-01')]
54
+ # MonthRange::Service.add(range_array, from_range_arrays)
55
+ #
56
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-05-01 ((2458971j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
57
+ # @example 複数期間にまたがる
58
+ # range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
59
+ # MonthRange::Service.add(range_array, from_range_arrays)
60
+ #
61
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
62
+ # @example 隣り合った期間を連結
63
+ # range_array = [Date.parse('2020-05-01'), Date.parse('2020-06-01')]
64
+ # MonthRange::Service.add(range_array, from_range_arrays)
65
+ #
66
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]
67
+ # @example 終端なし
68
+ # range_array = [Date.parse('2020-05-01'), nil]
69
+ # MonthRange::Service.add(range_array, from_range_arrays)
70
+ #
71
+ # #=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, nil]]
72
+ def self.add(range_array, from_range_arrays)
73
+ collection = range_arrays_to_collection(from_range_arrays)
74
+ start_month = range_array[0]
75
+ end_month = range_array[1]
76
+ m_range = MonthRange::MRange.new(start_month, end_month)
77
+ collection.add(m_range)
78
+ collection.to_a
79
+ end
80
+
81
+ # Change to MonthRange::Collection
82
+ # @param [[[Date, Date], [Date, Date],]] range_arrays
83
+ # @return [MonthRange::Collection]
84
+ def self.range_arrays_to_collection(range_arrays)
85
+ collection = MonthRange::Collection.new
86
+ range_arrays.each do |range|
87
+ start_month = range[0]
88
+ end_month = range[1]
89
+ m_range = MonthRange::MRange.new(start_month, end_month)
90
+ collection.add(m_range)
91
+ end
92
+ collection
93
+ end
94
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MonthRange
4
+ class Version
5
+ VERSION = '0.1.2'
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'month_range/version'
4
+ require_relative 'month_range/m_range'
5
+ require_relative 'month_range/collection'
6
+ require_relative 'month_range/error'
7
+ require_relative 'month_range/month'
8
+ require_relative 'month_range/service'
9
+ module MonthRange
10
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/month_range/version'
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'month_range'
6
+ spec.version = MonthRange::Version::VERSION
7
+ spec.authors = ['jungo araki']
8
+ spec.email = ['jun5araki@gmail.com']
9
+
10
+ spec.summary = 'Addition and subtraction for month range.'
11
+ spec.homepage = 'https://github.com/junara/month_range'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
14
+
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = spec.homepage
19
+ spec.metadata['documentation_uri'] = 'https://rubydoc.info/gems/month_range'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: month_range
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - jungo araki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - jun5araki@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - ".rubocop_todo.yml"
24
+ - ".travis.yml"
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/month_range.rb
34
+ - lib/month_range/collection.rb
35
+ - lib/month_range/error.rb
36
+ - lib/month_range/m_range.rb
37
+ - lib/month_range/month.rb
38
+ - lib/month_range/service.rb
39
+ - lib/month_range/version.rb
40
+ - month_range.gemspec
41
+ homepage: https://github.com/junara/month_range
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/junara/month_range
46
+ source_code_uri: https://github.com/junara/month_range
47
+ changelog_uri: https://github.com/junara/month_range
48
+ documentation_uri: https://rubydoc.info/gems/month_range
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.6.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.1.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Addition and subtraction for month range.
68
+ test_files: []