insensitive_hash 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.markdown +20 -0
- data/README.markdown +32 -27
- data/lib/insensitive_hash.rb +4 -4
- data/lib/insensitive_hash/insensitive_hash.rb +21 -51
- data/lib/insensitive_hash/version.rb +1 -1
- data/test/test_insensitive_hash.rb +61 -159
- metadata +2 -2
data/CHANGELOG.markdown
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
### 0.3.0
|
2
|
+
* "Inherited insensitivity" had been a great source of confusion,
|
3
|
+
as it stores transformed version of the given Array of Hash.
|
4
|
+
From 0.3.0, Hash values and descendant Hashes are converted to be insensitive
|
5
|
+
only on the initialization of InsensitiveHash or on `underscore=` call.
|
6
|
+
Refer to the following code
|
7
|
+
* Now allowing underscores for spaces is by default on, and cannot be turned off.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
ih = {}.insensitive
|
11
|
+
ih[:a] = { :b => :c }
|
12
|
+
|
13
|
+
ih[:a]['B'] # nil
|
14
|
+
|
15
|
+
# Initialize another InsensitiveHash
|
16
|
+
ih2 = ih.insensitive
|
17
|
+
ih2[:a]['B'] # :c
|
18
|
+
```
|
19
|
+
* `:underscore` option is on by default
|
20
|
+
|
1
21
|
### 0.2.4
|
2
22
|
* Bug fix: Invalid `dup`, `clone` behavior
|
3
23
|
* :safe setting from `Hash#insensitive`
|
data/README.markdown
CHANGED
@@ -17,9 +17,11 @@ Examples
|
|
17
17
|
require 'insensitive_hash'
|
18
18
|
|
19
19
|
ih = {}.insensitive
|
20
|
-
|
21
|
-
ih = { 'hello world' => true }.insensitive
|
22
|
-
|
20
|
+
|
21
|
+
ih = { :abc => 1, 'hello world' => true }.insensitive
|
22
|
+
|
23
|
+
ih['ABC'] # 1
|
24
|
+
ih[:Hello_World] # true
|
23
25
|
```
|
24
26
|
|
25
27
|
### Instantiation without monkey-patching Hash
|
@@ -31,13 +33,13 @@ require 'insensitive_hash/minimal'
|
|
31
33
|
|
32
34
|
ih = InsensitiveHash.new
|
33
35
|
ih = InsensitiveHash.new(:default_value)
|
34
|
-
ih = InsensitiveHash.new { |
|
36
|
+
ih = InsensitiveHash.new { |ih, k| ih[k] = InsensitiveHash.new }
|
35
37
|
|
36
38
|
ih = InsensitiveHash[ 'abc' => 1, :def => 2 ]
|
37
39
|
ih = InsensitiveHash[ 'abc', 1, :def, 2 ]
|
38
40
|
ih = InsensitiveHash[ [['abc', 1], [:def, 2]] ]
|
39
41
|
|
40
|
-
ih = InsensitiveHash[ 'hello world' => true ]
|
42
|
+
ih = InsensitiveHash[ 'hello world' => true ]
|
41
43
|
```
|
42
44
|
|
43
45
|
### Revert to normal Hash
|
@@ -49,7 +51,7 @@ h = ih.to_hash
|
|
49
51
|
|
50
52
|
### Basic usage
|
51
53
|
```ruby
|
52
|
-
ih =
|
54
|
+
ih = {:abc => 1, 'DEF' => 2}.insensitive
|
53
55
|
|
54
56
|
# Case-insensitive, Symbol/String-indifferent access.
|
55
57
|
ih['Abc'] # 1
|
@@ -70,17 +72,33 @@ ih.keys # ['DEF']
|
|
70
72
|
```
|
71
73
|
|
72
74
|
### "Inherited insensitivity"
|
75
|
+
|
76
|
+
When InsensitiveHash is built from another Hash,
|
77
|
+
descendant Hash values are recursively converted to be insensitive
|
78
|
+
(Useful when processing YAML inputs)
|
79
|
+
|
73
80
|
```ruby
|
74
|
-
|
75
|
-
|
76
|
-
ih = InsensitiveHash.new
|
77
|
-
ih['kids'] = { :hello => [ { :world => '!!!' } ] }
|
81
|
+
|
82
|
+
ih = { 'kids' => { :hello => [ { :world => '!!!' } ] } }.insensitive
|
78
83
|
ih[:kids]['Hello'].first['WORLD'] # !!!
|
79
84
|
|
80
|
-
ih
|
85
|
+
ih = {:one => [ [ [ { :a => { :b => { :c => 'd' } } } ] ] ]}.insensitive
|
81
86
|
ih['one'].first.first.first['A']['b'][:C] # 'd'
|
82
87
|
```
|
83
88
|
|
89
|
+
Once InsensitiveHash is initialized, you can convert its descendant Hash values by
|
90
|
+
building a new InsensitiveHash from it.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
ih = {}.insensitive
|
94
|
+
ih[:abc] = { :def => true }
|
95
|
+
|
96
|
+
ih['ABC']['DEF'] # nil
|
97
|
+
|
98
|
+
ih2 = ih.insensitive
|
99
|
+
ih2['ABC']['DEF'] # true
|
100
|
+
```
|
101
|
+
|
84
102
|
### Processing case-insensitive YAML input
|
85
103
|
```ruby
|
86
104
|
db = YAML.load(File.read 'database.yml').insensitive
|
@@ -90,23 +108,10 @@ db['Development']['ADAPTER']
|
|
90
108
|
db[:production][:adapter]
|
91
109
|
```
|
92
110
|
|
93
|
-
###
|
111
|
+
### Enabling key-clash detection (Safe mode)
|
94
112
|
```ruby
|
95
|
-
|
96
|
-
|
97
|
-
ih = h.insensitive(:underscore => true)
|
98
|
-
ih[:a_key_with_spaces] # true
|
99
|
-
|
100
|
-
# Or without Hash#insensitive,
|
101
|
-
ih = InsensitiveHash[ h ].tap { |ih| ih.underscore = true }
|
102
|
-
ih.underscore? # true
|
103
|
-
ih[:a_key_with_spaces] # true
|
104
|
-
```
|
105
|
-
|
106
|
-
### Enabling key-clash detection
|
107
|
-
```ruby
|
108
|
-
ih = InsensitiveHash.new.tap { |ih| ih.safe = true }
|
109
|
-
ih.safe? # true
|
113
|
+
ih = InsensitiveHash.new
|
114
|
+
ih.safe = true
|
110
115
|
|
111
116
|
# Will raise InsensitiveHash::KeyClashError
|
112
117
|
h.merge!('hello world' => 1, :hello_world => 2)
|
data/lib/insensitive_hash.rb
CHANGED
@@ -4,10 +4,10 @@ require 'insensitive_hash/insensitive_hash'
|
|
4
4
|
class Hash
|
5
5
|
# @return [InsensitiveHash]
|
6
6
|
def insensitive options = {}
|
7
|
-
InsensitiveHash.
|
8
|
-
ih.
|
9
|
-
ih.
|
10
|
-
ih.
|
7
|
+
InsensitiveHash[self].tap do |ih|
|
8
|
+
ih.safe = options[:safe] if options.has_key?(:safe)
|
9
|
+
ih.default = self.default
|
10
|
+
ih.default_proc = self.default_proc if self.default_proc
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -11,33 +11,9 @@ class InsensitiveHash < Hash
|
|
11
11
|
end
|
12
12
|
|
13
13
|
@key_map = {}
|
14
|
-
@underscore = false
|
15
14
|
@safe = false
|
16
15
|
end
|
17
16
|
|
18
|
-
# Sets whether to replace spaces in String keys to underscores
|
19
|
-
# @param [Boolean] us
|
20
|
-
# @return [Boolean]
|
21
|
-
def underscore= us
|
22
|
-
raise ArgumentError.new("Not true or false") unless [true, false].include?(us)
|
23
|
-
|
24
|
-
# Check key clash
|
25
|
-
detect_clash self, us
|
26
|
-
|
27
|
-
@underscore = us
|
28
|
-
@key_map = {}
|
29
|
-
self.keys.each do |k|
|
30
|
-
self[k] = self.delete(k)
|
31
|
-
end
|
32
|
-
|
33
|
-
us
|
34
|
-
end
|
35
|
-
|
36
|
-
# @return [Boolean] Whether to replace spaces in String keys to underscores
|
37
|
-
def underscore?
|
38
|
-
@underscore
|
39
|
-
end
|
40
|
-
|
41
17
|
# Sets whether to detect key clashes
|
42
18
|
# @param [Boolean]
|
43
19
|
# @return [Boolean]
|
@@ -61,9 +37,7 @@ class InsensitiveHash < Hash
|
|
61
37
|
def self.[] *init
|
62
38
|
h = Hash[*init]
|
63
39
|
InsensitiveHash.new.tap do |ih|
|
64
|
-
|
65
|
-
ih[key] = value
|
66
|
-
end
|
40
|
+
ih.merge! h
|
67
41
|
end
|
68
42
|
end
|
69
43
|
|
@@ -77,16 +51,16 @@ class InsensitiveHash < Hash
|
|
77
51
|
|
78
52
|
def []= key, value
|
79
53
|
delete key
|
80
|
-
ekey = encode key
|
54
|
+
ekey = encode key
|
81
55
|
@key_map[ekey] = key
|
82
|
-
super key,
|
56
|
+
super key, value
|
83
57
|
end
|
84
58
|
alias store []=
|
85
59
|
|
86
60
|
def merge! other_hash
|
87
|
-
detect_clash other_hash
|
61
|
+
detect_clash other_hash
|
88
62
|
other_hash.each do |key, value|
|
89
|
-
|
63
|
+
deep_set key, value
|
90
64
|
end
|
91
65
|
self
|
92
66
|
end
|
@@ -112,15 +86,11 @@ class InsensitiveHash < Hash
|
|
112
86
|
def replace other
|
113
87
|
super other
|
114
88
|
|
115
|
-
|
116
|
-
# What is the correct behavior of replace when the other hash is not an InsensitiveHash?
|
117
|
-
# underscore property precedence: other => self (FIXME)
|
118
|
-
self.underscore = other.respond_to?(:underscore?) ? other.underscore? : self.underscore?
|
119
|
-
self.safe = other.safe? if other.respond_to?(:safe?)
|
89
|
+
self.safe = other.respond_to?(:safe?) ? other.safe? : safe?
|
120
90
|
|
121
91
|
@key_map.clear
|
122
92
|
self.each do |k, v|
|
123
|
-
ekey = encode k
|
93
|
+
ekey = encode k
|
124
94
|
@key_map[ekey] = k
|
125
95
|
end
|
126
96
|
end
|
@@ -153,21 +123,26 @@ class InsensitiveHash < Hash
|
|
153
123
|
end
|
154
124
|
|
155
125
|
private
|
156
|
-
def
|
126
|
+
def deep_set key, value
|
127
|
+
wv = wrap value
|
128
|
+
self[key] = wv
|
129
|
+
end
|
130
|
+
|
131
|
+
def wrap value
|
157
132
|
case value
|
158
133
|
when InsensitiveHash
|
159
|
-
value
|
134
|
+
value
|
160
135
|
when Hash
|
161
|
-
InsensitiveHash[value]
|
136
|
+
InsensitiveHash[value]
|
162
137
|
when Array
|
163
|
-
value.map { |v| wrap v
|
138
|
+
value.map { |v| wrap v }
|
164
139
|
else
|
165
140
|
value
|
166
141
|
end
|
167
142
|
end
|
168
143
|
|
169
144
|
def lookup_key key, delete = false
|
170
|
-
ekey = encode key
|
145
|
+
ekey = encode key
|
171
146
|
if @key_map.has_key?(ekey)
|
172
147
|
delete ? @key_map.delete(ekey) : @key_map[ekey]
|
173
148
|
else
|
@@ -175,22 +150,17 @@ private
|
|
175
150
|
end
|
176
151
|
end
|
177
152
|
|
178
|
-
def encode key
|
153
|
+
def encode key
|
179
154
|
case key
|
180
155
|
when String, Symbol
|
181
|
-
key
|
182
|
-
if us
|
183
|
-
key.gsub(' ', '_')
|
184
|
-
else
|
185
|
-
key
|
186
|
-
end
|
156
|
+
key.to_s.downcase.gsub(' ', '_')
|
187
157
|
else
|
188
158
|
key
|
189
159
|
end
|
190
160
|
end
|
191
161
|
|
192
|
-
def detect_clash hash
|
193
|
-
hash.keys.map { |k| encode k
|
162
|
+
def detect_clash hash
|
163
|
+
hash.keys.map { |k| encode k }.tap { |ekeys|
|
194
164
|
raise KeyClashError.new("Key clash detected") if ekeys != ekeys.uniq
|
195
165
|
} if @safe
|
196
166
|
end
|
@@ -3,7 +3,7 @@ require 'bundler'
|
|
3
3
|
Bundler.setup(:default, :development)
|
4
4
|
require 'test/unit'
|
5
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
-
require 'insensitive_hash
|
6
|
+
require 'insensitive_hash'
|
7
7
|
|
8
8
|
class TestInsensitiveHash < Test::Unit::TestCase
|
9
9
|
def eight?
|
@@ -44,18 +44,27 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
44
44
|
assert ih.keys.include?(:b)
|
45
45
|
assert_equal 2, ih.keys.length
|
46
46
|
assert_equal({ :c => 5 }, ih['B'])
|
47
|
-
assert_equal 5, ih['B'][
|
47
|
+
assert_equal 5, ih['B'][:c]
|
48
|
+
assert_equal nil, ih['B']['C']
|
48
49
|
|
49
50
|
ih['c'] = [ { 'x' => 1 }, { :y => 2 } ]
|
50
51
|
assert ih.keys.include?('c')
|
51
52
|
assert_equal 3, ih.keys.length
|
53
|
+
|
54
|
+
assert_equal nil, ih[:c].first[:x]
|
55
|
+
assert_equal nil, ih[:c].last['Y']
|
56
|
+
|
57
|
+
ih = ih.insensitive
|
52
58
|
assert_equal 1, ih[:c].first[:x]
|
53
59
|
assert_equal 2, ih[:c].last['Y']
|
54
60
|
|
55
61
|
# Deeeeeper nesting
|
56
62
|
ih['c'] = [ [ [ { 'x' => 1 }, { :y => 2 } ] ] ]
|
63
|
+
assert_equal nil, ih[:c].first.first.last[:Y]
|
64
|
+
ih = ih.insensitive
|
57
65
|
assert_equal 2, ih[:c].first.first.last[:Y]
|
58
|
-
|
66
|
+
|
67
|
+
ih['c'] = { 'a' => { 'a' => { 'a' => 100 } } }.insensitive
|
59
68
|
assert_equal 100, ih[:C][:a][:A]['A']
|
60
69
|
|
61
70
|
ih[5] = 50
|
@@ -69,11 +78,12 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
69
78
|
:c => { :D => [ { 'e' => 3 } ] }
|
70
79
|
}
|
71
80
|
|
72
|
-
|
73
|
-
|
74
|
-
|
81
|
+
pend("TODO") do
|
82
|
+
assert_raise(NoMethodError) {
|
83
|
+
hash.insensitive
|
84
|
+
}
|
85
|
+
end
|
75
86
|
|
76
|
-
require 'insensitive_hash'
|
77
87
|
[hash.insensitive, InsensitiveHash[hash]].each do |ih|
|
78
88
|
assert ih.is_a?(Hash)
|
79
89
|
assert_equal InsensitiveHash, ih.class
|
@@ -86,15 +96,20 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
86
96
|
end
|
87
97
|
end
|
88
98
|
|
99
|
+
# Changelog 0.3.0
|
100
|
+
ih = {}.insensitive
|
101
|
+
ih[:a] = { :b => :c }
|
102
|
+
assert_nil ih[:a]['B']
|
103
|
+
ih2 = ih.insensitive
|
104
|
+
assert_equal :c, ih2[:a]['B']
|
105
|
+
|
89
106
|
# InsensitiveHash#insensitive
|
90
107
|
ihash1 = hash.insensitive
|
91
108
|
ihash1.default = :default
|
92
|
-
ihash1.underscore = true
|
93
109
|
ihash2 = ihash1.insensitive.insensitive.insensitive
|
94
110
|
|
95
111
|
assert_equal InsensitiveHash, ihash2.class
|
96
112
|
assert_equal :default, ihash2.default
|
97
|
-
assert_equal true, ihash2.underscore?
|
98
113
|
|
99
114
|
# Preserving default
|
100
115
|
[:default, nil].each do |d|
|
@@ -116,31 +131,23 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
116
131
|
|
117
132
|
# FIXME: test insensitive call with encoder
|
118
133
|
h = InsensitiveHash[{'Key with spaces' => 1}]
|
119
|
-
h2 = h.insensitive
|
120
|
-
h3 = h.insensitive :underscore => false
|
121
|
-
assert_raise(ArgumentError) { h.insensitive :underscore => :wat }
|
134
|
+
h2 = h.insensitive
|
122
135
|
|
123
136
|
assert_equal 1, h['key with spaces']
|
124
|
-
|
125
|
-
assert_equal 1, h3['key with spaces']
|
126
|
-
assert_nil h3[:key_with_spaces]
|
137
|
+
assert_equal 1, h[:key_with_spaces]
|
127
138
|
assert_equal 1, h2[:KEY_WITH_SPACES]
|
128
139
|
assert_equal 1, h2[:Key_with_spaces]
|
129
140
|
|
130
141
|
assert_equal ['Key with spaces'], h.keys
|
131
142
|
assert_equal ['Key with spaces'], h2.keys
|
132
|
-
assert_equal ['Key with spaces'], h3.keys
|
133
143
|
|
134
144
|
h = { 'A key with spaces' => true }
|
135
|
-
ih = h.insensitive
|
145
|
+
ih = h.insensitive
|
136
146
|
assert ih[:a_key_with_spaces]
|
137
|
-
assert ih.underscore?
|
138
147
|
|
139
148
|
# FIXME: from README
|
140
149
|
ih = InsensitiveHash[ h ]
|
141
|
-
ih.underscore = true
|
142
150
|
assert ih[:a_key_with_spaces]
|
143
|
-
assert ih.underscore?
|
144
151
|
|
145
152
|
# :safe
|
146
153
|
ih = {}.insensitive
|
@@ -176,17 +183,9 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
176
183
|
assert_keys [:a, 'hello world', :b], ih2.keys
|
177
184
|
assert ih2.has_key?('B'), 'correctly merged another hash'
|
178
185
|
|
179
|
-
assert_nil ih[:hello_world]
|
180
|
-
assert_nil ih2[:hello_world]
|
181
|
-
|
182
|
-
ih.underscore = true
|
183
186
|
assert_equal 2, ih[:hello_world]
|
184
187
|
assert_equal 2, ih.send(method, :b => 2)[:hello_world]
|
185
188
|
|
186
|
-
ih.underscore = false
|
187
|
-
assert_nil ih[:hello_world]
|
188
|
-
assert_nil ih.send(method, :b => 2)[:hello_world]
|
189
|
-
|
190
189
|
ih.default = 10
|
191
190
|
assert_equal 10, ih.send(method, :b => 2)[:anything]
|
192
191
|
|
@@ -232,7 +231,6 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
232
231
|
assert_equal 1, ih.length
|
233
232
|
|
234
233
|
ih = InsensitiveHash.new
|
235
|
-
ih.underscore = true
|
236
234
|
ih.merge!(:hello_world => 1, 'hello world' =>2)
|
237
235
|
unless eight?
|
238
236
|
assert_equal 2, ih.values.first
|
@@ -244,13 +242,9 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
244
242
|
def test_merge_clash
|
245
243
|
ih = InsensitiveHash.new
|
246
244
|
ih2 = InsensitiveHash.new
|
247
|
-
ih2.underscore = true
|
248
245
|
|
249
246
|
sih = InsensitiveHash.new
|
250
247
|
sih.safe = true
|
251
|
-
sih2 = InsensitiveHash.new
|
252
|
-
sih2.safe = true
|
253
|
-
sih2.underscore = true
|
254
248
|
|
255
249
|
[:merge, :merge!, :update, :update!].each do |method|
|
256
250
|
# No problem
|
@@ -259,18 +253,15 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
259
253
|
h.send(method, { 'a' => 1, 'A' => 1 })
|
260
254
|
end
|
261
255
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
}
|
269
|
-
end
|
256
|
+
assert_raise(InsensitiveHash::KeyClashError) {
|
257
|
+
sih.send(method, { :a => 1, :A => 1, 'A' => 1})
|
258
|
+
}
|
259
|
+
assert_raise(InsensitiveHash::KeyClashError) {
|
260
|
+
sih.send(method, { 'a' => 1, 'A' => 1 })
|
261
|
+
}
|
270
262
|
|
271
|
-
sih.send(method, { :hello_world => 1, 'hello world' => 2})
|
272
263
|
assert_raise(InsensitiveHash::KeyClashError) {
|
273
|
-
|
264
|
+
sih.send(method, { :hello_world => 1, 'hello world' => 2})
|
274
265
|
}
|
275
266
|
ih2.send(method, { :hello_world => 1, 'hello world' => 2})
|
276
267
|
end
|
@@ -370,6 +361,30 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
370
361
|
assert_equal :ok, h.default(:right)
|
371
362
|
end
|
372
363
|
|
364
|
+
def test_default_proc_patch
|
365
|
+
h = InsensitiveHash.new { |h, k| k }
|
366
|
+
assert_equal 1, h[1]
|
367
|
+
assert_equal 2, h[2]
|
368
|
+
|
369
|
+
h = InsensitiveHash.new { |h, k| h[k] = [] }
|
370
|
+
h[:abc] << 1
|
371
|
+
h[:abc] << 2
|
372
|
+
h[:abc] << 3
|
373
|
+
assert_equal [1, 2, 3], h[:abc]
|
374
|
+
|
375
|
+
h = InsensitiveHash.new { |h, k| h[k] = {} }
|
376
|
+
h[:abc][:def] = 1
|
377
|
+
h[:abc][:ghi] = { :xyz => true }
|
378
|
+
assert_equal 1, h[:abc][:def]
|
379
|
+
assert_equal true, h[:abc][:ghi][:xyz]
|
380
|
+
assert_equal nil, h[:abc][:ghi][:XYZ] # This shouldn't work anymore from 0.3.0
|
381
|
+
|
382
|
+
h = InsensitiveHash.new
|
383
|
+
h[:abc] = arr = [1, 2, 3]
|
384
|
+
arr << 4
|
385
|
+
assert_equal [1, 2, 3, 4], h[:ABC]
|
386
|
+
end
|
387
|
+
|
373
388
|
def test_delete_if
|
374
389
|
# FIXME: Test passes, but key_map not updated
|
375
390
|
h = InsensitiveHash[ :a => 100, :tmp_a => 200, :c => 300 ]
|
@@ -427,26 +442,6 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
427
442
|
h.replace(Hash.new { |h, k| h[k] = :default })
|
428
443
|
assert_equal :default, h[:anything]
|
429
444
|
assert_equal [:anything], h.keys
|
430
|
-
|
431
|
-
# underscore property of self
|
432
|
-
assert !h.underscore?
|
433
|
-
h.replace(InsensitiveHash['hello world' => 1].tap { |ih| ih.underscore = true })
|
434
|
-
assert h.underscore?
|
435
|
-
assert_equal 1, h[:hello_world]
|
436
|
-
|
437
|
-
# TODO: FIXME
|
438
|
-
# underscore property of other
|
439
|
-
h = InsensitiveHash.new
|
440
|
-
oh = InsensitiveHash[ 'hello world' => 1 ]
|
441
|
-
oh.underscore = true
|
442
|
-
assert !h.underscore?
|
443
|
-
h.replace(oh)
|
444
|
-
assert h.underscore?
|
445
|
-
|
446
|
-
oh.underscore = false
|
447
|
-
oh[:hello_world => 2]
|
448
|
-
h.replace(oh)
|
449
|
-
assert !h.underscore?
|
450
445
|
end
|
451
446
|
|
452
447
|
def test_rassoc
|
@@ -480,121 +475,28 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
480
475
|
assert_raise(eight? ? IndexError : KeyError) { h.fetch('D') }
|
481
476
|
end
|
482
477
|
|
483
|
-
def test_underscore
|
484
|
-
h = InsensitiveHash[{'Key with spaces' => 1}]
|
485
|
-
|
486
|
-
assert_equal 1, h['key with spaces']
|
487
|
-
assert_nil h[:key_with_spaces]
|
488
|
-
|
489
|
-
test_keys = [
|
490
|
-
:KEY_WITH_SPACES,
|
491
|
-
:Key_with_spaces,
|
492
|
-
:key_with_spaces,
|
493
|
-
'key with_spaces',
|
494
|
-
'KEY_WITH spaces'
|
495
|
-
]
|
496
|
-
|
497
|
-
10.times do
|
498
|
-
assert_raise(ArgumentError) { h.underscore = 'wat' }
|
499
|
-
assert !h.underscore?
|
500
|
-
h.underscore = true
|
501
|
-
assert h.underscore?
|
502
|
-
|
503
|
-
|
504
|
-
test_keys.each do |k|
|
505
|
-
assert_equal 1, h[k]
|
506
|
-
end
|
507
|
-
assert_equal ['Key with spaces'], h.keys
|
508
|
-
|
509
|
-
h.underscore = false
|
510
|
-
assert !h.underscore?
|
511
|
-
assert_equal 1, h['KEY WITH SPACES']
|
512
|
-
test_keys.each do |k|
|
513
|
-
assert_nil h[k]
|
514
|
-
end
|
515
|
-
assert_equal ['Key with spaces'], h.keys
|
516
|
-
end
|
517
|
-
|
518
|
-
h.underscore = true
|
519
|
-
test_keys.each do |tk|
|
520
|
-
h[tk] = 1
|
521
|
-
end
|
522
|
-
|
523
|
-
assert_equal [test_keys.last], h.keys
|
524
|
-
end
|
525
|
-
|
526
478
|
def test_underscore_inheritance
|
527
|
-
h =
|
479
|
+
h =
|
528
480
|
{
|
529
481
|
'Key with spaces' => {
|
530
482
|
'Another key with spaces' => 1
|
531
483
|
},
|
532
484
|
'Key 2 with spaces' =>
|
533
|
-
InsensitiveHash['Yet another key with spaces' => 2]
|
485
|
+
InsensitiveHash['Yet another key with spaces' => 2],
|
534
486
|
'Key 3 with spaces' =>
|
535
487
|
InsensitiveHash['Yet another key with spaces' => 3]
|
536
|
-
}
|
537
|
-
]
|
488
|
+
}.insensitive
|
538
489
|
|
539
490
|
assert_equal 1, h['key with spaces']['another key with spaces']
|
540
|
-
assert_equal false, h['key with spaces'].underscore?
|
541
|
-
assert_nil h['key with spaces'][:another_key_with_spaces]
|
542
|
-
assert_nil h[:key_with_spaces]
|
543
491
|
|
544
492
|
assert_equal 2, h['key 2 with spaces']['yet another key with spaces']
|
545
493
|
assert_equal 2, h['key 2 with spaces'][:yet_another_key_with_spaces]
|
546
|
-
assert_nil h[:key_2_with_spaces]
|
547
494
|
|
548
495
|
assert_equal 3, h['key 3 with spaces']['yet another key with spaces']
|
549
|
-
assert_nil h['key 3 with spaces'][:yet_another_key_with_spaces]
|
550
|
-
assert_nil h[:key_3_with_spaces]
|
551
496
|
|
552
|
-
h.underscore = true
|
553
|
-
assert_equal true, h[:key_with_spaces].underscore?
|
554
497
|
assert_equal 1, h[:key_with_spaces][:another_key_with_spaces]
|
555
|
-
assert_equal true, h[:key_2_with_spaces].underscore?
|
556
498
|
assert_equal 2, h[:key_2_with_spaces][:yet_another_key_with_spaces]
|
557
|
-
assert_equal true, h[:key_3_with_spaces].underscore?
|
558
499
|
assert_equal 3, h[:key_3_with_spaces][:yet_another_key_with_spaces]
|
559
|
-
|
560
|
-
h.underscore = false
|
561
|
-
assert_nil h[:key_with_spaces]
|
562
|
-
assert_nil h[:key_2_with_spaces]
|
563
|
-
assert_nil h[:key_3_with_spaces]
|
564
|
-
|
565
|
-
assert_equal false, h.underscore?
|
566
|
-
assert_equal true, h['key 2 with spaces'].underscore?
|
567
|
-
assert_equal true, h['key 3 with spaces'].underscore?
|
568
|
-
end
|
569
|
-
|
570
|
-
def test_underscore_clash
|
571
|
-
h = InsensitiveHash.new
|
572
|
-
h.safe = true
|
573
|
-
|
574
|
-
h['hello world'] = 1
|
575
|
-
h['HELLO_world'] = 2
|
576
|
-
assert_equal 1, h['HELLO WORLD']
|
577
|
-
assert_equal 2, h['HELLO_WORLD']
|
578
|
-
assert_equal ['hello world', 'HELLO_world'], h.keys unless eight? # Not order-preserving
|
579
|
-
|
580
|
-
assert_raise(InsensitiveHash::KeyClashError) { h.underscore = true }
|
581
|
-
h.delete('hello world')
|
582
|
-
h.underscore = true
|
583
|
-
|
584
|
-
assert_equal ['HELLO_world'], h.keys
|
585
|
-
assert_equal 2, h['HELLO WORLD']
|
586
|
-
assert_equal 2, h[:HELLO_WORLD]
|
587
|
-
end
|
588
|
-
|
589
|
-
def test_unset_underscore
|
590
|
-
h = InsensitiveHash.new
|
591
|
-
h.underscore = true
|
592
|
-
h[:hello_world] = 1
|
593
|
-
h.underscore = false
|
594
|
-
h['hello world'] = 2
|
595
|
-
|
596
|
-
assert_keys [:hello_world, 'hello world'], h.keys
|
597
|
-
assert_equal 2, h['Hello World']
|
598
500
|
end
|
599
501
|
|
600
502
|
def test_constructor_default
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: insensitive_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test-unit
|