xmlhasher 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +44 -9
- data/lib/xmlhasher/node.rb +1 -5
- data/lib/xmlhasher/version.rb +1 -1
- data/test/test_helper.rb +1 -0
- data/test/xmlhasher/parser_test.rb +6 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -1,23 +1,58 @@
|
|
1
1
|
# XmlHasher
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/cloocher/xmlhasher.png)](https://travis-ci.org/cloocher/xmlhasher)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/cloocher/xmlhasher/badge.png?branch=master)](https://coveralls.io/r/cloocher/xmlhasher)
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/xmlhasher.png)](http://badge.fury.io/rb/xmlhasher)
|
4
6
|
|
5
|
-
|
7
|
+
Fast XML to Ruby Hash converter
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
gem 'xmlhasher'
|
11
|
+
## Installation
|
12
12
|
|
13
|
-
|
13
|
+
Aggcat is available through [Rubygems](http://rubygems.org/gems/xmlhasher) and can be installed via:
|
14
14
|
|
15
|
-
|
15
|
+
```
|
16
|
+
$ gem install xmlhasher
|
17
|
+
```
|
16
18
|
|
17
|
-
|
19
|
+
or add it to your Gemfile like this:
|
18
20
|
|
19
|
-
|
21
|
+
```
|
22
|
+
gem 'xmlhasher'
|
23
|
+
```
|
20
24
|
|
21
25
|
## Usage
|
22
26
|
|
23
|
-
|
27
|
+
```ruby
|
28
|
+
require 'xmlhasher'
|
29
|
+
|
30
|
+
# XmlHasher global configuration
|
31
|
+
XmlHasher.configure do |config|
|
32
|
+
config.snakecase = true
|
33
|
+
config.ignore_namespaces = true
|
34
|
+
end
|
35
|
+
|
36
|
+
# alternatively, specify configuration options when instantiating a Parser
|
37
|
+
parser = XmlHasher::Parser.new(
|
38
|
+
:snakecase => true,
|
39
|
+
:ignore_namespaces => true
|
40
|
+
)
|
41
|
+
|
42
|
+
# parse XML file
|
43
|
+
XmlHasher.parse(File.new('/path/to/my/file.xml'))
|
44
|
+
|
45
|
+
# parse XML string
|
46
|
+
XmlHasher.parse("<tag1><tag2>content</tag2></tag1>")
|
47
|
+
# => {:tag1=>{:tag2=>"content"}}
|
48
|
+
|
49
|
+
```
|
50
|
+
## Requirements
|
51
|
+
|
52
|
+
* Ruby 1.8.7 or higher
|
53
|
+
|
54
|
+
## Copyright
|
55
|
+
Copyright (c) 2013 Gene Drabkin.
|
56
|
+
See [LICENSE][] for details.
|
57
|
+
|
58
|
+
[license]: LICENSE.md
|
data/lib/xmlhasher/node.rb
CHANGED
@@ -18,11 +18,7 @@ module XmlHasher
|
|
18
18
|
child = children.first
|
19
19
|
h[name].merge!(child.to_hash)
|
20
20
|
else
|
21
|
-
|
22
|
-
h[name].merge!(children.inject({}) { |r, child| (r[child.name] ||= []) << child.to_hash[child.name]; r })
|
23
|
-
else
|
24
|
-
h[name].merge!(children.inject({}) { |r, child| r.merge!(child.to_hash); r })
|
25
|
-
end
|
21
|
+
h[name].merge!(children.group_by { |child| child.name }.inject({}) { |r, (k, v)| v.length == 1 ? r.merge!(v.first.to_hash) : r[k] = v.map { |c| c.to_hash[c.name] }; r })
|
26
22
|
end
|
27
23
|
end
|
28
24
|
h[name] = nil if h[name].empty?
|
data/lib/xmlhasher/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -196,6 +196,12 @@ class XmlhasherTest < Test::Unit::TestCase
|
|
196
196
|
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
197
197
|
end
|
198
198
|
|
199
|
+
def test_array_detection_with_mixed_children
|
200
|
+
xml = %[<tag><tag2 attr='1' /><tag3>content</tag3><tag2 attr='2' /></tag>]
|
201
|
+
expected = {:tag => {:tag2 => [{:attr => '1'}, {:attr => '2'}], :tag3 => 'content'}}
|
202
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
203
|
+
end
|
204
|
+
|
199
205
|
def test_single_child
|
200
206
|
xml = %[<tag><tag2 attr='1' /></tag>]
|
201
207
|
expected = {:tag => {:tag2 => {:attr => '1'}}}
|