rangehash 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/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-12-29
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/rangehash.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_helper.rb
11
+ test/test_rangehash.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on rangehash, see http://rangehash.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = rangehash
2
+
3
+ * http://github.com/verdammelt/range-hash
4
+
5
+ == DESCRIPTION:
6
+
7
+ Simple class which uses Ranges (and Fixnums) as keys in for a hash.
8
+ Then look ups can be done with FixNum and if the FixNum is included in
9
+ one of the hashes that value is returned.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ * RangeHash keys are Ranges or Fixnums
14
+
15
+ == SYNOPSIS:
16
+
17
+ <code>
18
+ hash = RangeHash.new
19
+ hash[10..20] = 'foo'
20
+ hash[21..22] = 'bar'
21
+ val = hash[15] # val == 'foo'
22
+ </code>
23
+
24
+ == REQUIREMENTS:
25
+
26
+ == INSTALL:
27
+
28
+ sudo gem install range-hash
29
+
30
+ == LICENSE:
31
+
32
+ (The MIT License)
33
+
34
+ Copyright (c) 2010 Mark Simpson
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining
37
+ a copy of this software and associated documentation files (the
38
+ 'Software'), to deal in the Software without restriction, including
39
+ without limitation the rights to use, copy, modify, merge, publish,
40
+ distribute, sublicense, and/or sell copies of the Software, and to
41
+ permit persons to whom the Software is furnished to do so, subject to
42
+ the following conditions:
43
+
44
+ The above copyright notice and this permission notice shall be
45
+ included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
48
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
50
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
51
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
52
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
53
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/rangehash'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'rangehash' do
14
+ self.developer 'Mark Simpson', 'verdammelt@gmail.com'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
data/lib/rangehash.rb ADDED
@@ -0,0 +1,36 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Rangehash
5
+ VERSION = '0.0.1'
6
+ end
7
+
8
+ class RangeHash
9
+ def initialize (key_value_hash, default_value=nil)
10
+ @key_value_hash = key_value_hash || Hash.new()
11
+ @default_item = [nil, default_value]
12
+ end
13
+
14
+ def [](key)
15
+ (@key_value_hash.find{ |kv| kv[0] === key } || @default_item)[1]
16
+ end
17
+
18
+ def to_s
19
+ @key_value_hash.to_s
20
+ end
21
+
22
+ def sorted_keys
23
+ @key_value_hash.keys.sort do |a, b|
24
+ sort_key(a) <=> sort_key(b)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def sort_key(a)
31
+ return a.first if Range === a
32
+ a
33
+ end
34
+ end
35
+
36
+
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rangehash.rb'}"
9
+ puts "Loading rangehash gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/rangehash'
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestRangeHash < Test::Unit::TestCase
4
+ def test_construction_from_hash
5
+ hash = { 0..2 => :foo, 3..5 => :bar }
6
+ r = RangeHash.new(hash)
7
+ assert_not_nil(r)
8
+ assert_equal(hash.to_s, r.to_s)
9
+ end
10
+
11
+ def test_get_value_for_key_in_a_range
12
+ hash = { 0..2 => :foo, 3..5 => :bar }
13
+ r = RangeHash.new(hash)
14
+ assert_equal(:foo, r[1])
15
+ assert_equal(:bar, r[4])
16
+ end
17
+
18
+ def test_with_keys_that_have_excluded_endpoint
19
+ hash = { 0..2 => :foo, 3...5 => :bar, 5..10 => :baz }
20
+ r = RangeHash.new(hash)
21
+ assert_equal(:baz, r[5])
22
+ end
23
+
24
+ def test_default_value
25
+ hash = { 0..2 => :foo, 3..5 => :bar }
26
+ default_value = :quux
27
+ r = RangeHash.new(hash, default_value)
28
+ assert_equal(:quux, r[4568])
29
+ end
30
+
31
+ def test_bare_number_as_key
32
+ hash = { 0..2 => :foo, 3 => :bar, 4..6 => :baz}
33
+ r = RangeHash.new(hash)
34
+ assert_equal(:bar, r[3])
35
+ end
36
+
37
+ def test_sorted_keys_simple_case
38
+ hash = { 7..10 => :baz, 0..2 => :foo, 4..6 => :bar}
39
+ r = RangeHash.new(hash)
40
+ assert_equal([0..2, 4..6, 7..10], r.sorted_keys)
41
+ end
42
+
43
+ def test_sorted_keys_overlapping
44
+ hash = { 5..10 => :baz, 0..2 => :foo, 4..6 => :bar}
45
+ r = RangeHash.new(hash)
46
+ assert_equal([0..2, 4..6, 5..10], r.sorted_keys)
47
+ end
48
+
49
+ def test_sorted_keys_with_bare_fixnums
50
+ hash = { 5..10 => :baz, 0..2 => :foo, 3 => :quux, 4..6 => :bar}
51
+ r = RangeHash.new(hash)
52
+ assert_equal([0..2, 3, 4..6, 5..10], r.sorted_keys)
53
+ end
54
+ end
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rangehash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mark Simpson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain:
17
+ - |
18
+ -----BEGIN CERTIFICATE-----
19
+ MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMRMwEQYDVQQDDAp2ZXJk
20
+ YW1tZWx0MRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZFgNj
21
+ b20wHhcNMTAxMjI5MjAxNjI3WhcNMTExMjI5MjAxNjI3WjBBMRMwEQYDVQQDDAp2
22
+ ZXJkYW1tZWx0MRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZ
23
+ FgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQQsRTtwd0rN0X
24
+ ffw6plu3tUBiGXpRGWOlIZq6xjyAjjvmwMWnmwU5juNBlPJZzYiMmCIC821s3fgb
25
+ TM69f/cgqeevyI/h4Soxl2Le8NVGSeyvaZSNM8s1n9EQ5kN7VogYD0DbB4kHlfCM
26
+ JgD72juXiAKXnsXH8NvcBXCkoPiq7CjDJCmrqilTu1atELmP5RJ/Vm+Nt2odVDF5
27
+ t2x2ANmLVOZn59BTvHNXYbwrCIs82W4SwOSF4wL/I5ElH7dzr8PZN1yNBZPHtxpe
28
+ +4eTt6c4+DSP+rCOjLxBQxGPemY54zVawVtg4LjE/oOaAri99hz1Nl+PLgXg+oEP
29
+ veV2pcnDAgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW
30
+ BBTJ6wHlTv0Ievjg5o1zGil1w639SjANBgkqhkiG9w0BAQUFAAOCAQEAfXKfM9vA
31
+ u1ukuGjhd4mej3Daw8bnaX2URwy5/4ogP5+9rK+WTni5UpS09ksvN/I/PWnfwNWE
32
+ kzV0tl0DvQMpz+obpw02pt1uEzAHyPOYDGrgRweVaJbvrH2G3xAkSBwfkaDlzSrk
33
+ KomP5Nct1Nnkmg0mAAGjvS/ib3GbEANt8qSP6hfn6vEXMUnhnCmo69dbdqDwVDOJ
34
+ FVV9Kd1o7T8NmmDdn+G26uPPAX5EeJtyB1rV6pSPkQW99opIKcz6iEPNkAtVniwM
35
+ AM5LT+EO74YD568fpYjJUw+T4/TsPJBWyU/f21QoaRXI+x3VNSEqDZEf67Lsp2Ew
36
+ rPnJW2hNpPLFrA==
37
+ -----END CERTIFICATE-----
38
+
39
+ date: 2010-12-29 00:00:00 -05:00
40
+ default_executable:
41
+ dependencies:
42
+ - !ruby/object:Gem::Dependency
43
+ name: rubyforge
44
+ prerelease: false
45
+ requirement: &id001 !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 7
51
+ segments:
52
+ - 2
53
+ - 0
54
+ - 4
55
+ version: 2.0.4
56
+ type: :development
57
+ version_requirements: *id001
58
+ - !ruby/object:Gem::Dependency
59
+ name: gemcutter
60
+ prerelease: false
61
+ requirement: &id002 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 5
67
+ segments:
68
+ - 0
69
+ - 6
70
+ - 1
71
+ version: 0.6.1
72
+ type: :development
73
+ version_requirements: *id002
74
+ - !ruby/object:Gem::Dependency
75
+ name: hoe
76
+ prerelease: false
77
+ requirement: &id003 !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 27
83
+ segments:
84
+ - 2
85
+ - 5
86
+ - 0
87
+ version: 2.5.0
88
+ type: :development
89
+ version_requirements: *id003
90
+ description: |-
91
+ Simple class which uses Ranges (and Fixnums) as keys in for a hash.
92
+ Then look ups can be done with FixNum and if the FixNum is included in
93
+ one of the hashes that value is returned.
94
+ email:
95
+ - verdammelt@gmail.com
96
+ executables: []
97
+
98
+ extensions: []
99
+
100
+ extra_rdoc_files:
101
+ - History.txt
102
+ - Manifest.txt
103
+ - PostInstall.txt
104
+ files:
105
+ - History.txt
106
+ - Manifest.txt
107
+ - PostInstall.txt
108
+ - README.rdoc
109
+ - Rakefile
110
+ - lib/rangehash.rb
111
+ - script/console
112
+ - script/destroy
113
+ - script/generate
114
+ - test/test_helper.rb
115
+ - test/test_rangehash.rb
116
+ has_rdoc: true
117
+ homepage: http://github.com/verdammelt/range-hash
118
+ licenses: []
119
+
120
+ post_install_message: PostInstall.txt
121
+ rdoc_options:
122
+ - --main
123
+ - README.rdoc
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ hash: 3
141
+ segments:
142
+ - 0
143
+ version: "0"
144
+ requirements: []
145
+
146
+ rubyforge_project: rangehash
147
+ rubygems_version: 1.3.7
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: Simple class which uses Ranges (and Fixnums) as keys in for a hash
151
+ test_files:
152
+ - test/test_helper.rb
153
+ - test/test_rangehash.rb
metadata.gz.sig ADDED
Binary file