edwagner-range-subset 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.
- data/README.rdoc +21 -0
- data/VERSION.yml +4 -0
- data/lib/core_ext/range.rb +39 -0
- data/lib/range_subset.rb +1 -0
- data/spec/range_subset_spec.rb +136 -0
- data/spec/spec_helper.rb +9 -0
- metadata +60 -0
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= range_subset
|
2
|
+
|
3
|
+
http://github.com/edwagner/range-subset
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A simple extension to Ruby's core Range class to determine overlaps between two Ranges.
|
8
|
+
|
9
|
+
== INSTALL:
|
10
|
+
|
11
|
+
[sudo] gem install edwagner-range-subset
|
12
|
+
|
13
|
+
or
|
14
|
+
|
15
|
+
git clone git://github.com/edwagner/range-subset.git
|
16
|
+
cd range-subset
|
17
|
+
rake install
|
18
|
+
|
19
|
+
== COPYRIGHT:
|
20
|
+
|
21
|
+
Copyright (c) 2009 Ed Wagner. See LICENSE for details.
|
data/VERSION.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Extend the Range class with some set-like operators
|
2
|
+
class Range
|
3
|
+
|
4
|
+
# Return a new Range that is the common interval of the two ranges,
|
5
|
+
# or nil if there is no overlap
|
6
|
+
def &(range)
|
7
|
+
range = range.to_range if range.respond_to?(:to_range)
|
8
|
+
return nil if range.begin > self.end || range.end < self.begin
|
9
|
+
return nil if range.begin == self.end && self.exclude_end?
|
10
|
+
return nil if range.end == self.begin && range.exclude_end?
|
11
|
+
|
12
|
+
if self.end < range.end
|
13
|
+
exclude_end = self.exclude_end?
|
14
|
+
elsif range.end < self.end
|
15
|
+
exclude_end = range.exclude_end?
|
16
|
+
else
|
17
|
+
exclude_end = self.exclude_end? || range.exclude_end?
|
18
|
+
end
|
19
|
+
Range.new([range.begin, self.begin].max, [range.end, self.end].min, exclude_end)
|
20
|
+
end
|
21
|
+
alias intersection &
|
22
|
+
|
23
|
+
# Returns true if this range is a subset of the given range
|
24
|
+
def subset?(range)
|
25
|
+
range = range.to_range if range.respond_to?(:to_range)
|
26
|
+
return false if self.begin < range.begin || self.end > range.end
|
27
|
+
return false if self.end == range.end && !self.exclude_end? && range.exclude_end?
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns true if this range is a superset of the given range
|
32
|
+
def superset?(range)
|
33
|
+
range = range.to_range if range.respond_to?(:to_range)
|
34
|
+
return false if self.begin > range.begin || self.end < range.end
|
35
|
+
return false if self.end == range.end && self.exclude_end? && !range.exclude_end?
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/lib/range_subset.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'core_ext/range'
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "A Range with subset extensions" do
|
4
|
+
# Intersection
|
5
|
+
describe "when asked to intersect 1..5 with 2..6" do
|
6
|
+
before(:each) do
|
7
|
+
@r = (1..5) & (2..6)
|
8
|
+
@r2 = (2..6) & (1..5)
|
9
|
+
end
|
10
|
+
it "should return 2..5" do
|
11
|
+
@r.should == (2..5)
|
12
|
+
@r2.should == (2..5)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "when asked to intersect 1...5 with 2..6" do
|
17
|
+
before(:each) do
|
18
|
+
@r = (1...5) & (2..6)
|
19
|
+
@r2 = (2..6) & (1...5)
|
20
|
+
end
|
21
|
+
it "should return 2...5" do
|
22
|
+
@r.should == (2...5)
|
23
|
+
@r2.should == (2...5)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "when asked to intersect 1..5 with 2...6" do
|
28
|
+
before(:each) do
|
29
|
+
@r = (1..5) & (2...6)
|
30
|
+
@r2 = (2...6) & (1..5)
|
31
|
+
end
|
32
|
+
it "should return 2..5" do
|
33
|
+
@r.should == (2..5)
|
34
|
+
@r2.should == (2..5)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "when asked to intersect 1..5 with 2..3" do
|
39
|
+
before(:each) do
|
40
|
+
@r = (1..5) & (2..3)
|
41
|
+
@r2 = (2..3) & (1..5)
|
42
|
+
end
|
43
|
+
it "should return 2..3" do
|
44
|
+
@r.should == (2..3)
|
45
|
+
@r2.should == (2..3)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "when asked to intersect 1..5 with 2...3" do
|
50
|
+
before(:each) do
|
51
|
+
@r = (1..5) & (2...3)
|
52
|
+
@r2 = (2...3) & (1..5)
|
53
|
+
end
|
54
|
+
it "should return 2...3" do
|
55
|
+
@r.should == (2...3)
|
56
|
+
@r2.should == (2...3)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "when asked to intersect 1...5 with 2..3" do
|
61
|
+
before(:each) do
|
62
|
+
@r = (1...5) & (2..3)
|
63
|
+
@r2 = (2..3) & (1...5)
|
64
|
+
end
|
65
|
+
it "should return 2..3" do
|
66
|
+
@r.should == (2..3)
|
67
|
+
@r2.should == (2..3)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "when asked to intersect 1..5 with 7..10" do
|
72
|
+
before(:each) do
|
73
|
+
@r = (1..5) & (7..10)
|
74
|
+
@r2 = (7..10) & (1..5)
|
75
|
+
end
|
76
|
+
it "should return nil" do
|
77
|
+
@r.should == nil
|
78
|
+
@r2.should == nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "when asked to intersect 1..5 with 5..7" do
|
83
|
+
before(:each) do
|
84
|
+
@r1 = (1..5)
|
85
|
+
@r2 = (5..7)
|
86
|
+
@r12 = @r1 & @r2
|
87
|
+
@r21 = @r2 & @r1
|
88
|
+
end
|
89
|
+
it "should return 5..5" do
|
90
|
+
@r12.should == (5..5)
|
91
|
+
@r21.should == (5..5)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "when asked to intersect 1...5 with 5..7" do
|
96
|
+
before(:each) do
|
97
|
+
@r1 = (1...5)
|
98
|
+
@r2 = (5..7)
|
99
|
+
@r12 = @r1 & @r2
|
100
|
+
@r21 = @r2 & @r1
|
101
|
+
end
|
102
|
+
it "should return nil" do
|
103
|
+
@r12.should == nil
|
104
|
+
@r21.should == nil
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Comparisons
|
109
|
+
describe "when asked to compare ranges," do
|
110
|
+
it "1..5 should be a superset of 2..3" do
|
111
|
+
(1..5).should be_superset(2..3)
|
112
|
+
end
|
113
|
+
it "1..5 should be a superset of 2..5" do
|
114
|
+
(1..5).should be_superset(2..5)
|
115
|
+
end
|
116
|
+
it "1..5 should be a superset of 2...5" do
|
117
|
+
(1..5).should be_superset(2...5)
|
118
|
+
end
|
119
|
+
it "1...5 should not be a superset of 2..5" do
|
120
|
+
(1...5).should_not be_superset(2..5)
|
121
|
+
end
|
122
|
+
it "2..3 should be a subset of 1..5" do
|
123
|
+
(2..3).should be_subset(1..5)
|
124
|
+
end
|
125
|
+
it "2..5 should be a subset of 1..5" do
|
126
|
+
(2..5).should be_subset(1..5)
|
127
|
+
end
|
128
|
+
it "2...5 should be a subset of 1..5" do
|
129
|
+
(2...5).should be_subset(1..5)
|
130
|
+
end
|
131
|
+
it "2..5 should not be a subset of 1...5" do
|
132
|
+
(2..5).should_not be_subset(1...5)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: edwagner-range-subset
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ed Wagner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-18 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A simple extension to Ruby's core Range class to determine overlaps between two Ranges.
|
17
|
+
email: ed@edwagner.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/core_ext
|
28
|
+
- lib/core_ext/range.rb
|
29
|
+
- lib/range_subset.rb
|
30
|
+
- spec/range_subset_spec.rb
|
31
|
+
- spec/spec_helper.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/edwagner/range-subset
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --inline-source
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: A simple extension to Ruby's core Range class to determine overlaps between two Ranges.
|
59
|
+
test_files: []
|
60
|
+
|