nested_hash 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +1 -0
- data/lib/nested_hash.rb +14 -3
- data/lib/nested_hash/version.rb +1 -1
- data/test/test_simple_to_complex_hash.rb +9 -0
- metadata +3 -3
data/Changelog
CHANGED
data/lib/nested_hash.rb
CHANGED
@@ -26,8 +26,12 @@ class NestedHash < Hash
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
def delete(key)
|
30
|
+
look_for(key,nil, :delete)
|
31
|
+
end
|
32
|
+
|
29
33
|
|
30
|
-
def look_for(key,value,
|
34
|
+
def look_for(key,value, op = :set)
|
31
35
|
keys = key.split(".").map{|k| sanitize_key(k)}
|
32
36
|
|
33
37
|
previous = keys.shift
|
@@ -38,13 +42,20 @@ class NestedHash < Hash
|
|
38
42
|
memo
|
39
43
|
end)
|
40
44
|
|
41
|
-
|
45
|
+
case op
|
46
|
+
when :set
|
47
|
+
top[previous] = value
|
48
|
+
when :get
|
49
|
+
top[previous]
|
50
|
+
when :delete
|
51
|
+
top.delete(previous)
|
52
|
+
end
|
42
53
|
rescue => e
|
43
54
|
handle_exception(e, key, value)
|
44
55
|
end
|
45
56
|
|
46
57
|
def get(key)
|
47
|
-
look_for(key,nil,
|
58
|
+
look_for(key,nil, :get)
|
48
59
|
end
|
49
60
|
|
50
61
|
protected
|
data/lib/nested_hash/version.rb
CHANGED
@@ -52,4 +52,13 @@ class NestedHashTest < Test::Unit::TestCase
|
|
52
52
|
nh.compact_arrays!
|
53
53
|
assert_equal({"person"=>[{"name"=>"guillermo"}]}, nh)
|
54
54
|
end
|
55
|
+
|
56
|
+
def test_delete_method
|
57
|
+
nh = NestedHash.new()
|
58
|
+
nh.set("person.2.name", "guillermo")
|
59
|
+
nh.delete("person.2.name")
|
60
|
+
assert_equal({"person"=>[nil,nil,{}]}, nh)
|
61
|
+
nh.compact_arrays!
|
62
|
+
assert_equal({"person"=>[{}]}, nh)
|
63
|
+
end
|
55
64
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Guillermo A\xCC\x81lvarez"
|