RangeHash 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *~
2
+ pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rangehash.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ RangeHash (0.0.6)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rcov (0.9.10)
11
+ rspec (2.6.0)
12
+ rspec-core (~> 2.6.0)
13
+ rspec-expectations (~> 2.6.0)
14
+ rspec-mocks (~> 2.6.0)
15
+ rspec-core (2.6.4)
16
+ rspec-expectations (2.6.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.6.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ RangeHash!
25
+ rcov
26
+ rspec
data/History.txt ADDED
@@ -0,0 +1,25 @@
1
+ === 0.0.6 2011-08-14
2
+ * 1 Major change:
3
+ * switched from using hoe to bundler for gem creation
4
+
5
+ === 0.0.5 2011-08-11
6
+ * 1 cosmetic change:
7
+ * removing some uneeded rake cruft
8
+
9
+ === 0.0.4 2011-04-28
10
+ * 1 Major enhancement:
11
+ * Upated to Ruby 1.9
12
+
13
+ === 0.0.3 2011-04-25
14
+ * 1 major enhancement:
15
+ * RangeHash is now a full fledged derived class of Hash
16
+
17
+ === 0.0.2 2010-12-30
18
+
19
+ * 1 major enhancement:
20
+ * switch from test/unit to rspec
21
+
22
+ === 0.0.1 2010-12-29
23
+
24
+ * 1 major enhancement:
25
+ * Initial release
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = rangehash
2
+
3
+ * http://github.com/verdammelt/range-hash
4
+ * https://rubygems.org/gems/rangehash
5
+
6
+ == DESCRIPTION:
7
+
8
+ Simple class which uses Ranges (and Fixnums) as keys in for a hash.
9
+ Then look ups can be done with FixNum and if the FixNum is included in
10
+ one of the hashes that value is returned.
11
+
12
+ == FEATURES/PROBLEMS:
13
+
14
+ * RangeHash keys are Ranges or Fixnums
15
+
16
+ == SYNOPSIS:
17
+
18
+ hash = RangeHash.new
19
+ hash[10..20] = 'foo'
20
+ hash[21..22] = 'bar'
21
+ val = hash[15] # val == 'foo'
22
+
23
+ == REQUIREMENTS:
24
+
25
+ == INSTALL:
26
+
27
+ sudo gem install range-hash
28
+
29
+ == LICENSE:
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2010-2011 Mark Simpson
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining
36
+ a copy of this software and associated documentation files (the
37
+ 'Software'), to deal in the Software without restriction, including
38
+ without limitation the rights to use, copy, modify, merge, publish,
39
+ distribute, sublicense, and/or sell copies of the Software, and to
40
+ permit persons to whom the Software is furnished to do so, subject to
41
+ the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
49
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
50
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
51
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
52
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+ require "bundler/gem_tasks"
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = ['--format', 'progress' ]
7
+ end
8
+
9
+ desc "Run all specs with RCov"
10
+ RSpec::Core::RakeTask.new(:rcov) do |t|
11
+ t.rcov = true
12
+ t.rcov_opts = ['--text-report', '--save', 'coverage.info', '--exclude', 'spec_helper', '--exclude', '^/']
13
+ end
14
+
15
+ task :default => [:spec]
16
+
data/lib/rangehash.rb ADDED
@@ -0,0 +1,54 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ class RangeHash < Hash
5
+ def [](key)
6
+ value = find_value key
7
+ value == :notfound ? default : value
8
+ end
9
+
10
+ def fetch(key, default = nil)
11
+ value = find_value key
12
+ return value if value != :notfound
13
+ return default if default != nil
14
+ yield key if block_given?
15
+ raise IndexError, "key " + key.to_s + " not found" if not block_given?
16
+ end
17
+
18
+ def assoc(key)
19
+ found = find_pair(key)
20
+ found.empty? ? nil : found.first
21
+ end
22
+
23
+ def key?(key)
24
+ found = find_pair(key)
25
+ !(found.empty?)
26
+ end
27
+
28
+ alias_method :has_key?, :key?
29
+ alias_method :include?, :key?
30
+ alias_method :member?, :key?
31
+
32
+ def delete(key)
33
+ super
34
+ end
35
+
36
+ def sorted_keys
37
+ keys.sort do |a, b|
38
+ sort_key(a) <=> sort_key(b)
39
+ end
40
+ end
41
+
42
+ private
43
+ def find_pair(key)
44
+ each.select {|k, v| (k == key || k === key)}
45
+ end
46
+
47
+ def find_value(key)
48
+ (find_pair(key).first || [nil, :notfound])[1]
49
+ end
50
+
51
+ def sort_key(a)
52
+ return Range === a ? a.first : a
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ module Rangehash
2
+ VERSION = "0.0.6"
3
+ end
4
+
data/rangehash.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rangehash/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "RangeHash"
7
+ s.version = Rangehash::VERSION
8
+ s.authors = ["Mark Simpson"]
9
+ s.email = ["verdammelt@gmail.com"]
10
+ s.license = 'MIT'
11
+ s.homepage = "https://github.com/verdammelt/rangehash"
12
+ s.summary = %q{Subclass of Hash which allows Ranges as the keys}
13
+ s.description = <<-EOF
14
+ RangeHash subclasses Hash and adds functionality so that Ranges can
15
+ be the keys and values can be found by FixNums within those ranges.
16
+ EOF
17
+
18
+ s.rubyforge_project = "rangehash"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+
25
+ # specify any dependencies here; for example:
26
+ s.add_development_dependency "rspec"
27
+ s.add_development_dependency "rcov"
28
+ end
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,146 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe RangeHash do
4
+ let(:rh) { RangeHash.new }
5
+ let(:rh_with_default) { RangeHash.new(:default_value) }
6
+ let(:rh_with_other)
7
+
8
+ describe "[]" do
9
+ it "allows bare fixnum keys" do
10
+ rh[1] = :foo
11
+ rh[1].should == :foo
12
+ end
13
+
14
+ it "allows ranges as keys" do
15
+ rh[1..3] = :foo
16
+ rh[2].should == :foo
17
+ end
18
+
19
+ it "allows ranges as keys for lookup if equal" do
20
+ rh[1..3] = :foo
21
+ rh[1..3].should == :foo
22
+ end
23
+
24
+ it "allows mix of ranges and fixnums" do
25
+ rh[1] = :foo
26
+ rh[2..4] = :bar
27
+ [rh[1], rh[3]].should == [:foo, :bar]
28
+ end
29
+
30
+ it "returns default value if key not found" do
31
+ rh_with_default[1..3] = :foo
32
+ rh_with_default[42].should == :default_value
33
+ end
34
+ end
35
+
36
+ describe "[]=" do
37
+ it "allows adding new fixnum keys dynamically" do
38
+ rh = RangeHash.new(nil)
39
+ rh[42] = :answer
40
+ rh[42].should == :answer
41
+ end
42
+
43
+ it "allows adding new range keys dynamically" do
44
+ rh[1..3] = :foo
45
+ rh[2].should == :foo
46
+ end
47
+ end
48
+
49
+ describe ".delete" do
50
+ it "handles ranges as keys" do
51
+ rh[2..4] = :foo
52
+ rh.delete(2..4)
53
+ rh.has_key?(2..4).should be_false
54
+ end
55
+
56
+ it "does not handle ranges specially" do
57
+ rh[2..4] = :foo
58
+ rh.delete(3)
59
+ rh.key?(2..4).should be_true
60
+ end
61
+ end
62
+
63
+ describe ".assoc" do
64
+ it "handle reanges specially" do
65
+ rh[2..4] = :foo
66
+ rh.assoc(3).should == [(2..4), :foo]
67
+ end
68
+
69
+ it "returns nil if key not found" do
70
+ rh[2..4] = :foo
71
+ rh.assoc(42).should be_nil
72
+ end
73
+ end
74
+
75
+ describe ".fetch" do
76
+ it "handles ranges" do
77
+ rh[2..4] = :foo
78
+ rh.fetch(3).should == :foo
79
+ end
80
+
81
+ it "handle ranges as lookup if equal" do
82
+ rh[2..4] = :foo
83
+ rh.fetch(2..4).should == :foo
84
+ end
85
+
86
+
87
+ it "handles ranges and fires IndexError property" do
88
+ rh[2..4] = :foo
89
+ expect { rh.fetch(42) }.to raise_error(IndexError)
90
+ end
91
+
92
+ it "handles ranges and default value" do
93
+ rh[2..4] = :foo
94
+ rh.fetch(42, :default).should == :default
95
+ end
96
+
97
+ it "handles ranges and block" do
98
+ str = "block not called"
99
+
100
+ rh[2..4] = :foo
101
+ rh.fetch(42) { |el| str = "key:" + el.to_s }
102
+ str.should == "key:42"
103
+ end
104
+ end
105
+
106
+ describe ".has_key?" do
107
+ it "handles ranges" do
108
+ rh[2..4] = :foo
109
+ rh.has_key?(3).should be_true
110
+ rh.has_key?(2..4).should be_true
111
+ end
112
+ end
113
+
114
+ describe ".include?" do
115
+ it "handles ranges" do
116
+ rh[2..4] = :foo
117
+ rh.include?(3).should be_true
118
+ rh.has_key?(2..4).should be_true
119
+ end
120
+ end
121
+
122
+ describe ".key?" do
123
+ it "handles ranges" do
124
+ rh[2..4] = :foo
125
+ rh.key?(3).should be_true
126
+ rh.has_key?(2..4).should be_true
127
+ end
128
+ end
129
+
130
+ describe ".member?" do
131
+ it "handles ranges" do
132
+ rh[2..4] = :foo
133
+ rh.key?(3).should be_true
134
+ rh.has_key?(2..4).should be_true
135
+ end
136
+ end
137
+
138
+ describe ".sorted_keys" do
139
+ it "allows retrieval of sorted keys" do
140
+ rh[10..20] = :foo
141
+ rh[2] = :bar
142
+ rh[0] = :baz
143
+ rh.sorted_keys.should == [0, 2, 10..20]
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,11 @@
1
+ begin
2
+ require 'rspec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'rspec'
7
+ end
8
+ require 'rspec'
9
+
10
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
11
+ require 'rangehash'
data/todo.org ADDED
@@ -0,0 +1,3 @@
1
+ * Removing keys? How to handle removing ranges? split them?
2
+ * Handle overlapping ranges?
3
+ * migrate to use bundler
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: RangeHash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
11
+ platform: ruby
12
+ authors:
13
+ - Mark Simpson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-14 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rcov
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: " RangeHash subclasses Hash and adds functionality so that Ranges can \n be the keys and values can be found by FixNums within those ranges.\n"
50
+ email:
51
+ - verdammelt@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - .rspec
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - History.txt
64
+ - README.rdoc
65
+ - Rakefile
66
+ - lib/rangehash.rb
67
+ - lib/rangehash/version.rb
68
+ - rangehash.gemspec
69
+ - script/console
70
+ - script/destroy
71
+ - script/generate
72
+ - spec/rangehash_spec.rb
73
+ - spec/spec_helper.rb
74
+ - todo.org
75
+ has_rdoc: true
76
+ homepage: https://github.com/verdammelt/rangehash
77
+ licenses:
78
+ - MIT
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project: rangehash
105
+ rubygems_version: 1.5.0
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Subclass of Hash which allows Ranges as the keys
109
+ test_files:
110
+ - spec/rangehash_spec.rb
111
+ - spec/spec_helper.rb