hash_symbolizer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hash_symbolizer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Anjali Shenoy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # HashSymbolizer
2
+
3
+ Extends ActiveSupport's symbolize_keys to recursively symbolize all keys in a gem. By default, it is fully backwards compatible with ActiveSupport's symbolize_keys method. If the recursive flag is set to true however and it recursively symbolizes any values which are hashes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hash_symbolizer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hash_symbolizer
18
+
19
+ ## Usage
20
+
21
+ gem install hash_symbolizer
22
+ or
23
+ include it in your Gemfile and then run bundle install.
24
+
25
+ Examples:
26
+
27
+ {"a" => {"b" => 1}}.symbolize_keys = {:a => {"b" => 1}}
28
+
29
+ {"a" => {"b" => 1}}.symbolize_keys(true) = {:a => {:b => 1}}
30
+
31
+ {"a" => {"b" => 1}}.symbolize_keys! = {:a => {"b" => 1}}
32
+
33
+ {"a" => {"b" => 1}}.symbolize_keys!(true) = {:a => {:b => 1}}
34
+
35
+ Works on Ruby 1.9.2-p290, 1.9.3-p125, ActiveSupport version 3.2.2 as of now.
36
+
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ task :default => :Test
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.pattern = "test/**/*_test.rb"
9
+ t.warning = true
10
+ t.verbose = true
11
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hash_symbolizer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Anjali Shenoy"]
6
+ gem.email = ["anjshenoy@gmail.com"]
7
+ gem.description = %q{Extends ActiveSupport's symbolize_keys to recursively symbolize keys at all levels in a Hash}
8
+ gem.summary = %q{Extends ActiveSupport's symbolize_keys to recursively symbolize all keys in a gem. By default, it is fully backwards compatible with ActiveSupport's symbolize_keys method. If the recursive flag is set to true however and it recursively symbolizes any values which are hashes.}
9
+ gem.homepage = "http://github.com/anjshenoy/hash_symbolizer"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "hash_symbolizer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = HashSymbolizer::VERSION
17
+
18
+ gem.add_dependency "rake"
19
+ gem.add_dependency "activesupport"
20
+ end
@@ -0,0 +1,5 @@
1
+ require "hash_symbolizer/version"
2
+ require 'active_support/core_ext/hash/keys'
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require "hash_symbolizer/core_ext/hash"
5
+ require "hash_symbolizer/core_ext/hash_with_indifferent_access"
@@ -0,0 +1,23 @@
1
+ class Hash
2
+ # Return a new hash with all keys converted to symbols, as long as
3
+ # they respond to +to_sym+.
4
+ # If recursive is set to true, then keys at all levels will be symbolized.
5
+ # undef :symbolize_keys
6
+ # undef :symbolize_keys!
7
+
8
+ def symbolize_keys(recursive = false)
9
+ dup.symbolize_keys!(recursive)
10
+ end
11
+
12
+ # Destructively convert all keys to symbols, as long as they respond
13
+ # to +to_sym+.
14
+ # If recursive is set to true, then keys at all levels will be symbolized.
15
+ def symbolize_keys!(recursive = false)
16
+ keys.each do |key|
17
+ value = delete(key)
18
+ key = key.respond_to?(:to_sym) ? key.to_sym : key
19
+ self[key] = (recursive && value.is_a?(Hash)) ? value.dup.symbolize_keys!(recursive) : value
20
+ end
21
+ self
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ class HashWithIndifferentAccess < Hash
2
+
3
+ undef :symbolize_keys
4
+ # If recursive is set to true, keys at all levels will be symbolized
5
+ def symbolize_keys(recursive = false)
6
+ to_hash.symbolize_keys(recursive)
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module HashSymbolizer
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,123 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../../../lib/hash_symbolizer")
2
+ require 'test/unit'
3
+
4
+ class HashExtTest < Test::Unit::TestCase
5
+ def setup
6
+ @strings = { 'a' => 1, 'b' => 2 }
7
+ @symbols = { :a => 1, :b => 2 }
8
+ @mixed = { :a => 1, 'b' => 2 }
9
+ @fixnums = { 0 => 1, 1 => 2 }
10
+ @illegal_symbols = { [] => 3 }
11
+ @nested_hash = { 'a' => {'b' => 2} }
12
+ @nested_hash_first_level_symbolized = { :a => { 'b' => 2 } }
13
+ @nested_hash_all_keys_symbolized = { :a => { :b => 2 } }
14
+ end
15
+
16
+ def test_methods
17
+ h = {}
18
+ assert_respond_to h, :symbolize_keys
19
+ assert_respond_to h, :symbolize_keys!
20
+ assert_respond_to h, :to_options
21
+ assert_respond_to h, :to_options!
22
+ end
23
+
24
+ def test_symbolize_keys
25
+ assert_equal @symbols, @symbols.symbolize_keys
26
+ assert_equal @symbols, @strings.symbolize_keys
27
+ assert_equal @symbols, @mixed.symbolize_keys
28
+ assert_equal @nested_hash_first_level_symbolized, @nested_hash.symbolize_keys
29
+ end
30
+
31
+ def test_symbolize_keys_with_recursive_option
32
+ assert_equal @symbols, @symbols.symbolize_keys(true)
33
+ assert_equal @symbols, @strings.symbolize_keys(true)
34
+ assert_equal @symbols, @mixed.symbolize_keys(true)
35
+ assert_equal @nested_hash_all_keys_symbolized, @nested_hash.symbolize_keys(true)
36
+ end
37
+
38
+ def test_symbolize_keys!
39
+ assert_equal @symbols, @symbols.dup.symbolize_keys!
40
+ assert_equal @symbols, @strings.dup.symbolize_keys!
41
+ assert_equal @symbols, @mixed.dup.symbolize_keys!
42
+ assert_equal @nested_hash_first_level_symbolized, @nested_hash.symbolize_keys!
43
+ end
44
+
45
+ def test_symbolize_keys_bang_with_recursive_option
46
+ assert_equal @symbols, @symbols.dup.symbolize_keys!(true)
47
+ assert_equal @symbols, @strings.dup.symbolize_keys!(true)
48
+ assert_equal @symbols, @mixed.dup.symbolize_keys!(true)
49
+ assert_equal @nested_hash_all_keys_symbolized, @nested_hash.symbolize_keys!(true)
50
+ end
51
+
52
+ def test_symbolize_keys_preserves_keys_that_cant_be_symbolized
53
+ assert_equal @illegal_symbols, @illegal_symbols.symbolize_keys
54
+ assert_equal @illegal_symbols, @illegal_symbols.dup.symbolize_keys!
55
+ end
56
+
57
+ def test_symbolize_keys_with_recursive_option_preserves_keys_that_cant_be_symbolized
58
+ assert_equal @illegal_symbols, @illegal_symbols.symbolize_keys(true)
59
+ assert_equal @illegal_symbols, @illegal_symbols.dup.symbolize_keys!(true)
60
+ end
61
+
62
+ def test_symbolize_keys_preserves_fixnum_keys
63
+ assert_equal @fixnums, @fixnums.symbolize_keys
64
+ assert_equal @fixnums, @fixnums.dup.symbolize_keys!
65
+ end
66
+
67
+ def test_symbolize_keys_with_recursive_option_preserves_fixnum_keys
68
+ assert_equal @fixnums, @fixnums.symbolize_keys(true)
69
+ assert_equal @fixnums, @fixnums.dup.symbolize_keys!(true)
70
+ end
71
+
72
+ def test_symbolize_keys_for_hash_with_indifferent_access
73
+ assert_instance_of Hash, @symbols.with_indifferent_access.symbolize_keys
74
+ assert_equal @symbols, @symbols.with_indifferent_access.symbolize_keys
75
+ assert_equal @symbols, @strings.with_indifferent_access.symbolize_keys
76
+ assert_equal @symbols, @mixed.with_indifferent_access.symbolize_keys
77
+ end
78
+
79
+ def test_symbolize_keys_with_recursive_option_for_hash_with_indifferent_access
80
+ assert_instance_of Hash, @symbols.with_indifferent_access.symbolize_keys(true)
81
+ assert_equal @symbols, @symbols.with_indifferent_access.symbolize_keys(true)
82
+ assert_equal @symbols, @strings.with_indifferent_access.symbolize_keys(true)
83
+ assert_equal @symbols, @mixed.with_indifferent_access.symbolize_keys(true)
84
+ assert_equal @nested_hash_all_keys_symbolized, @nested_hash.symbolize_keys(true)
85
+ end
86
+
87
+ def test_symbolize_keys_bang_for_hash_with_indifferent_access
88
+ assert_raise(NoMethodError) { @symbols.with_indifferent_access.dup.symbolize_keys! }
89
+ assert_raise(NoMethodError) { @strings.with_indifferent_access.dup.symbolize_keys! }
90
+ assert_raise(NoMethodError) { @mixed.with_indifferent_access.dup.symbolize_keys! }
91
+ assert_raise(NoMethodError) { @nested_hash.with_indifferent_access.dup.symbolize_keys! }
92
+ end
93
+
94
+ def test_symbolize_keys_bang_with_recursive_option_for_hash_with_indifferent_access
95
+ assert_raise(NoMethodError) { @symbols.with_indifferent_access.dup.symbolize_keys!(true) }
96
+ assert_raise(NoMethodError) { @strings.with_indifferent_access.dup.symbolize_keys!(true) }
97
+ assert_raise(NoMethodError) { @mixed.with_indifferent_access.dup.symbolize_keys!(true) }
98
+ assert_raise(NoMethodError) { @nested_hash.with_indifferent_access.dup.symbolize_keys!(true) }
99
+ end
100
+
101
+ def test_symbolize_keys_preserves_keys_that_cant_be_symbolized_for_hash_with_indifferent_access
102
+ assert_equal @illegal_symbols, @illegal_symbols.with_indifferent_access.symbolize_keys
103
+ assert_raise(NoMethodError) { @illegal_symbols.with_indifferent_access.dup.symbolize_keys! }
104
+ end
105
+
106
+ def test_symbolize_keys_with_recursive_option_preserves_keys_that_cant_be_symbolized_for_hash_with_indifferent_access
107
+ assert_equal @illegal_symbols, @illegal_symbols.with_indifferent_access.symbolize_keys(true)
108
+ assert_raise(NoMethodError) { @illegal_symbols.with_indifferent_access.dup.symbolize_keys!(true) }
109
+ end
110
+
111
+ def test_symbolize_keys_preserves_fixnum_keys_for_hash_with_indifferent_access
112
+ assert_equal @fixnums, @fixnums.with_indifferent_access.symbolize_keys
113
+ assert_raise(NoMethodError) { @fixnums.with_indifferent_access.dup.symbolize_keys! }
114
+ end
115
+
116
+ def test_symbolize_keys_with_recursive_option_preserves_fixnum_keys_for_hash_with_indifferent_access
117
+ assert_equal @fixnums, @fixnums.with_indifferent_access.symbolize_keys(true)
118
+ assert_raise(NoMethodError) { @fixnums.with_indifferent_access.dup.symbolize_keys!(true) }
119
+ end
120
+
121
+
122
+ end
123
+
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_symbolizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anjali Shenoy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-04 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70148863629780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70148863629780
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70148863629360 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70148863629360
36
+ description: Extends ActiveSupport's symbolize_keys to recursively symbolize keys
37
+ at all levels in a Hash
38
+ email:
39
+ - anjshenoy@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - hash_symbolizer.gemspec
50
+ - lib/hash_symbolizer.rb
51
+ - lib/hash_symbolizer/core_ext/hash.rb
52
+ - lib/hash_symbolizer/core_ext/hash_with_indifferent_access.rb
53
+ - lib/hash_symbolizer/version.rb
54
+ - test/lib/hash_symbolizer/hash_ext_test.rb
55
+ homepage: http://github.com/anjshenoy/hash_symbolizer
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.6
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Extends ActiveSupport's symbolize_keys to recursively symbolize all keys
79
+ in a gem. By default, it is fully backwards compatible with ActiveSupport's symbolize_keys
80
+ method. If the recursive flag is set to true however and it recursively symbolizes
81
+ any values which are hashes.
82
+ test_files:
83
+ - test/lib/hash_symbolizer/hash_ext_test.rb