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 CHANGED
@@ -1,6 +1,7 @@
1
1
  2012-05-25 Guillermo Alvarez <guillermo@cientifico.net>
2
2
 
3
3
  * nested_hash.rb (get/set): Implement get and set methods.
4
+ * nested_hash.rb (delete): Added delete method.
4
5
 
5
6
  2012-05-24 Guillermo Alvarez <guillermo@cientifico.net>
6
7
 
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, write = true)
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
- write ? top[previous] = value : top[previous]
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, false)
58
+ look_for(key,nil, :get)
48
59
  end
49
60
 
50
61
  protected
@@ -1,3 +1,3 @@
1
1
  class NestedHash < Hash
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Guillermo A\xCC\x81lvarez"