to_2d_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,3 @@
1
+ pkg
2
+ *.DS_Store*
3
+ Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.0.1 (March 27, 2012)
2
+
3
+ Features:
4
+
5
+ - initial code, tests, and documentation
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Floris Huetink
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.md ADDED
@@ -0,0 +1,15 @@
1
+ to_2d_hash
2
+ ===
3
+
4
+ Convert an existing Hash to a two-dimensional Hash with concatenated keys:
5
+
6
+ h = {'a' => 'b', 'c' => {'d' => 'e'}}
7
+ h.to_2d_hash
8
+ # gives {'a' => 'b', 'c_d' => 'e'}
9
+
10
+ Options
11
+ ---
12
+
13
+ - `:delimiter` : glue string to use for key concatenation. Defaults to '_'
14
+
15
+ Copyright (c) 2012 Floris Huetink
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ begin
2
+ require "bundler"
3
+ Bundler.setup
4
+ rescue LoadError
5
+ $stderr.puts "You need to have Bundler installed to be able build this gem."
6
+ end
7
+
8
+ gemspec = eval(File.read(Dir["*.gemspec"].first))
9
+
10
+
11
+ desc "Validate the gemspec"
12
+ task :gemspec do
13
+ gemspec.validate
14
+ end
15
+
16
+ desc "Build gem locally"
17
+ task :build => :gemspec do
18
+ system "gem build #{gemspec.name}.gemspec"
19
+ FileUtils.mkdir_p "pkg"
20
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
21
+ end
22
+
23
+ desc "Install gem locally"
24
+ task :install => :build do
25
+ system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
26
+ end
27
+
28
+ desc "Clean automatically generated files"
29
+ task :clean do
30
+ FileUtils.rm_rf "pkg"
31
+ end
@@ -0,0 +1,20 @@
1
+ class Hash
2
+ # Create a two-dimensional copy of the current hash
3
+ #
4
+ # @option opts [String] :delimiter ('_') Delimiter to use when concatenating keys
5
+ # @option opts [String] :prefix (nil) Key prefix. Set automatically when converting nested hashes recursively. No need to set this manually.
6
+ #
7
+ # @return [Hash]
8
+ def to_2d_hash(opts={})
9
+ output = self.class.new
10
+ self.each do |k, v|
11
+ key = opts[:prefix] ? "#{opts[:prefix]}#{opts[:delimiter]||'_'}#{k}" : k
12
+ if v.is_a? Hash
13
+ output.merge! v.to_2d_hash(opts.merge(prefix: key))
14
+ else
15
+ output[key] = v
16
+ end
17
+ end
18
+ output
19
+ end
20
+ end
data/lib/to_2d_hash.rb ADDED
@@ -0,0 +1 @@
1
+ require "hash/to_2d_hash"
File without changes
@@ -0,0 +1,40 @@
1
+ require 'test/unit'
2
+ require 'hash/to_2d_hash'
3
+ class TestTo2dHash < Test::Unit::TestCase
4
+
5
+ def test_default_conversion
6
+ input = {
7
+ "k1" => "v1",
8
+ "k2" => {
9
+ "k21" => "v21",
10
+ "k22" => "v22",
11
+ "k23" => {
12
+ "k231" => "v231"
13
+ }
14
+ }
15
+ }
16
+ test = {
17
+ "k1" => "v1",
18
+ "k2_k21" => "v21",
19
+ "k2_k22" => "v22",
20
+ "k2_k23_k231" => "v231"
21
+ }
22
+
23
+ assert_equal test, input.to_2d_hash
24
+ end
25
+
26
+ def test_custom_delimiter
27
+ input = {
28
+ "k1" => {
29
+ "k11" => "v11"
30
+ }
31
+ }
32
+
33
+ test = {
34
+ "k1:k11" => "v11"
35
+ }
36
+
37
+ assert_equal test, input.to_2d_hash(delimiter: ':')
38
+
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "to_2d_hash"
3
+ s.version = "0.0.1"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.authors = ["Floris Huetink"]
6
+ s.email = ["floris@avocado.nl"]
7
+ s.homepage = "http://github.com/florish/gem_template"
8
+ s.summary = "Hash#to_2d_hash"
9
+ s.description = "Converts a nested Hash to a two-dimensional Hash with concatenated keys"
10
+ s.rubyforge_project = s.name
11
+
12
+ s.required_ruby_version = '>= 1.9.2'
13
+ s.required_rubygems_version = ">= 1.8"
14
+
15
+ # If you have runtime dependencies, add them here
16
+ # s.add_runtime_dependency "other", "~> 1.2"
17
+
18
+ # If you have development dependencies, add them here
19
+ # s.add_development_dependency "another", "= 0.9"
20
+
21
+ # The list of files to be contained in the gem
22
+ s.files = `git ls-files`.split("\n")
23
+ # s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
24
+ # s.extensions = `git ls-files ext/extconf.rb`.split("\n")
25
+
26
+ s.require_path = 'lib'
27
+
28
+ # For C extensions
29
+ # s.extensions = "ext/extconf.rb"
30
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_2d_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Floris Huetink
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Converts a nested Hash to a two-dimensional Hash with concatenated keys
15
+ email:
16
+ - floris@avocado.nl
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/hash/to_2d_hash.rb
28
+ - lib/to_2d_hash.rb
29
+ - test/test_helper.rb
30
+ - test/to_2d_hash_test.rb
31
+ - to_2d_hash.gemspec
32
+ homepage: http://github.com/florish/gem_template
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.9.2
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '1.8'
50
+ requirements: []
51
+ rubyforge_project: to_2d_hash
52
+ rubygems_version: 1.8.21
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Hash#to_2d_hash
56
+ test_files: []