availabiliter 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5b67cc74bfc51556d4ec00b9fc2a1a98364d78aca1ea7975a8970e36858b023c
4
+ data.tar.gz: e6c267b060c68affa3d4c122d97f821221c76f543486db80a8b8704b5a3e09c1
5
+ SHA512:
6
+ metadata.gz: 3a4607aff951f4a9538cacc901ed45dd3f50f739d4b3662de249fb58257cd44e24621713bfabe585b2fac257ddc45016879dc372dc031e2dc5c74a91faca99e8
7
+ data.tar.gz: f48c05946d0488765966a6b8bb1bc130b833fa1d132cfc0a382bc4e5e40163e06ab8aff51782ef0fb004634a6066a74ef8ca431e8c2194e954116c0952ff6a22
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
+
13
+ # byebug historic
14
+ .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,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7.2
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - 'spec/**/*'
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Availabilities calculation with dates
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in availabiliter.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ availabiliter (0.1.0)
5
+ activesupport (~> 6.1.3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.1.3.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
+ ast (2.4.2)
17
+ byebug (11.1.3)
18
+ concurrent-ruby (1.1.9)
19
+ diff-lcs (1.4.4)
20
+ i18n (1.8.10)
21
+ concurrent-ruby (~> 1.0)
22
+ minitest (5.14.4)
23
+ parallel (1.20.1)
24
+ parser (3.0.1.1)
25
+ ast (~> 2.4.1)
26
+ rainbow (3.0.0)
27
+ rake (13.0.3)
28
+ regexp_parser (2.1.1)
29
+ rexml (3.2.5)
30
+ rspec (3.10.0)
31
+ rspec-core (~> 3.10.0)
32
+ rspec-expectations (~> 3.10.0)
33
+ rspec-mocks (~> 3.10.0)
34
+ rspec-core (3.10.1)
35
+ rspec-support (~> 3.10.0)
36
+ rspec-expectations (3.10.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.10.0)
39
+ rspec-mocks (3.10.2)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.10.0)
42
+ rspec-support (3.10.2)
43
+ rubocop (1.16.1)
44
+ parallel (~> 1.10)
45
+ parser (>= 3.0.0.0)
46
+ rainbow (>= 2.2.2, < 4.0)
47
+ regexp_parser (>= 1.8, < 3.0)
48
+ rexml
49
+ rubocop-ast (>= 1.7.0, < 2.0)
50
+ ruby-progressbar (~> 1.7)
51
+ unicode-display_width (>= 1.4.0, < 3.0)
52
+ rubocop-ast (1.7.0)
53
+ parser (>= 3.0.1.1)
54
+ ruby-progressbar (1.11.0)
55
+ tzinfo (2.0.4)
56
+ concurrent-ruby (~> 1.0)
57
+ unicode-display_width (2.0.0)
58
+ zeitwerk (2.4.2)
59
+
60
+ PLATFORMS
61
+ arm64-darwin-20
62
+
63
+ DEPENDENCIES
64
+ availabiliter!
65
+ byebug (~> 11.1.3)
66
+ rake (~> 13.0.3)
67
+ rspec (~> 3.10)
68
+ rubocop (~> 1.16.1)
69
+
70
+ BUNDLED WITH
71
+ 2.2.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 lioneldebauge
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,70 @@
1
+ # Availabiliter
2
+
3
+ Availabiliter is a Ruby availability calculator for date ranges. It handles the following edge cases:
4
+
5
+ - endless date ranges
6
+ - overlapping date ranges
7
+ - consecutive date ranges
8
+ - one day ranges
9
+
10
+ It also allows to calculate availabilities from a dedicated date in time.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'availabiliter'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install availabiliter
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ # Build an array with all the date ranges you are looking an availability for
32
+ range_array = [Date.new(1999, 1, 2)..Date.new(1999, 5, 1), Date.new(2000, 1, 1)..Date.new(2000, 2, 1), Date.new(2000, 3, 1)..nil]
33
+
34
+ Availabiliter.get_availabilities(range_array)
35
+ # => [Date.new(1999, 5, 2)..Date.new(1999, 12, 31), Date.new(2000, 2, 2)..Date.new(2000, 2, 29)]
36
+
37
+ # If you want to retrieve availabilities starting from a specific date simply add it as a second argument
38
+ Availabiliter.get_availabilities(range_array, Date.new(1999, 12, 15))
39
+ # => [Date.new(1999, 12, 15)..Date.new(1999, 12, 31), Date.new(2000, 2, 2)..Date.new(2000, 2, 29)]
40
+
41
+ # If all of your date ranges have an end the last availability will be endless
42
+ range_array = [Date.new(1999, 1, 2)..Date.new(1999, 5, 1)]
43
+
44
+ Availabiliter.get_availabilities(range_array)
45
+ # => [Date.new(1999, 5, 2)..]
46
+
47
+ ```
48
+ ## Dependencies
49
+
50
+ This gem depends solely on Active Support gem. One of the future planned improvement is to remove this dependency.
51
+
52
+ ## Future improvements
53
+
54
+ - remove any dependency from the project
55
+ - support Datetime
56
+ - support beginless ranges
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lioneldebauge/availabiliter.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/availabiliter/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "availabiliter"
7
+ spec.version = Availabiliter::VERSION
8
+ spec.authors = ["lioneldebauge"]
9
+ spec.email = ["lionel@livecolonies.com"]
10
+
11
+ spec.summary = "Availabiliter calculates availabilities for ruby ranges within a given time frame."
12
+ spec.homepage = "https://github.com/lioneldebauge/availabiliter.git"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.2")
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/lioneldebauge/availabiliter.git"
20
+ spec.metadata["changelog_uri"] = "https://github.com/lioneldebauge/availabiliter/blob/main/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "activesupport", "~> 6.1.3.2"
32
+ spec.add_development_dependency "byebug", "~> 11.1.3"
33
+ spec.add_development_dependency "rake", "~> 13.0.3"
34
+ spec.add_development_dependency "rspec", "~> 3.10"
35
+ spec.add_development_dependency "rubocop", "~> 1.16.1"
36
+ end
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 "availabiliter"
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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "availabiliter/version"
4
+ require_relative "availabiliter/timeframe"
5
+
6
+ # Availability calculator class
7
+ module Availabiliter
8
+ class << self
9
+ def get_availabilities(array, timeframe_start = nil)
10
+ TimeFrame.new(array, timeframe_start).availabilities
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/date/calculations"
4
+
5
+ # DateRange is a Range with only dates
6
+ # DateRange start_date is always the earliest date and end_date the latest date
7
+ class DateRange < Range
8
+ include Comparable
9
+
10
+ alias start_date begin
11
+ alias end_date end
12
+
13
+ def initialize(start_date, end_date)
14
+ super
15
+ raise ArgumentError, "bad value for DateRange" unless valid?
16
+ end
17
+
18
+ def independent?(other)
19
+ return true if other.nil?
20
+
21
+ !overlaps?(other) && !adjacent?(other)
22
+ end
23
+
24
+ def tomorrow
25
+ end_date&.tomorrow
26
+ end
27
+
28
+ def yesterday
29
+ start_date.yesterday
30
+ end
31
+
32
+ ## adjacent == touches but doesn't overlap other DateRange
33
+ def adjacent?(other)
34
+ return other.end_date == yesterday if end_date.nil?
35
+
36
+ other.end_date == yesterday || other.start_date == tomorrow
37
+ end
38
+
39
+ def overlaps?(other)
40
+ cover?(other.begin) || other.cover?(first)
41
+ end
42
+
43
+ def next_availability(next_date_range)
44
+ return if end_date.nil?
45
+ return tomorrow..nil if next_date_range.nil?
46
+ return unless independent?(next_date_range)
47
+
48
+ gap_start = tomorrow
49
+ gap_end = next_date_range.yesterday
50
+
51
+ gap_start..gap_end
52
+ end
53
+
54
+ def furthest(other)
55
+ return self if end_date.nil? || other.nil?
56
+ return other if other.end_date.nil?
57
+
58
+ [self, other].max_by(&:end_date)
59
+ end
60
+
61
+ private
62
+
63
+ def valid?
64
+ start_date.instance_of?(Date) && (end_date.nil? || start_date < end_date)
65
+ end
66
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "date_range"
4
+
5
+ # A TimeFrame is an object representing several DateRange instances. It can have a start.
6
+ # It can have only one endless DateRange.
7
+ class TimeFrame
8
+ attr_reader :start_date, :date_ranges
9
+
10
+ def initialize(array, start_date = nil)
11
+ @start_date = start_date
12
+ @date_ranges = build_date_ranges(array)
13
+
14
+ raise ArgumentError unless valid?
15
+ end
16
+
17
+ def availabilities
18
+ return [start_date..nil] if date_ranges.empty?
19
+ return build_availabilities if start_date.nil?
20
+
21
+ availabilities = build_availabilities
22
+ start_date < first_date_range.start_date ? availabilities.unshift(first_availability) : availabilities
23
+ end
24
+
25
+ private
26
+
27
+ def build_date_ranges(array)
28
+ date_range_array = array.filter_map do |range|
29
+ next if out_of_timeframe?(range.end)
30
+
31
+ DateRange.new(range.begin, range.end)
32
+ end
33
+
34
+ date_range_array.sort_by(&:start_date)
35
+ end
36
+
37
+ def build_availabilities
38
+ furthest_date_range = first_date_range
39
+
40
+ date_ranges.filter_map.with_index do |date_range, index|
41
+ next_date_range = date_ranges[index + 1]
42
+
43
+ furthest_date_range = furthest_date_range.furthest(date_range)
44
+ furthest_date_range.next_availability(next_date_range)
45
+ end
46
+ end
47
+
48
+ def out_of_timeframe?(range_end)
49
+ start_date.present? && (range_end.present? && range_end < start_date)
50
+ end
51
+
52
+ def first_availability
53
+ start_date..first_date_range.yesterday
54
+ end
55
+
56
+ def first_date_range
57
+ date_ranges.first
58
+ end
59
+
60
+ def valid?
61
+ end_date_valid? && date_ranges.count { |date_range| date_range.end_date.nil? } <= 1
62
+ end
63
+
64
+ def end_date_valid?
65
+ start_date.instance_of?(Date) || start_date.instance_of?(NilClass)
66
+ end
67
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Availabiliter
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: availabiliter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - lioneldebauge
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.3.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 11.1.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 11.1.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 13.0.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 13.0.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.16.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.16.1
83
+ description:
84
+ email:
85
+ - lionel@livecolonies.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".github/workflows/main.yml"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - CHANGELOG.md
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - availabiliter.gemspec
101
+ - bin/console
102
+ - bin/setup
103
+ - lib/availabiliter.rb
104
+ - lib/availabiliter/date_range.rb
105
+ - lib/availabiliter/timeframe.rb
106
+ - lib/availabiliter/version.rb
107
+ homepage: https://github.com/lioneldebauge/availabiliter.git
108
+ licenses:
109
+ - MIT
110
+ metadata:
111
+ allowed_push_host: https://rubygems.org
112
+ homepage_uri: https://github.com/lioneldebauge/availabiliter.git
113
+ source_code_uri: https://github.com/lioneldebauge/availabiliter.git
114
+ changelog_uri: https://github.com/lioneldebauge/availabiliter/blob/main/CHANGELOG.md
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 2.7.2
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.2.15
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Availabiliter calculates availabilities for ruby ranges within a given time
134
+ frame.
135
+ test_files: []