extended_range 0.1.0

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: 3ebf8471421f9984b9fe5e24a2a148a9f834c84aae7186a702c43e4f29788343
4
+ data.tar.gz: 8ff3fb81b2800899126f1858c82fb560b8664b22f61242bfa999e5cbd8a325b1
5
+ SHA512:
6
+ metadata.gz: be50007c896e083f9c54d93d2d00c38af4211362997347f5caa80b4bfc8e98b54c1683b41fdb9c1c2eaabbe67c552b5ef760f79f3c93c97f7ceebd0a49b00793
7
+ data.tar.gz: 6e603c446b077a04ed4adbfde9490c9a30faf3a35345251d1ab3912ed8462a1901dedcb772ef8c4100b33344abd871e7f349d1bb23241256e700e15b4b234abf
@@ -0,0 +1,21 @@
1
+ version: 2.1
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: ruby:3.0.2
6
+ steps:
7
+ - checkout
8
+ - run:
9
+ name: Install gem dependencies
10
+ command: bundle install
11
+ - run:
12
+ name: Rubocop
13
+ command: bundle exec rubocop
14
+ - run:
15
+ name: RSpec Tests
16
+ command: |
17
+ bundle exec rspec --profile 10 \
18
+ --format RspecJunitFormatter \
19
+ --out test_results/rspec.xml \
20
+ --format documentation \
21
+ $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
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/AbcSize:
16
+ Enabled: false
17
+
18
+ Metrics/BlockLength:
19
+ Enabled: false
20
+
21
+ Metrics/CyclomaticComplexity:
22
+ Enabled: false
23
+
24
+ Metrics/MethodLength:
25
+ Enabled: false
26
+
27
+ Metrics/PerceivedComplexity:
28
+ Enabled: false
29
+
30
+ Naming/MethodParameterName:
31
+ Enabled: false
32
+
33
+ Style/Documentation:
34
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in extended_range.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rspec_junit_formatter"
11
+ gem "rubocop", "~> 1.7"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Anthony Felix Hernandez
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,43 @@
1
+ # ExtendedRange
2
+
3
+ [![hernanat](https://circleci.com/gh/hernanat/extended_range/tree/main.svg?style=svg)](https://circleci.com/gh/hernanat/extended_range/tree/main)
4
+ [![Gem Version](https://badge.fury.io/rb/extended_range.svg)](https://badge.fury.io/rb/extended_range)
5
+
6
+ `extended_range` is a set of extension class and instance methods to Ruby's `Range` class.
7
+
8
+ Read the [docs](https://rubydoc.info/github/hernanat/extended_range) for more information
9
+ about its capabilities and how to use it.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "extended_range"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install extended_range
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec`
30
+ to run the tests. You can also run `bin/console` for an interactive prompt that will allow
31
+ you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`.
34
+
35
+ ## Documentation
36
+
37
+ Documentation can be viewed at https://rubydoc.info/github/hernanat/extended_range
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hernanat/extended_range
42
+
43
+ TODO: contribution guidelines
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]
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 "extended_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,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/extended_range/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "extended_range"
7
+ spec.version = ExtendedRange::VERSION
8
+ spec.authors = ["Anthony Felix Hernandez"]
9
+ spec.email = ["ant@antfeedr.com"]
10
+
11
+ spec.summary = "Some handy methods for the `Range` class."
12
+ spec.homepage = "https://github.com/hernanat/extended_range"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+
31
+ spec.add_development_dependency "pry", "~> 0.14"
32
+ spec.add_development_dependency "yard", "~> 0.9"
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CoreExtensions
4
+ module Range
5
+ module Checks
6
+ # Checks whether or not `other` overlaps with `self`.
7
+ #
8
+ # @param [Range] other The range to check for overlap with `self`.
9
+ #
10
+ # @return [Boolean]
11
+ #
12
+ # @example Overlapping range
13
+ # (1..4).overlaps?(2..6) #=> true
14
+ #
15
+ # @example Non-overlapping range
16
+ # (1..4).overlaps?(5..7) #=> false
17
+ def overlaps?(other)
18
+ return true if self == other
19
+
20
+ if endless? || other.endless?
21
+ endless_range_overlaps?(other)
22
+ elsif self.end == other.begin || other.end == self.begin
23
+ # If `a.end` is the same as `b.begin`, but `a.exclude_end?` is true,
24
+ # then the ranges do not overlap. Similar in the other direction.
25
+ (self.end == other.begin && !exclude_end?) ||
26
+ (other.end == self.begin && !other.exclude_end?)
27
+ else
28
+ self.begin <= other.end && other.begin <= self.end
29
+ end
30
+ end
31
+
32
+ # Returns `true` if `self` is endless, `false` otherwise.
33
+ def endless?
34
+ self.end.nil?
35
+ end
36
+
37
+ # Returns `true` if `self` is beginless, `false` otherwise.
38
+ def beginless?
39
+ self.begin.nil?
40
+ end
41
+
42
+ private
43
+
44
+ # `other` is not necessarily endless, neither is `self`.
45
+ def endless_range_overlaps?(other)
46
+ # two endless ranges necessarily overlap.
47
+ if endless? && other.endless?
48
+ true
49
+ elsif endless?
50
+ # self or other is endless, check to see if they overlap by checking if the
51
+ # endless range's begin falls before the non-endless range's end. We also
52
+ # include a constraint to prevent open ranges whose end is the same as the
53
+ # second range's beginning from being considered as overlapping.
54
+ self.begin <= other.end && !other.exclude_end?
55
+ else
56
+ other.begin <= self.end && !exclude_end?
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "checks"
4
+
5
+ module CoreExtensions
6
+ module Range
7
+ module Operations
8
+ include Checks
9
+
10
+ def self.included(klass)
11
+ klass.extend(ClassMethods)
12
+ end
13
+
14
+ # Returns a range whose end is included in the range.
15
+ #
16
+ # @param delta The amount which should be subtracted from the range end when converting
17
+ # to a closed range.
18
+ #
19
+ # @return [Range] self if the range is already closed, otherwise return a new range
20
+ # where the `begin` is the same as `self`, and the `end` is `self.end - delta`.
21
+ #
22
+ # @example Convert an open time range to a closed time range.
23
+ # (Time.new(2020)...Time.new(2021)).to_closed_range #=> 2020-01-01 00:00:00 -0800..2020-12-31 23:59:59 -0800
24
+ #
25
+ # @example Convert an open time range to a closed time range and specify the delta.
26
+ # (Time.new(2020)...Time.new(2021)).to_closed_range(delta: Float::EPSILON)
27
+ # #=> 2020-01-01 00:00:00 -0800..2020-12-31 23:59:59 4503599627370495/4503599627370496 -0800
28
+ def to_closed_range(delta: 1)
29
+ return self unless exclude_end?
30
+
31
+ if delta.is_a?(Proc)
32
+ (self.begin..(delta.call(self.end)))
33
+ else
34
+ (self.begin..self.end - delta)
35
+ end
36
+ end
37
+
38
+ # Calls {ClassMethods#subtract} with `self` and `other`
39
+ #
40
+ # @param [Range] other The range to subtract.
41
+ #
42
+ # @return [Array<Range>] An array of ranges obtained from subtracting `other` from `self`.
43
+ def -(other)
44
+ self.class.subtract(self, other)
45
+ end
46
+
47
+ # Calls {ClassMethods#merge} with `self` and `*others`
48
+ #
49
+ # @param [Range] other The range to merge.
50
+ #
51
+ # @return [Array<Range>] An array of merged ranges.
52
+ def merge(other)
53
+ self.class.merge(self, other)
54
+ end
55
+
56
+ module ClassMethods
57
+ # Merge the given ranges together.
58
+ #
59
+ # @param [Array<Range>] ranges The ranges objects to merge.
60
+ #
61
+ # @return [Array<Range>] An array of potentially merged ranges.
62
+ #
63
+ # @example Merge two ranges.
64
+ # Range.merge(1..5, 2..6) #=> [1..6]
65
+ #
66
+ # @example Merge an endless range with a non-endless range.
67
+ # Range.merge(1..5, 2..) #=> [1..]
68
+ #
69
+ # @example Merge two endless ranges.
70
+ # Range.merge(1.., 2..) #=> [1..]
71
+ #
72
+ # @example Can't merge an open range with a range whose start is the same as the open range's end.
73
+ # Range.merge(1...3, 3..5) #=> [1...3, 3..5]
74
+ def merge(*ranges)
75
+ ranges.sort_by!(&:begin).inject([]) do |merged, range|
76
+ if merged.empty? || !range.overlaps?(merged.last)
77
+ merged.append(range)
78
+ else
79
+ last_range = merged.pop
80
+
81
+ merged.append(merge_ranges(last_range, range))
82
+ end
83
+ end
84
+ end
85
+
86
+ # Subtract an array of ranges from the given original range.
87
+ #
88
+ # @param [Range] original_range The range to subtract from.
89
+ #
90
+ # @param [Array<Range>] subtracted_ranges The ranges to subtract from `original_range`
91
+ #
92
+ # @param delta The amount which should be added to the begin of a resulting subrange in
93
+ # the event that the previous subrange end was open.
94
+ #
95
+ # @example Subtraction of closed, finite ranges.
96
+ # Range.subtract(1..20, 2..3, 4..6, 7..12) #=> [1...2, 13..20]
97
+ #
98
+ # @example Subtraction of a mixture of closed and open finite ranges.
99
+ # Range.subtract(1..20, 2..3, 4...6, 7..12) #=> [1...2, 6...7, 13..20]
100
+ #
101
+ # @example Subtraction of finite ranges from an endless range
102
+ # Range.subtract(1.., 2..3, 4...6, 7..12) #=> [1...2, 4...7, 13..]
103
+ #
104
+ # @example Subtraction of two endless ranges
105
+ # Range.subtract(1.., 10..) #=> [1...10]
106
+ # Range.subtract(10.., 1..) #=> []
107
+ def subtract(original_range, *subtracted_ranges, delta: 1)
108
+ subtracted_ranges.sort_by!(&:begin).inject([original_range]) do |result, subtracted_range|
109
+ if result.last&.overlaps?(subtracted_range)
110
+ result.append(*subtract_overlapping_range(result.pop, subtracted_range, delta: delta))
111
+ else
112
+ result
113
+ end
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ def merge_ranges(a, b)
120
+ exclude_end = should_merged_exclude_end?(a, b)
121
+ ::Range.new(a.begin, merged_range_end(a, b), exclude_end)
122
+ end
123
+
124
+ def should_merged_exclude_end?(a, b)
125
+ return true if a.exclude_end? && b.exclude_end?
126
+
127
+ # Deal with endless ranges
128
+ if a.endless? || b.endless?
129
+ # If `a.end` is `nil` and `a.exclude_end?` is `true`, then `b.exclude_end?` can't
130
+ # be `true`, so it suffices to check if `b.end` is not `nil`. If `b.end` is not `nil`,
131
+ # then the result is an endless range that excludes the end. If `b.end` *is* `nil`,
132
+ # then the result is an endless range that does *not* exclude the end (because
133
+ # we know from above that `b.exclude_end?` cannot be `true`.
134
+ #
135
+ # The same holds for the inverse statement (i.e. the RHS of the `||` below).
136
+ #
137
+ # The goal is to maintain logical consistency for for `exclude_end` even in
138
+ # the event that the result is an endless range.
139
+ (a.exclude_end? && a.endless? && !b.endless?) ||
140
+ (b.exclude_end? && b.endless? && !a.endless?)
141
+ else
142
+ (a.exclude_end? && b.end < a.end) || (b.exclude_end? && a.end < b.end)
143
+ end
144
+ end
145
+
146
+ def merged_range_end(a, b)
147
+ if a.endless? || b.endless?
148
+ nil
149
+ else
150
+ a.end > b.end ? a.end : b.end
151
+ end
152
+ end
153
+
154
+ # Subtract range `b` from range `a` and return the resulting range(s)
155
+ def subtract_overlapping_range(a, b, delta: 1)
156
+ return [] if a == b
157
+
158
+ result = []
159
+
160
+ result << (a.begin...b.begin) if a.begin < b.begin
161
+
162
+ # If `b` is endless then the entire rest of `a` will be gone.
163
+ return result if b.endless?
164
+
165
+ other_begin = if b.exclude_end?
166
+ b.end
167
+ else
168
+ delta.is_a?(Proc) ? delta.call(b.end) : b.end + delta
169
+ end
170
+
171
+ result << ::Range.new(other_begin, a.end, a.exclude_end?) if a.endless? || other_begin < a.end
172
+
173
+ result
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExtendedRange
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "extended_range/version"
4
+ require_relative "core_extensions/range/operations"
5
+ require_relative "core_extensions/range/checks"
6
+
7
+ Range.include CoreExtensions::Range::Operations
8
+ Range.include CoreExtensions::Range::Checks
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extended_range
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Felix Hernandez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ description:
42
+ email:
43
+ - ant@antfeedr.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".circleci/config.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - extended_range.gemspec
59
+ - lib/core_extensions/range/checks.rb
60
+ - lib/core_extensions/range/operations.rb
61
+ - lib/extended_range.rb
62
+ - lib/extended_range/version.rb
63
+ homepage: https://github.com/hernanat/extended_range
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/hernanat/extended_range
68
+ source_code_uri: https://github.com/hernanat/extended_range
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 3.0.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.2.22
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Some handy methods for the `Range` class.
88
+ test_files: []