insensitive_hash 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,6 @@
1
+ ### 0.1.0 / 2011/12/20
2
+ * Can opt-out of monkey-patching Hash with `require 'insensitive_hash/minimal'`
3
+
1
4
  ### 0.0.2-3 / 2011/10/28
2
5
  * Removed duplicated code in the constructor
3
6
  * More tests
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Junegunn Choi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown CHANGED
@@ -1,5 +1,4 @@
1
1
  # insensitive_hash
2
-
3
2
  Hash with case-insensitive, Symbol/String-indifferent key access.
4
3
 
5
4
  ## Installation
@@ -20,7 +19,9 @@ require 'insensitive_hash'
20
19
  InsensitiveHash.new(:abc => 1, 'DEF' => 2)
21
20
  ```
22
21
 
23
- ### Usage
22
+ If you don't like to have Hash#insensitive method, `require 'insensitive_hash/minimal'`
23
+
24
+ ### Basic usage
24
25
  ```ruby
25
26
  ih = InsensitiveHash.new(:abc => 1, 'DEF' => 2)
26
27
 
@@ -40,9 +41,13 @@ ih.values # [2, 10]
40
41
  # delete
41
42
  ih.delete :Abc # 10
42
43
  ih.keys # ['DEF']
44
+ ```
43
45
 
44
- # Hashes and Hashes in Arrays as values are automatically converted to be insensitive
46
+ ### "Inherited insensitivity"
47
+ ```ruby
48
+ # Hash values are recursively converted to be insensitive
45
49
  # (Useful when processing YAML inputs)
50
+ ih = InsensitiveHash.new
46
51
  ih['kids'] = { :hello => [ { :world => '!!!' } ] }
47
52
  ih[:kids]['Hello'].first['WORLD'] # !!!
48
53
 
@@ -50,6 +55,15 @@ ih['one'] = [ [ [ { :a => { :b => { :c => 'd' } } } ] ] ]
50
55
  ih['one'].first.first.first['A']['b'][:C] # 'd'
51
56
  ```
52
57
 
58
+ ### Processing case-insensitive YAML input
59
+ ```ruby
60
+ db = YAML.load(File.read 'database.yml').insensitive
61
+
62
+ # Access values however you like
63
+ db['Development']['ADAPTER']
64
+ db[:production][:adapter]
65
+ ```
66
+
53
67
  ## Contributing to insensitive_hash
54
68
 
55
69
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = InsensitiveHash::VERSION
8
8
  s.authors = ["Junegunn Choi"]
9
9
  s.email = ["junegunn.c@gmail.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/junegunn/insensitive_hash"
11
11
  s.summary = %q{Case-insensitive Ruby Hash}
12
12
 
13
13
  s.description = %q{Hash with case-insensitive, Symbol/String-indifferent key access}
@@ -1,56 +1,5 @@
1
- require "insensitive_hash/version"
2
-
3
- class InsensitiveHash < Hash
4
- def initialize hash = {}
5
- @key_map = {}
6
-
7
- hash.each do |key, value|
8
- self[key] = value
9
- end
10
- end
11
-
12
- def [] key
13
- super(@key_map[encode key])
14
- end
15
-
16
- def []= key, value
17
- delete key
18
- @key_map[encode key] = key
19
-
20
- super(key, InsensitiveHash.wrap(value))
21
- end
22
-
23
- def has_key? key
24
- super @key_map[encode key]
25
- end
26
-
27
- def delete key
28
- super @key_map[encode key]
29
- end
30
-
31
- private
32
- def self.wrap value
33
- case value
34
- when Hash
35
- value.class == InsensitiveHash ? value : InsensitiveHash.new(value)
36
- when Array
37
- value.map { |v| InsensitiveHash.wrap v }
38
- else
39
- value
40
- end
41
- end
42
-
43
- def encode key
44
- case key
45
- when String
46
- key.downcase
47
- when Symbol
48
- key.to_s.downcase
49
- else
50
- key
51
- end
52
- end
53
- end
1
+ require 'insensitive_hash/version'
2
+ require 'insensitive_hash/insensitive_hash'
54
3
 
55
4
  class Hash
56
5
  def insensitive
@@ -0,0 +1,52 @@
1
+ class InsensitiveHash < Hash
2
+ def initialize hash = {}
3
+ @key_map = {}
4
+
5
+ hash.each do |key, value|
6
+ self[key] = value
7
+ end
8
+ end
9
+
10
+ def [] key
11
+ super(@key_map[encode key])
12
+ end
13
+
14
+ def []= key, value
15
+ delete key
16
+ @key_map[encode key] = key
17
+
18
+ super(key, InsensitiveHash.wrap(value))
19
+ end
20
+
21
+ def has_key? key
22
+ super @key_map[encode key]
23
+ end
24
+
25
+ def delete key
26
+ super @key_map[encode key]
27
+ end
28
+
29
+ private
30
+ def self.wrap value
31
+ case value
32
+ when Hash
33
+ value.class == InsensitiveHash ? value : InsensitiveHash.new(value)
34
+ when Array
35
+ value.map { |v| InsensitiveHash.wrap v }
36
+ else
37
+ value
38
+ end
39
+ end
40
+
41
+ def encode key
42
+ case key
43
+ when String
44
+ key.downcase
45
+ when Symbol
46
+ key.to_s.downcase
47
+ else
48
+ key
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,3 @@
1
+ require 'insensitive_hash/version'
2
+ require 'insensitive_hash/insensitive_hash'
3
+
@@ -1,3 +1,3 @@
1
1
  class InsensitiveHash < Hash
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- require 'insensitive_hash'
4
+ require 'insensitive_hash/minimal'
5
5
 
6
6
  class TestInsensitiveHash < Test::Unit::TestCase
7
7
  def test_from_hash
@@ -11,6 +11,11 @@ class TestInsensitiveHash < Test::Unit::TestCase
11
11
  :c => { :D => [ { 'e' => 3 } ] }
12
12
  }
13
13
 
14
+ assert_raise(NoMethodError) {
15
+ hash.insensitive
16
+ }
17
+
18
+ require 'insensitive_hash'
14
19
  [hash.insensitive, InsensitiveHash.new(hash)].each do |ih|
15
20
  assert ih.is_a?(Hash)
16
21
  assert_equal InsensitiveHash, ih.class
metadata CHANGED
@@ -1,55 +1,66 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: insensitive_hash
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.1.0
6
6
  platform: ruby
7
- authors:
8
- - Junegunn Choi
7
+ authors:
8
+ - Junegunn Choi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-28 00:00:00.000000000Z
12
+
13
+ date: 2011-12-20 00:00:00 Z
13
14
  dependencies: []
15
+
14
16
  description: Hash with case-insensitive, Symbol/String-indifferent key access
15
- email:
16
- - junegunn.c@gmail.com
17
+ email:
18
+ - junegunn.c@gmail.com
17
19
  executables: []
20
+
18
21
  extensions: []
22
+
19
23
  extra_rdoc_files: []
20
- files:
21
- - .gitignore
22
- - CHANGELOG.markdown
23
- - Gemfile
24
- - README.markdown
25
- - Rakefile
26
- - insensitive_hash.gemspec
27
- - lib/insensitive_hash.rb
28
- - lib/insensitive_hash/version.rb
29
- - test/test_insensitive_hash.rb
30
- homepage: ''
24
+
25
+ files:
26
+ - .gitignore
27
+ - CHANGELOG.markdown
28
+ - Gemfile
29
+ - LICENSE.txt
30
+ - README.markdown
31
+ - Rakefile
32
+ - insensitive_hash.gemspec
33
+ - lib/insensitive_hash.rb
34
+ - lib/insensitive_hash/insensitive_hash.rb
35
+ - lib/insensitive_hash/minimal.rb
36
+ - lib/insensitive_hash/version.rb
37
+ - test/test_insensitive_hash.rb
38
+ homepage: https://github.com/junegunn/insensitive_hash
31
39
  licenses: []
40
+
32
41
  post_install_message:
33
42
  rdoc_options: []
34
- require_paths:
35
- - lib
36
- required_ruby_version: !ruby/object:Gem::Requirement
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
37
47
  none: false
38
- requirements:
39
- - - ! '>='
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
- required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
53
  none: false
44
- requirements:
45
- - - ! '>='
46
- - !ruby/object:Gem::Version
47
- version: '0'
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
48
58
  requirements: []
59
+
49
60
  rubyforge_project: insensitive_hash
50
- rubygems_version: 1.8.6
61
+ rubygems_version: 1.8.11
51
62
  signing_key:
52
63
  specification_version: 3
53
64
  summary: Case-insensitive Ruby Hash
54
- test_files:
55
- - test/test_insensitive_hash.rb
65
+ test_files:
66
+ - test/test_insensitive_hash.rb