deep_hash 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in deep_hash.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ deep_hash
2
+ =========
3
+
4
+ `deep_hash` is a Ruby gem that provides ways to deal with multi-level default hashes.
5
+
6
+ Usage
7
+ -----
8
+
9
+ Right now the only functionality is to create such a hash. This is useful if you use multi-dimensional default
10
+ hashes a lot. For example, multi-dimensional tables of counts (defaulting to 0) are often useful.
11
+
12
+ ```ruby
13
+ require "deep_hash"
14
+ h = Hash.deep(0) # is equivalent to
15
+ h = {}
16
+
17
+ h = Hash.deep(2) # is equivalent to
18
+ h = Hash.new { |k1, v1| k1[v1] = Hash.new { |k2, v2| k2[v2] = {} } }
19
+
20
+ h = Hash.deep(0) { "foobar" } # is equivalent to
21
+ h = Hash.new { |k, v| k[v] = "foobar" }
22
+
23
+ h = Hash.deep(1) { 0 } is equivalent to
24
+ h = Hash.new { |k1, v1| k1[v1] = Hash.new { |k2, v2| k2[v2] = 0 } }
25
+ ```
26
+
27
+ Installation
28
+ ------------
29
+
30
+ gem install deep_hash
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/deep_hash.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "deep_hash/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "deep_hash"
7
+ s.version = DeepHash::VERSION
8
+ s.authors = ["Caleb Spare"]
9
+ s.email = ["cespare@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Utilities for dealing with multi-level hashes}
12
+ s.description = %q{DeepHash is a collection of utilities for dealing with multi-level hashes.}
13
+
14
+ s.rubyforge_project = "deep_hash"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "scope", "~> 0.2.2"
22
+ end
@@ -0,0 +1,3 @@
1
+ module DeepHash
2
+ VERSION = "0.0.1"
3
+ end
data/lib/deep_hash.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "deep_hash/version"
2
+
3
+ class Hash
4
+ def self.deep(depth, &block)
5
+ raise "Need non-negative depth" if depth < 0
6
+ if depth.zero?
7
+ return block_given? ? Hash.new { |k, v| k[v] = block.call } : {}
8
+ end
9
+ Hash.new { |k, v| k[v] = Hash.deep(depth - 1, &block) }
10
+ end
11
+ end
@@ -0,0 +1,51 @@
1
+ require "scope"
2
+ require "minitest/autorun"
3
+
4
+ require "deep_hash"
5
+
6
+ class DeepHashTest < Scope::TestCase
7
+ def assert_expected_hash(expected, actual)
8
+ assert expected.is_a? Hash
9
+ assert actual.is_a? Hash
10
+ assert_equal expected.keys.sort, actual.keys.sort
11
+ expected.keys.each do |key|
12
+ if expected[key].is_a? Hash
13
+ assert_expected_hash expected[key], actual[key]
14
+ else
15
+ assert_equal expected[key], actual[key]
16
+ end
17
+ end
18
+ end
19
+
20
+ context "deep hash creation" do
21
+ context "with no block" do
22
+ should "create a plain hash with depth 0" do
23
+ assert_expected_hash({}, Hash.deep(0))
24
+ end
25
+
26
+ should "should create a properly nested hash of the specified depth" do
27
+ hash = Hash.deep(1)
28
+ hash[0][1] = 2
29
+ assert_equal({}, hash[1])
30
+ assert_expected_hash({ 0 => { 1 => 2 }, 1 => {} }, hash)
31
+ end
32
+ end
33
+
34
+ context "with a block given" do
35
+ should "create a normal default-valued hash with depth 0" do
36
+ hash = Hash.deep(0) { "foobar" }
37
+ hash[:a] = 3
38
+ assert_equal "foobar", hash[:b]
39
+ assert_expected_hash({ :a => 3, :b => "foobar" }, hash)
40
+ end
41
+
42
+ should "properly create a mult-level default hash" do
43
+ hash = Hash.deep(1) { "foobar" }
44
+ hash[:a][0] = 5
45
+ assert_equal "foobar", hash[:a][1]
46
+ assert_equal "foobar", hash[:b][0]
47
+ assert_expected_hash({ :a => { 0 => 5, 1 => "foobar" }, :b => { 0 => "foobar" } }, hash)
48
+ end
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deep_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Caleb Spare
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-28 00:00:00.000000000 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: scope
17
+ requirement: &2165618400 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.2
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2165618400
26
+ description: DeepHash is a collection of utilities for dealing with multi-level hashes.
27
+ email:
28
+ - cespare@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - deep_hash.gemspec
38
+ - lib/deep_hash.rb
39
+ - lib/deep_hash/version.rb
40
+ - test/deep_hash_test.rb
41
+ has_rdoc: true
42
+ homepage: ''
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project: deep_hash
62
+ rubygems_version: 1.6.2
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Utilities for dealing with multi-level hashes
66
+ test_files:
67
+ - test/deep_hash_test.rb