rangehash 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.3 2011-04-25
2
+ * 1 major enhancement:
3
+ * RangeHash is now a full fledged derived class of Hash
4
+
1
5
  === 0.0.2 2010-12-30
2
6
 
3
7
  * 1 major enhancement:
data/README.rdoc CHANGED
@@ -30,7 +30,7 @@ one of the hashes that value is returned.
30
30
 
31
31
  (The MIT License)
32
32
 
33
- Copyright (c) 2010 Mark Simpson
33
+ Copyright (c) 2010-2011 Mark Simpson
34
34
 
35
35
  Permission is hereby granted, free of charge, to any person obtaining
36
36
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -15,7 +15,8 @@ $hoe = Hoe.spec 'rangehash' do
15
15
  self.developer 'Mark Simpson', 'verdammelt@gmail.com'
16
16
  self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
17
17
  self.rubyforge_name = self.name
18
- self.test_globs = ["spec/**/*_spec.rb"] #["test/**/test_*.rb", "spec/**/*_spec.rb"]
18
+ self.test_globs = ["spec/**/*_spec.rb"]
19
+ self.extra_rdoc_files = ["README.rdoc"]
19
20
  end
20
21
 
21
22
  require 'newgem/tasks'
data/lib/rangehash.rb CHANGED
@@ -2,39 +2,52 @@ $:.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.2'
5
+ VERSION = '0.0.3'
6
6
  end
7
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
-
8
+ class RangeHash < Hash
14
9
  def [](key)
15
- (@key_value_hash.find{ |kv| kv[0] === key } || @default_item)[1]
10
+ value = find_value key
11
+ value == :notfound ? default : value
16
12
  end
17
-
18
- def []=(key, value)
19
- @key_value_hash[key] = value
13
+
14
+ def fetch(key, default = nil)
15
+ value = find_value key
16
+ return value if value != :notfound
17
+ return default if default != nil
18
+ yield key if block_given?
19
+ raise IndexError, "key " + key.to_s + " not found" if not block_given?
20
20
  end
21
-
22
- def to_s
23
- @key_value_hash.to_s
21
+
22
+ def key?(key)
23
+ found = find_pair(key)
24
+ !(found.empty?)
24
25
  end
25
-
26
+
27
+ alias_method :has_key?, :key?
28
+ alias_method :include?, :key?
29
+ alias_method :member?, :key?
30
+
31
+ def delete(key)
32
+ super
33
+ end
34
+
26
35
  def sorted_keys
27
- @key_value_hash.keys.sort do |a, b|
36
+ keys.sort do |a, b|
28
37
  sort_key(a) <=> sort_key(b)
29
38
  end
30
39
  end
31
40
 
32
41
  private
42
+ def find_pair(key)
43
+ each_pair.select {|k, v| (k == key || k === key)}
44
+ end
33
45
 
46
+ def find_value(key)
47
+ (find_pair(key).first || [nil, :notfound])[1]
48
+ end
49
+
34
50
  def sort_key(a)
35
- return a.first if Range === a
36
- a
51
+ return Range === a ? a.first : a
37
52
  end
38
53
  end
39
-
40
-
@@ -1,59 +1,141 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
- # Time to add your specs!
4
- # http://rspec.info/
5
3
  describe RangeHash do
6
- describe "retrieval by key:" do
7
- it "should allow bare fixnum keys" do
8
- rh = RangeHash.new({ 1 => :foo })
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
9
11
  rh[1].should == :foo
10
12
  end
11
13
 
12
- it "should allow ranges as keys" do
13
- rh = RangeHash.new({ 1..3 => :foo })
14
+ it "allows ranges as keys" do
15
+ rh[1..3] = :foo
14
16
  rh[2].should == :foo
15
17
  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
18
+
19
+ it "allows ranges as keys for lookup if equal" do
20
+ rh[1..3] = :foo
21
+ rh[1..3].should == :foo
20
22
  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
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]
27
28
  end
28
-
29
- it "should allow nil initial value" do
30
- rh = RangeHash.new(nil)
31
- rh.to_s.should == ""
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
32
33
  end
33
34
  end
34
35
 
35
- describe "adding keys and values: " do
36
- it "should allow adding new fixnum keys dynamically" do
36
+ describe "[]=" do
37
+ it "allows adding new fixnum keys dynamically" do
37
38
  rh = RangeHash.new(nil)
38
39
  rh[42] = :answer
39
40
  rh[42].should == :answer
40
41
  end
41
42
 
42
- it "should allow adding new range keys dynamically" do
43
- rh = RangeHash.new(nil)
43
+ it "allows adding new range keys dynamically" do
44
44
  rh[1..3] = :foo
45
45
  rh[2].should == :foo
46
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
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
52
60
  end
53
61
  end
54
62
 
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]
63
+ describe ".fetch" do
64
+ it "handles ranges" do
65
+ rh[2..4] = :foo
66
+ rh.fetch(3).should == :foo
67
+ end
68
+
69
+ it "handle ranges as lookup if equal" do
70
+ rh[2..4] = :foo
71
+ rh.fetch(2..4).should == :foo
72
+ end
73
+
74
+
75
+ it "handles ranges and fires IndexError property" do
76
+ rh[2..4] = :foo
77
+ expect { rh.fetch(42) }.to raise_error(IndexError)
78
+ end
79
+
80
+ it "handles ranges and default value" do
81
+ rh[2..4] = :foo
82
+ rh.fetch(42, :default).should == :default
83
+ end
84
+
85
+ it "handles ranges and block" do
86
+ str = "block not called"
87
+
88
+ rh[2..4] = :foo
89
+ rh.fetch(42) { |el| str = "key:" + el.to_s }
90
+ str.should == "key:42"
91
+ end
92
+ end
93
+
94
+ describe ".has_key?" do
95
+ it "handles ranges" do
96
+ rh[2..4] = :foo
97
+ rh.has_key?(3).should be_true
98
+ rh.has_key?(2..4).should be_true
99
+ end
100
+ end
101
+
102
+ describe ".include?" do
103
+ it "handles ranges" do
104
+ rh[2..4] = :foo
105
+ rh.include?(3).should be_true
106
+ rh.has_key?(2..4).should be_true
107
+ end
108
+ end
109
+
110
+ describe ".key?" do
111
+ it "handles ranges" do
112
+ rh[2..4] = :foo
113
+ rh.key?(3).should be_true
114
+ rh.has_key?(2..4).should be_true
115
+ end
116
+ end
117
+
118
+ describe ".member?" do
119
+ it "handles ranges" do
120
+ rh[2..4] = :foo
121
+ rh.key?(3).should be_true
122
+ rh.has_key?(2..4).should be_true
123
+ end
124
+ end
125
+
126
+ describe ".index" do
127
+ it "handles ranges" do
128
+ rh[2..4] = :foo
129
+ rh.index(:foo).should == (2..4)
130
+ end
131
+ end
132
+
133
+ describe ".sorted_keys" do
134
+ it "allows retrieval of sorted keys" do
135
+ rh[10..20] = :foo
136
+ rh[2] = :bar
137
+ rh[0] = :baz
138
+ rh.sorted_keys.should == [0, 2, 10..20]
139
+ end
58
140
  end
59
141
  end
data.tar.gz.sig CHANGED
Binary file
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: 27
5
- prerelease: false
4
+ hash: 25
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mark Simpson
@@ -36,57 +36,25 @@ cert_chain:
36
36
  rPnJW2hNpPLFrA==
37
37
  -----END CERTIFICATE-----
38
38
 
39
- date: 2010-12-30 00:00:00 -05:00
39
+ date: 2011-04-26 00:00:00 -04:00
40
40
  default_executable:
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
43
- name: rubyforge
43
+ name: hoe
44
44
  prerelease: false
45
45
  requirement: &id001 !ruby/object:Gem::Requirement
46
46
  none: false
47
47
  requirements:
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
- hash: 7
50
+ hash: 35
51
51
  segments:
52
52
  - 2
53
- - 0
53
+ - 9
54
54
  - 4
55
- version: 2.0.4
55
+ version: 2.9.4
56
56
  type: :development
57
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
58
  description: |-
91
59
  Simple class which uses Ranges (and Fixnums) as keys in for a hash.
92
60
  Then look ups can be done with FixNum and if the FixNum is included in
@@ -101,6 +69,7 @@ extra_rdoc_files:
101
69
  - History.txt
102
70
  - Manifest.txt
103
71
  - PostInstall.txt
72
+ - README.rdoc
104
73
  files:
105
74
  - .rspec
106
75
  - History.txt
@@ -116,6 +85,7 @@ files:
116
85
  - spec/spec_helper.rb
117
86
  - tasks/rspec.rake
118
87
  - todo.org
88
+ - .gemtest
119
89
  has_rdoc: true
120
90
  homepage: http://github.com/verdammelt/range-hash
121
91
  licenses: []
@@ -147,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
117
  requirements: []
148
118
 
149
119
  rubyforge_project: rangehash
150
- rubygems_version: 1.3.7
120
+ rubygems_version: 1.5.0
151
121
  signing_key:
152
122
  specification_version: 3
153
123
  summary: Simple class which uses Ranges (and Fixnums) as keys in for a hash
metadata.gz.sig CHANGED
Binary file