range_operators 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010
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/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ LICENSE
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ lib/range_operators.rb
6
+ lib/rangify.rb
7
+ spec/range_operators_spec.rb
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = RangeOperators
2
+
3
+ This gem will mixin range operations (addition and subtraction) into Ruby's Range class.
4
+
5
+ It requires that the second operand have a '- (integer)' method defined (ie. Fixnum, Bignum, and Date).
6
+ (This allows the determination of a previous/predecessor value of an object.)
7
+
8
+ It assumes inclusive ranges (ie. 1..4) and range.first <= range.last.
9
+
10
+ Values returned from the operations are in an array.
11
+
12
+ = Usage
13
+
14
+ The methods added are Range#- (alias Range#minus) and Range#+ (alias Range#plus).
15
+
16
+
17
+ Examples:
18
+
19
+ irb > require 'range_operators'
20
+
21
+ irb > (1..10) - (4..6) => [1..3, 7..10]
22
+
23
+ irb > (1..10).minus(9..12) => [1..8]
24
+
25
+ irb > (1..10) + (9..12) => [1..12]
26
+
27
+ irb > (1..10).plus(15..20) => [1..10, 15..20]
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('range_operators', '0.0.1') do |p|
6
+ p.description = "A gem that adds addition and subtraction to Ruby's Range class."
7
+ p.url = "http://github.com/monocle/range_operators"
8
+ p.author = "monocle"
9
+ p.email = "frappez_2000@yahoo.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
@@ -0,0 +1,47 @@
1
+ #Will add range operations ('+' and '-') to Ruby's Range class.
2
+ #Requires that the second operand have a '- (integer)' method defined (ie. Fixnum, Bignum, and Date).
3
+ #Assumes inclusive ranges (ie. 1..4) and range.first <= range.last.
4
+ #Values returned from the operations are in an array.
5
+ module RangeOperators
6
+ require 'rangify'
7
+
8
+ def +(value)
9
+ [self, value].rangify
10
+ end
11
+
12
+ alias plus +
13
+
14
+ def -(value)
15
+ if value.class == self.first.class
16
+ self.minus_obj(value)
17
+ else
18
+ [self.minus_obj(value.first)[0], self.minus_obj(value.last)[1]].compact
19
+ end
20
+ end
21
+
22
+ alias minus -
23
+
24
+ protected
25
+
26
+ def minus_obj(value)
27
+ first = case value <=> self.first.succ
28
+ when -1 then nil
29
+ when 0 then self.first
30
+ else
31
+ value < self.last.succ ? self.first..(value - 1) : self
32
+ end
33
+
34
+ second = case self.last <=> value.succ
35
+ when -1 then nil
36
+ when 0 then self.last
37
+ else
38
+ value.succ > self.first ? value.succ..self.last : self
39
+ end
40
+
41
+ [first, second]
42
+ end
43
+ end
44
+
45
+ class Range
46
+ include RangeOperators
47
+ end
data/lib/rangify.rb ADDED
@@ -0,0 +1,52 @@
1
+ #= Rangify
2
+ #
3
+ #This gem will add the rangify method to Ruby's Array class.
4
+ #The rangify method will produce appropriate ranges from the objects in the array.
5
+ #
6
+ #= Usage
7
+ #
8
+ #Examples:
9
+ #
10
+ #[1,2,3,6,7,8].rangify = [1..3, 6..8]
11
+ #
12
+ #[10..15, 16..20, 21, 22].rangify = [10..22]
13
+ #
14
+ #
15
+ #Assumes inclusive ranges (ie. 1..4) and range.first <= range.last.
16
+ #
17
+ #Works with integers, dates and strings. However, all the objects in the array must be of the same class.
18
+
19
+ module Rangify
20
+ def rangify
21
+ array = self.uniq.sort_by { |element| comparison_value(element, :first) }
22
+ array.inject([]) do |collection, value|
23
+ unless collection.empty?
24
+ last = collection.last
25
+ last_value = comparison_value(last, :last)
26
+ current_value = comparison_value(value, :first)
27
+ if (last_value.succ <=> current_value) == -1
28
+ collection << value
29
+ else
30
+ first = [comparison_value(last, :first), comparison_value(value, :first)].min
31
+ second = [comparison_value(last, :last), comparison_value(value, :last)].max
32
+ collection[-1] = [first..second]
33
+ collection.flatten!
34
+ end
35
+ else
36
+ collection << value
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ # For a Range, will return value.first or value.last. A non-Range will return itself.
44
+ def comparison_value(value, position)
45
+ return value if value.class != Range
46
+ position == :first ? value.first : value.last
47
+ end
48
+ end
49
+
50
+ class Array
51
+ include Rangify
52
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{range_operators}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["monocle"]
9
+ s.cert_chain = ["/ruby/keys/range_operators/gem-public_cert.pem"]
10
+ s.date = %q{2010-07-03}
11
+ s.description = %q{A gem that adds addition and subtraction to Ruby's Range class.}
12
+ s.email = %q{frappez_2000@yahoo.com}
13
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc", "lib/range_operators.rb", "lib/rangify.rb"]
14
+ s.files = ["LICENSE", "Manifest", "README.rdoc", "Rakefile", "lib/range_operators.rb", "lib/rangify.rb", "spec/range_operators_spec.rb", "range_operators.gemspec"]
15
+ s.homepage = %q{http://github.com/monocle/range_operators}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Range_operators", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{range_operators}
19
+ s.rubygems_version = %q{1.3.6}
20
+ s.signing_key = %q{/ruby/keys/range_operators/gem-private_key.pem}
21
+ s.summary = %q{A gem that adds addition and subtraction to Ruby's Range class.}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
@@ -0,0 +1,180 @@
1
+ require 'lib/range_operators'
2
+
3
+ describe RangeOperators do
4
+ describe '#+' do
5
+ describe 'when given a range of integers' do
6
+ before :each do
7
+ @range = 1..10
8
+ end
9
+
10
+ it '1..10 + 11 = [1..11]' do
11
+ (@range + 11).should == [1..11]
12
+ end
13
+
14
+ it '1..10 + 12 = [1..10, 12]' do
15
+ (@range + 12).should == [1..10, 12]
16
+ end
17
+
18
+ it '1..10 + 10..12 = [1..12]' do
19
+ (@range + (10..12)).should == [1..12]
20
+ end
21
+
22
+ it '1..10 + 11..12 = [1..12]' do
23
+ (@range + (11..12)).should == [1..12]
24
+ end
25
+
26
+ it '1..10 + 9..12 = [1..12]' do
27
+ (@range + (9..12)).should == [1..12]
28
+ end
29
+
30
+ it '1..10 + 1..12 = [1..12]' do
31
+ (@range + (1..12)).should == [1..12]
32
+ end
33
+
34
+ it '1..10 + 12..20 = [1..10, 12..20]' do
35
+ (@range + (12..20)).should == [1..10, 12..20]
36
+ end
37
+
38
+ it '1..10 + 0 = [0..10]' do
39
+ (@range + 0).should == [0..10]
40
+ end
41
+
42
+ it '1..10 + -1 = [-1, 1..10]' do
43
+ (@range + -1).should == [-1, 1..10]
44
+ end
45
+
46
+ it '1..10 + (-10..-1) = [-10..-1, 1..10]' do
47
+ (@range + (-10..-1)).should == [-10..-1, 1..10]
48
+ end
49
+
50
+ it '1..10 + (-10..0) = [-10..10]' do
51
+ (@range + (-10..0)).should == [-10..10]
52
+ end
53
+
54
+ it '1..10 + (-10..1) = [-10..10]' do
55
+ (@range + (-10..1)).should == [-10..10]
56
+ end
57
+
58
+ it '1..10 + (-10..2) = [-10..10]' do
59
+ (@range + (-10..2)).should == [-10..10]
60
+ end
61
+
62
+ it '1..10 + (-10..20) = [-10..20]' do
63
+ (@range + (-10..20)).should == [-10..20]
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '#-' do
69
+ describe 'when given a range of integers' do
70
+ before :each do
71
+ @range = 1..10
72
+ end
73
+
74
+ it 'should raise error if subtracting a non-integer' do
75
+ lambda{@range - 'a'}.should raise_error
76
+ end
77
+
78
+ describe 'and subtracting an integer' do
79
+ it '(1..10) - 5 = [1..4, 6..10]' do
80
+ (@range - 5).should == [1..4, 6..10]
81
+ end
82
+
83
+ it '(1..10) - 2 = [1, 3..10]' do
84
+ (@range - 2).should == [1, 3..10]
85
+ end
86
+
87
+ it '(1..10)- 9 = [1..8, 10]' do
88
+ (@range - 9).should == [1..8, 10]
89
+ end
90
+
91
+ it '(1..10) - 10 = [1..9, nil]' do
92
+ (@range - 10).should == [1..9, nil]
93
+ end
94
+
95
+ it '(1..10) - 1 = [nil, 2..10]' do
96
+ (@range - 1).should == [nil, 2..10]
97
+ end
98
+
99
+ it '(1..10) - 11 = [1..10, nil]' do
100
+ (@range - 11).should == [1..10, nil]
101
+ end
102
+
103
+ it '(1..10) - 12 = [1..10, nil]' do
104
+ (@range - 12).should == [1..10, nil]
105
+ end
106
+
107
+ it '(1..10) - 0 = [nil, 1..10]' do
108
+ (@range - 0).should == [nil, 1..10]
109
+ end
110
+
111
+ it '(1..10) - -1 = [nil, 1..10]' do
112
+ (@range - -1).should == [nil, 1..10]
113
+ end
114
+ end
115
+
116
+ describe 'and subtracting another range' do
117
+
118
+ it '1..10 - 4..6 = [1..3, 7..10]' do
119
+ (@range - (4..6)).should == [1..3, 7..10]
120
+ end
121
+
122
+ it '1..10 - 2..6 = [1, 7..10]' do
123
+ (@range - (2..6)).should == [1, 7..10]
124
+ end
125
+
126
+ it '1..10 - 4..9 = [1..3, 10]' do
127
+ (@range - (4..9)).should == [1..3, 10]
128
+ end
129
+
130
+ it '1..10 - 2..9 = [1, 10]' do
131
+ (@range - (2..9)).should == [1, 10]
132
+ end
133
+
134
+ it '1..10 - 2..11 = [1]' do
135
+ (@range - (2..11)).should == [1]
136
+ end
137
+
138
+ it '1..10 - 0..9 = [10]' do
139
+ (@range - (0..9)).should == [10]
140
+ end
141
+
142
+ it '1..10 - 4..10 = [1..3]' do
143
+ (@range - (4..10)).should == [1..3]
144
+ end
145
+
146
+ it '1..10 - 4..12 = [1..3]' do
147
+ (@range - (4..12)).should == [1..3]
148
+ end
149
+
150
+ it '1..10 - 1..6 = [7..10]' do
151
+ (@range - (1..6)).should == [7..10]
152
+ end
153
+
154
+ it '1..10 - (-2..6) = [7..10]' do
155
+ (@range - (-2..6)).should == [7..10]
156
+ end
157
+
158
+ it '1..10 - 11..20 = [1..10]' do
159
+ (@range - (11..20)).should == [1..10]
160
+ end
161
+
162
+ it '1..10 - (-10..0) = [1..10]' do
163
+ (@range - (-10..0)).should == [1..10]
164
+ end
165
+
166
+ it '1..10 - (-10..20) = []' do
167
+ (@range - (-10..20)).should == []
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ it '#minus is an alias of #-' do
174
+ RangeOperators.instance_method(:minus).should == RangeOperators.instance_method(:-)
175
+ end
176
+
177
+ it '#plus is an alias of #+' do
178
+ RangeOperators.instance_method(:plus).should == RangeOperators.instance_method(:+)
179
+ end
180
+ end
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: range_operators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - monocle
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ - |
17
+ -----BEGIN CERTIFICATE-----
18
+ MIIDOjCCAiKgAwIBAgIBADANBgkqhkiG9w0BAQUFADBDMRUwEwYDVQQDDAxmcmFw
19
+ cGV6XzIwMDAxFTATBgoJkiaJk/IsZAEZFgV5YWhvbzETMBEGCgmSJomT8ixkARkW
20
+ A2NvbTAeFw0xMDA3MDQwMzE1NDlaFw0xMTA3MDQwMzE1NDlaMEMxFTATBgNVBAMM
21
+ DGZyYXBwZXpfMjAwMDEVMBMGCgmSJomT8ixkARkWBXlhaG9vMRMwEQYKCZImiZPy
22
+ LGQBGRYDY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvItTt36D
23
+ sudZebh5I4GSDLEU+eWS2rUP4K8M86AQiwu/39Uro7ZxFbreHIkFrZskLloT+yo9
24
+ pJS9ng8BKttTjL8OOkX/29Fi258hOJ/Y1LT/Lc+M1LT1n4GqQClZzlYeAlLlVDrr
25
+ fG4TiA0uZUXI4YDBhPWcfUsQierGcW62PpU2h3iH/b8I7+vxV+YU2NIkBe9QsNm6
26
+ U3Qini4ASAn9/B/OQkW41BdywXq9uHLRzGFwoxR9zUAgKYOn3i0qUAWWTSoDdR1C
27
+ GrmY/rNIM5skCFFSDWp9a7Uc0abHeb4FBhSAkpOud4+007MyXbt6tar/kNzhE/I7
28
+ buUX5u9ZCvjSywIDAQABozkwNzAJBgNVHRMEAjAAMB0GA1UdDgQWBBTFXNz226Hp
29
+ rqbDtxebnFGqSwgIqTALBgNVHQ8EBAMCBLAwDQYJKoZIhvcNAQEFBQADggEBADVg
30
+ ROLUTcNYRy9B5qCgZCKr3NnkKXSCmbQyG3T+9Iy5t7KcaDVQOkuuUpMi62j4Slor
31
+ Wa6zqcUThZvrs84nbnsrGtQhLzGYFBp3D/3teeXHmXTq8Xwes0kbUi7JtAE2Z1IK
32
+ 98N4/kUg8qN+UQXdO7rODfD4tupYj9IzHOyYIrNgQxgEmzEGfAKE1qzAMgmTHOGz
33
+ M6J99Rayo0SoYJCoRCi4aX1dpZnEzfT1RGc2IlkCWdzWkcLvJFBSKtDdjnpG/9SB
34
+ GEIH3y7N759WvBM49n8gsO+yclEacYeJlyaEg1Z+hhmYPykUpDXzb39Vd7kbWmkp
35
+ lTblHT4UBS6VZYs6PY8=
36
+ -----END CERTIFICATE-----
37
+
38
+ date: 2010-07-03 00:00:00 -07:00
39
+ default_executable:
40
+ dependencies: []
41
+
42
+ description: A gem that adds addition and subtraction to Ruby's Range class.
43
+ email: frappez_2000@yahoo.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - LICENSE
50
+ - README.rdoc
51
+ - lib/range_operators.rb
52
+ - lib/rangify.rb
53
+ files:
54
+ - LICENSE
55
+ - Manifest
56
+ - README.rdoc
57
+ - Rakefile
58
+ - lib/range_operators.rb
59
+ - lib/rangify.rb
60
+ - spec/range_operators_spec.rb
61
+ - range_operators.gemspec
62
+ has_rdoc: true
63
+ homepage: http://github.com/monocle/range_operators
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --line-numbers
69
+ - --inline-source
70
+ - --title
71
+ - Range_operators
72
+ - --main
73
+ - README.rdoc
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 1
89
+ - 2
90
+ version: "1.2"
91
+ requirements: []
92
+
93
+ rubyforge_project: range_operators
94
+ rubygems_version: 1.3.6
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: A gem that adds addition and subtraction to Ruby's Range class.
98
+ test_files: []
99
+
metadata.gz.sig ADDED
Binary file