insensitive_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
6
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in insensitive_hash.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,64 @@
1
+ # insensitive_hash
2
+
3
+ Hash with case-insensitive, Symbol/String-indifferent key access.
4
+
5
+ ## Installation
6
+ ```
7
+ gem install insensitive_hash
8
+ ```
9
+
10
+ ## Examples
11
+
12
+ ### Instantiation
13
+ ```ruby
14
+ require 'insensitive_hash'
15
+
16
+ # Monkey-patched Hash#insensitive method
17
+ {'abc' => 1, :def => 2}.insensitive
18
+
19
+ # Or,
20
+ InsensitiveHash.new(:abc => 1, 'DEF' => 2)
21
+ ```
22
+
23
+ ### Usage
24
+ ```ruby
25
+ ih = InsensitiveHash.new(:abc => 1, 'DEF' => 2)
26
+
27
+ # Case-insensitive, Symbol/String-indifferent access.
28
+ ih['Abc'] # 1
29
+ ih[:ABC] # 1
30
+ ih['abc'] # 1
31
+ ih[:abc] # 1
32
+ ih.has_key?(:DeF) # true
33
+
34
+ ih['ABC'] = 10
35
+
36
+ # keys and values
37
+ ih.keys # ['DEF', 'ABC']
38
+ ih.values # [2, 10]
39
+
40
+ # delete
41
+ ih.delete :Abc # 10
42
+ ih.keys # ['DEF']
43
+
44
+ # Hashes and Hashes in Arrays as values are automatically converted to be insensitive
45
+ # (Useful for processing YAML inputs)
46
+ ih['kids'] = { :hello => [ { :world => '!!!' } ] }
47
+ ih[:kids]['Hello'].first['WORLD'] # !!!
48
+ ```
49
+
50
+ ## Contributing to insensitive_hash
51
+
52
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
53
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
54
+ * Fork the project
55
+ * Start a feature/bugfix branch
56
+ * Commit and push until you are happy with your contribution
57
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
58
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
59
+
60
+ ## Copyright
61
+
62
+ Copyright (c) 2011 Junegunn Choi. See LICENSE.txt for
63
+ further details.
64
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "insensitive_hash/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "insensitive_hash"
7
+ s.version = InsensitiveHash::VERSION
8
+ s.authors = ["Junegunn Choi"]
9
+ s.email = ["junegunn.c@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Case-insensitive Ruby Hash}
12
+
13
+ s.description = %q{Hash with case-insensitive, Symbol/String-indifferent key access}
14
+
15
+ s.rubyforge_project = "insensitive_hash"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ # specify any dependencies here; for example:
23
+ # s.add_development_dependency "rspec"
24
+ # s.add_runtime_dependency "rest-client"
25
+ end
@@ -0,0 +1,60 @@
1
+ require "insensitive_hash/version"
2
+
3
+ class InsensitiveHash < Hash
4
+ def initialize hash = {}
5
+ @key_map = hash.keys.inject({}) { |h, k| h[encode k] = k; h }
6
+
7
+ hash.each do |key, value|
8
+ self[key] = InsensitiveHash.wrap 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
54
+
55
+ class Hash
56
+ def insensitive
57
+ InsensitiveHash.new self
58
+ end
59
+ end
60
+
@@ -0,0 +1,3 @@
1
+ class InsensitiveHash < Hash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require 'insensitive_hash'
5
+
6
+ class TestInsensitiveHash < Test::Unit::TestCase
7
+ def test_from_hash
8
+ hash = {
9
+ 'a' => 1,
10
+ 'B' => 2,
11
+ :c => { :D => [ { 'e' => 3 } ] }
12
+ }
13
+
14
+ [hash.insensitive, InsensitiveHash.new(hash)].each do |ih|
15
+ assert ih.is_a?(Hash)
16
+ assert_equal InsensitiveHash, ih.class
17
+ ['c', 'C', :c, :C].each do |c|
18
+ assert_equal InsensitiveHash, ih[c].class
19
+
20
+ ['d', 'D', :d, :D].each do |d|
21
+ assert_equal InsensitiveHash, ih[c][d].first.class
22
+ end
23
+ end
24
+ end
25
+
26
+ #hash = {"source db"=>{"driver"=>"com.mysql.jdbc.Driver", "url"=>"jdbc:mysql://localhost/test", "user"=>"root"}, "target db"=>{"driver"=>"com.mysql.jdbc.Driver", "url"=>"jdbc:mysql://localhost/test", "user"=>"root"}, "mappings"=>[{"table name"=>"a", "writers/partition"=>0}]}
27
+ #p Marshal.load(Marshal.dump(hash)).insensitive
28
+ end
29
+
30
+ def test_has_key_set
31
+ ih = InsensitiveHash.new
32
+ ih['a'] = 1
33
+ ih['A'] = 2
34
+
35
+ ['a', 'A', :a, :A].each do |a|
36
+ assert ih.has_key?(a)
37
+ assert_equal 2, ih[a]
38
+ end
39
+
40
+ assert_equal ['A'], ih.keys
41
+ assert_equal [2], ih.values
42
+
43
+ ih[:A] = 4
44
+ assert_equal [:A], ih.keys
45
+ assert_equal [4], ih.values
46
+
47
+ ih[:b] = { :c => 5 }
48
+ assert ih.keys.include?(:A)
49
+ assert ih.keys.include?(:b)
50
+ assert_equal 2, ih.keys.length
51
+ assert_equal 5, ih['B']['C']
52
+
53
+ ih['c'] = [ { 'x' => 1 }, { :y => 2 } ]
54
+ assert ih.keys.include?('c')
55
+ assert_equal 3, ih.keys.length
56
+ assert_equal 1, ih[:c].first[:x]
57
+ assert_equal 2, ih[:c].last['Y']
58
+
59
+ ih[5] = 50
60
+ assert_equal 50, ih[5]
61
+ end
62
+
63
+ def test_delete
64
+ ih = InsensitiveHash.new(:a => 1)
65
+ assert_equal [:a], ih.keys
66
+ assert_equal [1], ih.values
67
+
68
+ assert_equal 1, ih.delete('A')
69
+ assert_equal [], ih.keys
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insensitive_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-27 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Hash with case-insensitive, Symbol/String-indifferent key access
15
+ email:
16
+ - junegunn.c@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.markdown
24
+ - Rakefile
25
+ - insensitive_hash.gemspec
26
+ - lib/insensitive_hash.rb
27
+ - lib/insensitive_hash/version.rb
28
+ - test/test_insensitive_hash.rb
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: insensitive_hash
49
+ rubygems_version: 1.8.6
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Case-insensitive Ruby Hash
53
+ test_files:
54
+ - test/test_insensitive_hash.rb