rangehash 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/.rspec +2 -0
- data/History.txt +5 -0
- data/Manifest.txt +5 -2
- data/README.rdoc +6 -7
- data/Rakefile +13 -6
- data/lib/rangehash.rb +5 -1
- data/spec/rangehash_spec.rb +59 -0
- data/spec/spec_helper.rb +11 -0
- data/tasks/rspec.rake +21 -0
- data/todo.org +2 -0
- metadata +10 -8
- metadata.gz.sig +0 -0
- data/test/test_helper.rb +0 -3
- data/test/test_rangehash.rb +0 -54
data.tar.gz.sig
CHANGED
Binary file
|
data/.rspec
ADDED
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
.rspec
|
1
2
|
History.txt
|
2
3
|
Manifest.txt
|
3
4
|
PostInstall.txt
|
@@ -7,5 +8,7 @@ lib/rangehash.rb
|
|
7
8
|
script/console
|
8
9
|
script/destroy
|
9
10
|
script/generate
|
10
|
-
|
11
|
-
|
11
|
+
spec/rangehash_spec.rb
|
12
|
+
spec/spec_helper.rb
|
13
|
+
tasks/rspec.rake
|
14
|
+
todo.org
|
data/README.rdoc
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
= rangehash
|
2
2
|
|
3
3
|
* http://github.com/verdammelt/range-hash
|
4
|
+
* https://rubygems.org/gems/rangehash
|
4
5
|
|
5
6
|
== DESCRIPTION:
|
6
7
|
|
@@ -14,18 +15,16 @@ one of the hashes that value is returned.
|
|
14
15
|
|
15
16
|
== SYNOPSIS:
|
16
17
|
|
17
|
-
|
18
|
-
hash =
|
19
|
-
hash[
|
20
|
-
hash[
|
21
|
-
val = hash[15] # val == 'foo'
|
22
|
-
</code>
|
18
|
+
hash = RangeHash.new
|
19
|
+
hash[10..20] = 'foo'
|
20
|
+
hash[21..22] = 'bar'
|
21
|
+
val = hash[15] # val == 'foo'
|
23
22
|
|
24
23
|
== REQUIREMENTS:
|
25
24
|
|
26
25
|
== INSTALL:
|
27
26
|
|
28
|
-
sudo gem install range-hash
|
27
|
+
sudo gem install range-hash
|
29
28
|
|
30
29
|
== LICENSE:
|
31
30
|
|
data/Rakefile
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
1
2
|
require 'rubygems'
|
2
3
|
gem 'hoe', '>= 2.1.0'
|
3
4
|
require 'hoe'
|
@@ -13,14 +14,20 @@ Hoe.plugin :newgem
|
|
13
14
|
$hoe = Hoe.spec 'rangehash' do
|
14
15
|
self.developer 'Mark Simpson', 'verdammelt@gmail.com'
|
15
16
|
self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
16
|
-
self.rubyforge_name = self.name
|
17
|
-
|
18
|
-
|
17
|
+
self.rubyforge_name = self.name
|
18
|
+
self.test_globs = ["spec/**/*_spec.rb"] #["test/**/test_*.rb", "spec/**/*_spec.rb"]
|
19
19
|
end
|
20
20
|
|
21
21
|
require 'newgem/tasks'
|
22
22
|
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
require 'rake'
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
|
27
|
+
remove_task :rcov
|
28
|
+
|
29
|
+
desc "Run all specs with RCov"
|
30
|
+
RSpec::Core::RakeTask.new(:rcov) do |t|
|
31
|
+
t.rcov = true
|
32
|
+
t.rcov_opts = ['--text-report', '--save', 'coverage.info', '--exclude', 'spec_helper', '--exclude', '^/']
|
33
|
+
end
|
data/lib/rangehash.rb
CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
4
|
module Rangehash
|
5
|
-
VERSION = '0.0.
|
5
|
+
VERSION = '0.0.2'
|
6
6
|
end
|
7
7
|
|
8
8
|
class RangeHash
|
@@ -15,6 +15,10 @@ class RangeHash
|
|
15
15
|
(@key_value_hash.find{ |kv| kv[0] === key } || @default_item)[1]
|
16
16
|
end
|
17
17
|
|
18
|
+
def []=(key, value)
|
19
|
+
@key_value_hash[key] = value
|
20
|
+
end
|
21
|
+
|
18
22
|
def to_s
|
19
23
|
@key_value_hash.to_s
|
20
24
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
# Time to add your specs!
|
4
|
+
# http://rspec.info/
|
5
|
+
describe RangeHash do
|
6
|
+
describe "retrieval by key:" do
|
7
|
+
it "should allow bare fixnum keys" do
|
8
|
+
rh = RangeHash.new({ 1 => :foo })
|
9
|
+
rh[1].should == :foo
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should allow ranges as keys" do
|
13
|
+
rh = RangeHash.new({ 1..3 => :foo })
|
14
|
+
rh[2].should == :foo
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return default value if key not found" do
|
18
|
+
rh = RangeHash.new({ 1..3 => :foo}, :bar)
|
19
|
+
rh[42].should == :bar
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "initialization:" do
|
24
|
+
it "should have default value of nil" do
|
25
|
+
rh = RangeHash.new({ 1..3 => :foo})
|
26
|
+
rh[42].should == nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should allow nil initial value" do
|
30
|
+
rh = RangeHash.new(nil)
|
31
|
+
rh.to_s.should == ""
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "adding keys and values: " do
|
36
|
+
it "should allow adding new fixnum keys dynamically" do
|
37
|
+
rh = RangeHash.new(nil)
|
38
|
+
rh[42] = :answer
|
39
|
+
rh[42].should == :answer
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should allow adding new range keys dynamically" do
|
43
|
+
rh = RangeHash.new(nil)
|
44
|
+
rh[1..3] = :foo
|
45
|
+
rh[2].should == :foo
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should replace existing key-value pair when adding" do
|
49
|
+
rh = RangeHash.new({ 1 => :foo})
|
50
|
+
rh[1] = :bar
|
51
|
+
rh[1].should == :bar
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should allow retrieval of sorted keys" do
|
56
|
+
rh = RangeHash.new({ 10..20 => :foo, 2 => :bar, 0 => :baz })
|
57
|
+
rh.sorted_keys.should == [0, 2, 10..20]
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# begin
|
2
|
+
# require 'spec'
|
3
|
+
# rescue LoadError
|
4
|
+
# require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
+
# require 'spec'
|
6
|
+
# end
|
7
|
+
begin
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
RSpec::Core::RakeTask.new do |t|
|
19
|
+
t.rspec_opts = ['--colour', '--format', 'd']
|
20
|
+
t.ruby_opts = ['-w']
|
21
|
+
end
|
data/todo.org
ADDED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rangehash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mark Simpson
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
rPnJW2hNpPLFrA==
|
37
37
|
-----END CERTIFICATE-----
|
38
38
|
|
39
|
-
date: 2010-12-
|
39
|
+
date: 2010-12-30 00:00:00 -05:00
|
40
40
|
default_executable:
|
41
41
|
dependencies:
|
42
42
|
- !ruby/object:Gem::Dependency
|
@@ -102,6 +102,7 @@ extra_rdoc_files:
|
|
102
102
|
- Manifest.txt
|
103
103
|
- PostInstall.txt
|
104
104
|
files:
|
105
|
+
- .rspec
|
105
106
|
- History.txt
|
106
107
|
- Manifest.txt
|
107
108
|
- PostInstall.txt
|
@@ -111,8 +112,10 @@ files:
|
|
111
112
|
- script/console
|
112
113
|
- script/destroy
|
113
114
|
- script/generate
|
114
|
-
-
|
115
|
-
-
|
115
|
+
- spec/rangehash_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- tasks/rspec.rake
|
118
|
+
- todo.org
|
116
119
|
has_rdoc: true
|
117
120
|
homepage: http://github.com/verdammelt/range-hash
|
118
121
|
licenses: []
|
@@ -149,5 +152,4 @@ signing_key:
|
|
149
152
|
specification_version: 3
|
150
153
|
summary: Simple class which uses Ranges (and Fixnums) as keys in for a hash
|
151
154
|
test_files:
|
152
|
-
-
|
153
|
-
- test/test_rangehash.rb
|
155
|
+
- spec/rangehash_spec.rb
|
metadata.gz.sig
CHANGED
Binary file
|
data/test/test_helper.rb
DELETED
data/test/test_rangehash.rb
DELETED
@@ -1,54 +0,0 @@
|
|
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
|