nested_hash 0.0.1 → 0.1.0
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/CHANGELOG +13 -0
- data/README.md +33 -8
- data/lib/nested_hash.rb +32 -0
- data/lib/nested_hash/version.rb +1 -1
- data/test/test_simple_to_complex_hash.rb +9 -4
- metadata +3 -2
data/CHANGELOG
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
2012-05-09 Guillermo Alvarez Fernandez <guillermo@cientifico.net>
|
3
|
+
|
4
|
+
* nested_hash.rb (process_nested): Added the ability of have hashs inside of
|
5
|
+
arrays.
|
6
|
+
|
7
|
+
* nested_hash.rb (post_process): Added the ability of post_process the final
|
8
|
+
hash.
|
9
|
+
|
10
|
+
* nested_hash.rb (log_exception): Create a exception handler, being able to
|
11
|
+
generate errors on stderr and continue with more keys.
|
12
|
+
|
13
|
+
|
data/README.md
CHANGED
@@ -1,16 +1,28 @@
|
|
1
1
|
# NestedHash
|
2
2
|
|
3
|
-
|
4
3
|
NestedHash is a Hash converter. It will created a simple, key encoded hash, in to a nested hash. For example, the hash:
|
5
4
|
|
6
5
|
```ruby
|
7
|
-
{
|
6
|
+
{
|
7
|
+
"name" => "guillermo",
|
8
|
+
"properties.age" => 29,
|
9
|
+
"properties.sex" => "male",
|
10
|
+
"parents.1" => "ramon",
|
11
|
+
"parents.2" => "gloria"
|
12
|
+
}
|
8
13
|
```
|
9
14
|
|
10
|
-
into the hash
|
15
|
+
will be converted into the hash
|
11
16
|
|
12
17
|
```ruby
|
13
|
-
{
|
18
|
+
{
|
19
|
+
"name" => "guillermo",
|
20
|
+
"properties" => {
|
21
|
+
"age" => 29,
|
22
|
+
"sex" => "male"
|
23
|
+
},
|
24
|
+
"parents" => [ "ramon", "gloria" ]
|
25
|
+
}
|
14
26
|
```
|
15
27
|
|
16
28
|
# Usage
|
@@ -18,18 +30,31 @@ into the hash
|
|
18
30
|
```ruby
|
19
31
|
require 'nested_hash'
|
20
32
|
|
21
|
-
my_normal_hash = {
|
33
|
+
my_normal_hash = {
|
34
|
+
"name" => "guillermo",
|
35
|
+
"properties.age" => 29,
|
36
|
+
"properties.sex" => "male",
|
37
|
+
"parents.1" => "ramon",
|
38
|
+
"parents.2" => "gloria"
|
39
|
+
}
|
22
40
|
|
23
41
|
my_new_hash = NestedHash.new(my_normal_hash)
|
42
|
+
my_new_hash # => {"name"=>"guillermo", "properties"=>{"age"=>29, "sex"=>"male"}, "parents"=>["ramon", "gloria"]}
|
43
|
+
```
|
44
|
+
|
45
|
+
NestedHash inherits from *Hash*, so you can use it as a normal hash.
|
46
|
+
|
24
47
|
|
48
|
+
# Installation
|
49
|
+
|
50
|
+
```shell
|
51
|
+
$ gem install nested_hash
|
52
|
+
```
|
25
53
|
|
26
54
|
# Motivations
|
27
55
|
|
28
56
|
The reason for creating this ruby gem is to convert Excel files to json files. With the rubygems roo you already can get the rows into a one level hash (colum title as a key). This complement help to create more difficult structures with the same excel file.
|
29
57
|
|
30
|
-
# Todo
|
31
|
-
|
32
|
-
* Add support for properties in arrays
|
33
58
|
|
34
59
|
# License
|
35
60
|
|
data/lib/nested_hash.rb
CHANGED
@@ -12,10 +12,22 @@ class NestedHash < Hash
|
|
12
12
|
copy(key,v)
|
13
13
|
end
|
14
14
|
end
|
15
|
+
post_process
|
15
16
|
end
|
16
17
|
|
17
18
|
protected
|
18
19
|
|
20
|
+
def post_process
|
21
|
+
compact_arrays
|
22
|
+
end
|
23
|
+
|
24
|
+
def compact_arrays(element = self)
|
25
|
+
element.each do |item|
|
26
|
+
item.compact! if item.is_a?(Array)
|
27
|
+
compact_arrays(item) if item.is_a?(Array) || item.is_a?(Hash)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
19
31
|
def copy_invalid_keys?
|
20
32
|
true
|
21
33
|
end
|
@@ -51,6 +63,7 @@ class NestedHash < Hash
|
|
51
63
|
top = keys.inject(self) do |memo,key|
|
52
64
|
if is_for_array?(key)
|
53
65
|
memo[previous] ||= []
|
66
|
+
key = key.to_i
|
54
67
|
else
|
55
68
|
memo[previous] ||= {}
|
56
69
|
end
|
@@ -64,6 +77,25 @@ class NestedHash < Hash
|
|
64
77
|
else
|
65
78
|
top[previous] = value
|
66
79
|
end
|
80
|
+
rescue => e
|
81
|
+
handle_exception(e, key, value)
|
82
|
+
end
|
83
|
+
|
84
|
+
def handle_exception(e, key, value)
|
85
|
+
log_exception(e, key, value) if log_exceptions?
|
86
|
+
raise e unless continue_on_exceptions?
|
87
|
+
end
|
88
|
+
|
89
|
+
def log_exception(e)
|
90
|
+
$stderr.puts "Problem procesing the pair \"#{key}\" => \"#{value}\""
|
91
|
+
end
|
92
|
+
|
93
|
+
def continue_on_exceptions?
|
94
|
+
true
|
95
|
+
end
|
96
|
+
|
97
|
+
def log_exceptions?
|
98
|
+
true
|
67
99
|
end
|
68
100
|
|
69
101
|
def is_for_array?(key)
|
data/lib/nested_hash/version.rb
CHANGED
@@ -8,10 +8,6 @@ class NestedHashTest < Test::Unit::TestCase
|
|
8
8
|
nested_hash.is_a?(Hash)
|
9
9
|
end
|
10
10
|
|
11
|
-
def test_true
|
12
|
-
assert true
|
13
|
-
end
|
14
|
-
|
15
11
|
def test_subitem_conversion
|
16
12
|
initial = { "properties.age" => 3 }
|
17
13
|
final = { "properties" => { "age" => 3 }}
|
@@ -31,4 +27,13 @@ class NestedHashTest < Test::Unit::TestCase
|
|
31
27
|
assert_equal ['child'], hash.keys
|
32
28
|
assert_equal ['pepe','juan'], hash["child"]
|
33
29
|
end
|
30
|
+
|
31
|
+
def test_hash_in_array
|
32
|
+
initial = { "game.1.name" => 'Guillermo',
|
33
|
+
"game.1.lives" => 3,
|
34
|
+
"game.2.lives" => 5 }
|
35
|
+
hash = NestedHash.new(initial)
|
36
|
+
assert_equal( {"name" => "Guillermo", "lives" => 3} , hash["game"][0], 'should merge elements')
|
37
|
+
assert_equal( {"lives" => 5}, hash["game"][1])
|
38
|
+
end
|
34
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-09 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'For example a hash like { "properties.age" => 3 } will be converted
|
15
15
|
into { "properties" => {"age" => 3}} '
|
@@ -20,6 +20,7 @@ extensions: []
|
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
22
|
- .gitignore
|
23
|
+
- CHANGELOG
|
23
24
|
- Gemfile
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|