nested_hash 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,3 +1,7 @@
1
+ 2012-05-25 Guillermo Alvarez <guillermo@cientifico.net>
2
+
3
+ * nested_hash.rb (get/set): Implement get and set methods.
4
+
1
5
  2012-05-24 Guillermo Alvarez <guillermo@cientifico.net>
2
6
 
3
7
  * nested_hash.rb (process): [bug] When there is only one level, the key was
data/lib/nested_hash.rb CHANGED
@@ -3,31 +3,60 @@ require "nested_hash/version"
3
3
  class NestedHash < Hash
4
4
 
5
5
  def initialize(hash = {})
6
- hash.each do |key,v|
7
- key = sanitize_long_key(key)
8
-
9
- if is_valid_key?(key)
10
- process(key,v)
11
- elsif copy_invalid_keys?
12
- copy(key,v)
13
- end
6
+ hash.each do |key,value|
7
+ set(key,value)
14
8
  end
15
9
  post_process
16
10
  end
17
11
 
18
- protected
12
+ def set(key,value)
13
+ key = sanitize_long_key(key)
14
+ if is_valid_key?(key)
15
+ process(key,value)
16
+ elsif copy_invalid_keys?
17
+ copy(key,value)
18
+ end
19
+ end
19
20
 
20
- def post_process
21
- compact_arrays
21
+ def get(key)
22
+ key = sanitize_long_key(key)
22
23
  end
23
24
 
24
- def compact_arrays(element = self)
25
+ def compact_arrays!(element = self)
25
26
  element.each do |item|
26
27
  item.compact! if item.is_a?(Array)
27
- compact_arrays(item) if item.is_a?(Array) || item.is_a?(Hash)
28
+ compact_arrays!(item) if item.is_a?(Array) || item.is_a?(Hash)
28
29
  end
29
30
  end
30
31
 
32
+
33
+ def set(key,value, write = true)
34
+ keys = key.split(".").map{|k| sanitize_key(k)}
35
+
36
+ previous = keys.shift
37
+ top = (keys.inject(self) do |memo,key|
38
+ memo[previous] ||= ( is_for_array?(key) ? [] : {} )
39
+ memo = memo[previous]
40
+ previous = key
41
+ memo
42
+ end)
43
+
44
+ write ? top[previous] = value : top[previous]
45
+ rescue => e
46
+ handle_exception(e, key, value)
47
+ end
48
+
49
+ def get(key)
50
+ set(key,nil, false)
51
+ end
52
+
53
+ protected
54
+
55
+ def post_process
56
+ compact_arrays!
57
+ end
58
+
59
+
31
60
  def copy_invalid_keys?
32
61
  true
33
62
  end
@@ -37,7 +66,7 @@ class NestedHash < Hash
37
66
  end
38
67
 
39
68
  def sanitize_key(key)
40
- key
69
+ ( key =~ /^\d+$/ ) ? key.to_i : key
41
70
  end
42
71
 
43
72
  def is_valid_key?(key)
@@ -61,27 +90,6 @@ class NestedHash < Hash
61
90
  v
62
91
  end
63
92
 
64
- def process_nested(key, value)
65
- keys = key.split(".")
66
- previous = sanitize_key(keys.shift)
67
- top = keys.inject(self) do |memo,key|
68
- if is_for_array?(key)
69
- memo[previous] ||= []
70
- key = key.to_i
71
- else
72
- key = sanitize_key(key)
73
- memo[previous] ||= {}
74
- end
75
- a = memo[previous]
76
- previous = key
77
- a
78
- end
79
-
80
- top[previous] = value
81
- rescue => e
82
- handle_exception(e, key, value)
83
- end
84
-
85
93
  def handle_exception(e, key, value)
86
94
  log_exception(e, key, value) if log_exceptions?
87
95
  raise e unless continue_on_exceptions?
@@ -100,7 +108,7 @@ class NestedHash < Hash
100
108
  end
101
109
 
102
110
  def is_for_array?(key)
103
- key =~ /\A\d+\z/
111
+ key.is_a?(Numeric)
104
112
  end
105
113
 
106
114
 
@@ -1,3 +1,3 @@
1
1
  class NestedHash < Hash
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -36,4 +36,20 @@ class NestedHashTest < Test::Unit::TestCase
36
36
  assert_equal( {"name" => "Guillermo", "lives" => 3} , hash["game"][0], 'should merge elements')
37
37
  assert_equal( {"lives" => 5}, hash["game"][1])
38
38
  end
39
+
40
+
41
+ def test_set_and_get
42
+ nh = NestedHash.new()
43
+ nh.set("person.2.name", "guillermo")
44
+
45
+ assert_equal({"person"=>[nil, nil, {"name"=>"guillermo"}]}, nh)
46
+ end
47
+
48
+ def test_compact_array
49
+ nh = NestedHash.new()
50
+ nh.set("person.2.name", "guillermo")
51
+
52
+ nh.compact_arrays!
53
+ assert_equal({"person"=>[{"name"=>"guillermo"}]}, nh)
54
+ end
39
55
  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: 19
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Guillermo A\xCC\x81lvarez"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-24 00:00:00 +00:00
18
+ date: 2012-05-25 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies: []
21
21