insensitive_hash 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.markdown +7 -3
- data/README.markdown +44 -13
- data/lib/insensitive_hash.rb +1 -0
- data/lib/insensitive_hash/insensitive_hash.rb +14 -1
- data/lib/insensitive_hash/version.rb +1 -1
- data/test/test_insensitive_hash.rb +44 -2
- metadata +10 -6
data/CHANGELOG.markdown
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
+
### 0.2.4
|
2
|
+
* Bug fix: Invalid `dup`, `clone` behavior
|
3
|
+
* :safe setting from `Hash#insensitive`
|
4
|
+
|
1
5
|
### 0.2.3 / 2012/02/18
|
2
|
-
* Key-clash detection made optional with `
|
6
|
+
* Key-clash detection made optional with `InsensitiveHash#safe=`
|
3
7
|
* Ruby 1.8 compatibility
|
4
8
|
|
5
9
|
### 0.2.2 / 2012/02/13
|
6
10
|
* :underscore option added
|
7
|
-
* `
|
11
|
+
* `InsensitiveHash::KeyClashError` added for safer operations
|
8
12
|
* Bug fix: Preserve default/default_proc on merge/replace/insensitive
|
9
|
-
* Subtle behavioral change in `
|
13
|
+
* Subtle behavioral change in `InsensitiveHash#replace` when called with an ordinary Hash
|
10
14
|
|
11
15
|
### 0.2.1 / 2012/02/10
|
12
16
|
* Bug fix: Insensitive `fetch`
|
data/README.markdown
CHANGED
@@ -12,23 +12,41 @@ Examples
|
|
12
12
|
--------
|
13
13
|
|
14
14
|
### Instantiation
|
15
|
+
|
15
16
|
```ruby
|
16
17
|
require 'insensitive_hash'
|
17
18
|
|
18
|
-
|
19
|
-
ih = {'abc' => 1, :def => 2}.insensitive
|
19
|
+
ih = {}.insensitive
|
20
|
+
ih = { 'abc' => 1, :def => 2 }.insensitive
|
21
|
+
ih = { 'hello world' => true }.insensitive(:underscore => true)
|
22
|
+
# :underscore option allows ih[:hello_world]
|
23
|
+
```
|
24
|
+
|
25
|
+
### Instantiation without monkey-patching Hash
|
26
|
+
|
27
|
+
If you don't like to have Hash#insensitive method, `require 'insensitive_hash/minimal'`
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'insensitive_hash/minimal'
|
31
|
+
|
32
|
+
ih = InsensitiveHash.new
|
33
|
+
ih = InsensitiveHash.new(:default_value)
|
34
|
+
ih = InsensitiveHash.new { |h, k| :default_value_from_block }
|
35
|
+
|
36
|
+
ih = InsensitiveHash[ 'abc' => 1, :def => 2 ]
|
37
|
+
ih = InsensitiveHash[ 'abc', 1, :def, 2 ]
|
38
|
+
ih = InsensitiveHash[ [['abc', 1], [:def, 2]] ]
|
20
39
|
|
21
|
-
|
22
|
-
|
23
|
-
|
40
|
+
ih = InsensitiveHash[ 'hello world' => true ].tap { |ih| ih.underscore = true }
|
41
|
+
```
|
42
|
+
|
43
|
+
### Revert to normal Hash
|
24
44
|
|
25
|
-
|
45
|
+
```ruby
|
26
46
|
h = ih.sensitive
|
27
47
|
h = ih.to_hash
|
28
48
|
```
|
29
49
|
|
30
|
-
If you don't like to have Hash#insensitive method, `require 'insensitive_hash/minimal'`
|
31
|
-
|
32
50
|
### Basic usage
|
33
51
|
```ruby
|
34
52
|
ih = InsensitiveHash[:abc => 1, 'DEF' => 2]
|
@@ -76,16 +94,29 @@ db[:production][:adapter]
|
|
76
94
|
```ruby
|
77
95
|
h = { 'A key with spaces' => true }
|
78
96
|
|
79
|
-
ih = h.insensitive
|
97
|
+
ih = h.insensitive(:underscore => true)
|
80
98
|
ih[:a_key_with_spaces] # true
|
81
99
|
|
82
|
-
# Or,
|
83
|
-
ih = InsensitiveHash[ h ]
|
84
|
-
ih.underscore = true
|
100
|
+
# Or without Hash#insensitive,
|
101
|
+
ih = InsensitiveHash[ h ].tap { |ih| ih.underscore = true }
|
85
102
|
ih.underscore? # true
|
86
103
|
ih[:a_key_with_spaces] # true
|
87
104
|
```
|
88
105
|
|
106
|
+
### Enabling key-clash detection
|
107
|
+
```ruby
|
108
|
+
ih = InsensitiveHash.new.tap { |ih| ih.safe = true }
|
109
|
+
ih.safe? # true
|
110
|
+
|
111
|
+
# Will raise InsensitiveHash::KeyClashError
|
112
|
+
h.merge!('hello world' => 1, :hello_world => 2)
|
113
|
+
|
114
|
+
# Disables key-clash detection
|
115
|
+
h.safe = false
|
116
|
+
h.merge!('hello world' => 1, :hello_world => 2)
|
117
|
+
h['Hello World'] # 2
|
118
|
+
```
|
119
|
+
|
89
120
|
## Contributing to insensitive_hash
|
90
121
|
|
91
122
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
@@ -98,6 +129,6 @@ ih[:a_key_with_spaces] # true
|
|
98
129
|
|
99
130
|
## Copyright
|
100
131
|
|
101
|
-
Copyright (c)
|
132
|
+
Copyright (c) 2012 Junegunn Choi. See LICENSE.txt for
|
102
133
|
further details.
|
103
134
|
|
data/lib/insensitive_hash.rb
CHANGED
@@ -4,6 +4,7 @@ class InsensitiveHash < Hash
|
|
4
4
|
|
5
5
|
def initialize default = nil, &block
|
6
6
|
if block_given?
|
7
|
+
raise ArgumentError.new('wrong number of arguments') unless default.nil?
|
7
8
|
super &block
|
8
9
|
else
|
9
10
|
super
|
@@ -41,7 +42,7 @@ class InsensitiveHash < Hash
|
|
41
42
|
# @param [Boolean]
|
42
43
|
# @return [Boolean]
|
43
44
|
def safe= s
|
44
|
-
raise ArgumentError.new("
|
45
|
+
raise ArgumentError.new("Neither true nor false") unless [true, false].include?(s)
|
45
46
|
@safe = s
|
46
47
|
end
|
47
48
|
|
@@ -139,6 +140,18 @@ class InsensitiveHash < Hash
|
|
139
140
|
super *args, &block
|
140
141
|
end
|
141
142
|
|
143
|
+
def dup
|
144
|
+
super.tap { |copy|
|
145
|
+
copy.instance_variable_set :@key_map, @key_map.dup
|
146
|
+
}
|
147
|
+
end
|
148
|
+
|
149
|
+
def clone
|
150
|
+
super.tap { |copy|
|
151
|
+
copy.instance_variable_set :@key_map, @key_map.dup
|
152
|
+
}
|
153
|
+
end
|
154
|
+
|
142
155
|
private
|
143
156
|
def wrap value, us
|
144
157
|
case value
|
@@ -17,7 +17,6 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
17
17
|
else
|
18
18
|
assert_equal set1, set2
|
19
19
|
end
|
20
|
-
|
21
20
|
end
|
22
21
|
|
23
22
|
def test_has_key_set
|
@@ -142,6 +141,19 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
142
141
|
ih.underscore = true
|
143
142
|
assert ih[:a_key_with_spaces]
|
144
143
|
assert ih.underscore?
|
144
|
+
|
145
|
+
# :safe
|
146
|
+
ih = {}.insensitive
|
147
|
+
assert_raise(ArgumentError) {
|
148
|
+
ihs = {'a' => 1}.insensitive(:safe => 1)
|
149
|
+
}
|
150
|
+
ihs = {'a' => 1}.insensitive(:safe => true)
|
151
|
+
assert !ih.safe?
|
152
|
+
assert ihs.safe?
|
153
|
+
|
154
|
+
assert_raise(InsensitiveHash::KeyClashError) {
|
155
|
+
ihs.merge(:a => 2, 'A' => 3)
|
156
|
+
}
|
145
157
|
end
|
146
158
|
|
147
159
|
def test_delete
|
@@ -563,7 +575,7 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
563
575
|
h['HELLO_world'] = 2
|
564
576
|
assert_equal 1, h['HELLO WORLD']
|
565
577
|
assert_equal 2, h['HELLO_WORLD']
|
566
|
-
assert_equal ['hello world', 'HELLO_world'], h.keys
|
578
|
+
assert_equal ['hello world', 'HELLO_world'], h.keys unless eight? # Not order-preserving
|
567
579
|
|
568
580
|
assert_raise(InsensitiveHash::KeyClashError) { h.underscore = true }
|
569
581
|
h.delete('hello world')
|
@@ -584,5 +596,35 @@ class TestInsensitiveHash < Test::Unit::TestCase
|
|
584
596
|
assert_keys [:hello_world, 'hello world'], h.keys
|
585
597
|
assert_equal 2, h['Hello World']
|
586
598
|
end
|
599
|
+
|
600
|
+
def test_constructor_default
|
601
|
+
h = InsensitiveHash.new :default
|
602
|
+
assert_equal :default, h[:xxx]
|
603
|
+
|
604
|
+
h = InsensitiveHash.new { :default_from_block }
|
605
|
+
assert_equal :default_from_block, h[:xxx]
|
606
|
+
|
607
|
+
# But not both!
|
608
|
+
assert_raise(ArgumentError) {
|
609
|
+
InsensitiveHash.new(:default) { :default_from_block }
|
610
|
+
}
|
611
|
+
end
|
612
|
+
|
613
|
+
def test_dup_clone
|
614
|
+
a = InsensitiveHash.new
|
615
|
+
a[:key] = :value
|
616
|
+
|
617
|
+
b = a.dup
|
618
|
+
b.delete 'key'
|
619
|
+
assert_nil b[:key]
|
620
|
+
|
621
|
+
assert_equal :value, a[:key]
|
622
|
+
|
623
|
+
c = a.clone
|
624
|
+
c.delete 'KEY'
|
625
|
+
assert_nil c[:key]
|
626
|
+
|
627
|
+
assert_equal :value, a[:key]
|
628
|
+
end
|
587
629
|
end
|
588
630
|
|
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.2.
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test-unit
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: 2.3.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.3.0
|
25
30
|
description: Hash with case-insensitive, Symbol/String-indifferent key access
|
26
31
|
email:
|
27
32
|
- junegunn.c@gmail.com
|
@@ -61,10 +66,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
66
|
version: '0'
|
62
67
|
requirements: []
|
63
68
|
rubyforge_project: insensitive_hash
|
64
|
-
rubygems_version: 1.8.
|
69
|
+
rubygems_version: 1.8.24
|
65
70
|
signing_key:
|
66
71
|
specification_version: 3
|
67
72
|
summary: Case-insensitive Ruby Hash
|
68
73
|
test_files:
|
69
74
|
- test/test_insensitive_hash.rb
|
70
|
-
has_rdoc:
|