Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit c91dd4687ab0c09a0fae30098786fb6fc779ae6d @cespare committed Jun 29, 2011
Showing with 96 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +1 −0 Rakefile
  4. +22 −0 deep_hash.gemspec
  5. +11 −0 lib/deep_hash.rb
  6. +3 −0 lib/deep_hash/version.rb
  7. +51 −0 test/deep_hash_test.rb
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in deep_hash.gemspec
+gemspec
@@ -0,0 +1 @@
+require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "deep_hash/version"
+
+Gem::Specification.new do |s|
+ s.name = "deep_hash"
+ s.version = DeepHash::VERSION
+ s.authors = ["Caleb Spare"]
+ s.email = ["cespare@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{Utilities for dealing with multi-level hashes}
+ s.description = %q{DeepHash is a collection of utilities for dealing with multi-level hashes.}
+
+ s.rubyforge_project = "deep_hash"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency "scope", "~> 0.2.2"
+end
@@ -0,0 +1,11 @@
+require "deep_hash/version"
+
+class Hash
+ def self.deep(depth, &block)
+ raise "Need non-negative depth" if depth < 0
+ if depth.zero?
+ return block_given? ? Hash.new { |k, v| k[v] = block.call } : {}
+ end
+ Hash.new { |k, v| k[v] = Hash.deep(depth - 1, &block) }
+ end
+end
@@ -0,0 +1,3 @@
+module DeepHash
+ VERSION = "0.0.1"
+end
@@ -0,0 +1,51 @@
+require "scope"
+require "minitest/autorun"
+
+require "deep_hash"
+
+class DeepHashTest < Scope::TestCase
+ def assert_expected_hash(expected, actual)
+ assert expected.is_a? Hash
+ assert actual.is_a? Hash
+ assert_equal expected.keys.sort, actual.keys.sort
+ expected.keys.each do |key|
+ if expected[key].is_a? Hash
+ assert_expected_hash expected[key], actual[key]
+ else
+ assert_equal expected[key], actual[key]
+ end
+ end
+ end
+
+ context "deep hash creation" do
+ context "with no block" do
+ should "create a plain hash with depth 0" do
+ assert_expected_hash({}, Hash.deep(0))
+ end
+
+ should "should create a properly nested hash of the specified depth" do
+ hash = Hash.deep(1)
+ hash[0][1] = 2
+ assert_equal({}, hash[1])
+ assert_expected_hash({ 0 => { 1 => 2 }, 1 => {} }, hash)
+ end
+ end
+
+ context "with a block given" do
+ should "create a normal default-valued hash with depth 0" do
+ hash = Hash.deep(0) { "foobar" }
+ hash[:a] = 3
+ assert_equal "foobar", hash[:b]
+ assert_expected_hash({ :a => 3, :b => "foobar" }, hash)
+ end
+
+ should "properly create a mult-level default hash" do
+ hash = Hash.deep(1) { "foobar" }
+ hash[:a][0] = 5
+ assert_equal "foobar", hash[:a][1]
+ assert_equal "foobar", hash[:b][0]
+ assert_expected_hash({ :a => { 0 => 5, 1 => "foobar" }, :b => { 0 => "foobar" } }, hash)
+ end
+ end
+ end
+end

0 comments on commit c91dd46

Please sign in to comment.