deep_hash 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -11,6 +11,7 @@ hashes a lot. For example, multi-dimensional tables of counts (defaulting to 0)
11
11
 
12
12
  ```ruby
13
13
  require "deep_hash"
14
+
14
15
  h = Hash.deep(0) # is equivalent to
15
16
  h = {}
16
17
 
@@ -20,7 +21,7 @@ h = Hash.new { |k1, v1| k1[v1] = Hash.new { |k2, v2| k2[v2] = {} } }
20
21
  h = Hash.deep(0) { "foobar" } # is equivalent to
21
22
  h = Hash.new { |k, v| k[v] = "foobar" }
22
23
 
23
- h = Hash.deep(1) { 0 } is equivalent to
24
+ h = Hash.deep(1) { 0 } # is equivalent to
24
25
  h = Hash.new { |k1, v1| k1[v1] = Hash.new { |k2, v2| k2[v2] = 0 } }
25
26
  ```
26
27
 
data/lib/deep_hash.rb CHANGED
@@ -1,11 +1,15 @@
1
1
  require "deep_hash/version"
2
2
 
3
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 } : {}
4
+ def self.deep(depth = nil, &block)
5
+ raise "Need a number for depth" unless depth.nil? || depth.respond_to?(:to_i)
6
+ if depth.nil?
7
+ raise "Cannot use a block with nil depth." if block_given?
8
+ return Hash.new { |k, v| k[v] = Hash.deep(nil) }
8
9
  end
10
+ depth = depth.to_i
11
+ raise "Need non-negative depth" if depth < 0
12
+ return block_given? ? Hash.new { |k, v| k[v] = block.call } : {} if depth.zero?
9
13
  Hash.new { |k, v| k[v] = Hash.deep(depth - 1, &block) }
10
14
  end
11
15
  end
@@ -1,3 +1,3 @@
1
1
  module DeepHash
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -47,5 +47,19 @@ class DeepHashTest < Scope::TestCase
47
47
  assert_expected_hash({ :a => { 0 => 5, 1 => "foobar" }, :b => { 0 => "foobar" } }, hash)
48
48
  end
49
49
  end
50
+
51
+ context "with nil depth" do
52
+ should "raise an error if a block is given" do
53
+ assert_raises(RuntimeError) { Hash.deep {} }
54
+ end
55
+
56
+ should "create an arbitrary depth default hash" do
57
+ hash = Hash.deep
58
+ hash[0][0][0] = 1
59
+ hash[0][1] = 2
60
+ hash[1] = :bar
61
+ assert_expected_hash({ 0 => { 0 => { 0 => 1 }, 1 => 2 }, 1 => :bar }, hash)
62
+ end
63
+ end
50
64
  end
51
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-28 00:00:00.000000000 -07:00
12
+ date: 2011-07-07 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: scope
17
- requirement: &2165618400 !ruby/object:Gem::Requirement
17
+ requirement: &2153397060 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 0.2.2
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2165618400
25
+ version_requirements: *2153397060
26
26
  description: DeepHash is a collection of utilities for dealing with multi-level hashes.
27
27
  email:
28
28
  - cespare@gmail.com