xml-fu 0.1.5 → 0.1.6
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/.travis-yml +8 -0
- data/README.md +9 -5
- data/Rakefile +13 -2
- data/lib/xml-fu.rb +4 -1
- data/lib/xml-fu/array.rb +3 -0
- data/lib/xml-fu/hash.rb +4 -2
- data/lib/xml-fu/version.rb +1 -1
- data/spec/lib/array_spec.rb +13 -0
- data/spec/lib/hash_spec.rb +8 -2
- data/spec/xmlfu_spec.rb +18 -9
- data/xml-fu.gemspec +1 -0
- metadata +19 -4
data/.travis-yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# XmlFu [](http://travis-ci.org/CITguy/xml-fu)
|
2
2
|
|
3
3
|
Convert Ruby Hashes to XML
|
4
4
|
|
@@ -243,7 +243,7 @@ XmlFu.xml(
|
|
243
243
|
Since with simple values, you cannot infer the value of their node container purely on their value, simple values
|
244
244
|
are currently ignored in arrays and only Hashes are translated.
|
245
245
|
|
246
|
-
```
|
246
|
+
```ruby
|
247
247
|
"foo" => [
|
248
248
|
{:bar => "biz"},
|
249
249
|
nil, # ignored
|
@@ -257,10 +257,14 @@ are currently ignored in arrays and only Hashes are translated.
|
|
257
257
|
#=> "<foo><bar>biz</bar></foo>"
|
258
258
|
```
|
259
259
|
|
260
|
+
## Options
|
261
|
+
* **:instruct** => true
|
262
|
+
* Adds <xml version="1.0" encoding="UTF-8"?> to generated XML
|
263
|
+
|
260
264
|
|
261
|
-
|
265
|
+
## Cheat Sheet
|
262
266
|
|
263
|
-
|
267
|
+
### Key
|
264
268
|
1. if key denotes self-closing node (key/)
|
265
269
|
* attributes are preserved with Hash values
|
266
270
|
* value and "=" values are ignored
|
@@ -273,7 +277,7 @@ are currently ignored in arrays and only Hashes are translated.
|
|
273
277
|
* Array is flattened
|
274
278
|
* Only Hash items in array are translated
|
275
279
|
|
276
|
-
|
280
|
+
### Value
|
277
281
|
1. if value is Hash:
|
278
282
|
* "@" keys are attributes of the node
|
279
283
|
* "=" key can be used in conjunction with any "@" keys to specify content of node
|
data/Rakefile
CHANGED
@@ -1,2 +1,13 @@
|
|
1
|
-
|
2
|
-
require "bundler/gem_tasks"
|
1
|
+
##!/usr/bin/env rake
|
2
|
+
#require "bundler/gem_tasks"
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require "rspec/core/rake_task"
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new do |t|
|
9
|
+
t.rspec_opts = %w(-c)
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :spec
|
13
|
+
task :test => :spec
|
data/lib/xml-fu.rb
CHANGED
@@ -24,7 +24,10 @@ module XmlFu
|
|
24
24
|
# an array of one hash (the root node hash)
|
25
25
|
#
|
26
26
|
# <foo><bar/><baz/></foo> => [{"foo" => [{"bar/" => ""},{"baz/" => ""}] }]
|
27
|
-
def parse(xml, options)
|
27
|
+
def parse(xml=nil, options={})
|
28
|
+
parsed_xml = xml
|
29
|
+
|
30
|
+
return Array(parsed_xml)
|
28
31
|
end
|
29
32
|
|
30
33
|
def configure
|
data/lib/xml-fu/array.rb
CHANGED
@@ -69,6 +69,9 @@ module XmlFu
|
|
69
69
|
# @param [Hash] opts Hash of options to pass to the iteration
|
70
70
|
def self.each_with_xml(arr, opts={})
|
71
71
|
xml = Builder::XmlMarkup.new
|
72
|
+
xml.instruct! if opts[:instruct] == true
|
73
|
+
opts.delete(:instruct)
|
74
|
+
|
72
75
|
|
73
76
|
arr.each do |item|
|
74
77
|
key = opts.fetch(:key, "")
|
data/lib/xml-fu/hash.rb
CHANGED
@@ -11,7 +11,7 @@ module XmlFu
|
|
11
11
|
|
12
12
|
# Convert Hash to XML String
|
13
13
|
def self.to_xml(hash, options={})
|
14
|
-
each_with_xml hash do |xml, name, value, attributes|
|
14
|
+
each_with_xml hash, options do |xml, name, value, attributes|
|
15
15
|
xml << Node.new(name, value, attributes).to_xml
|
16
16
|
end
|
17
17
|
end#self.to_xml
|
@@ -37,8 +37,10 @@ module XmlFu
|
|
37
37
|
|
38
38
|
# Provides a convenience function to iterate over the hash
|
39
39
|
# Logic will filter out attribute and content keys from hash values
|
40
|
-
def self.each_with_xml(hash)
|
40
|
+
def self.each_with_xml(hash, opts)
|
41
41
|
xml = Builder::XmlMarkup.new
|
42
|
+
xml.instruct! if opts[:instruct] == true
|
43
|
+
opts.delete(:instruct)
|
42
44
|
|
43
45
|
hash.each do |key,value|
|
44
46
|
node_value = value
|
data/lib/xml-fu/version.rb
CHANGED
data/spec/lib/array_spec.rb
CHANGED
@@ -27,6 +27,19 @@ describe XmlFu::Array do
|
|
27
27
|
# XmlFu.infer_simple_value_nodes = false
|
28
28
|
#end
|
29
29
|
|
30
|
+
it "should return plain xml with simple args" do
|
31
|
+
result = XmlFu::Array.to_xml([{:foo => "bar"}])
|
32
|
+
result.should == "<foo>bar</foo>"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should add XML doctype when :instruct => true" do
|
36
|
+
result = XmlFu::Array.to_xml([{:foo => "bar"}], :instruct => true)
|
37
|
+
result.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>"
|
38
|
+
|
39
|
+
result = XmlFu.xml([{:foo => "bar"}], :instruct => true)
|
40
|
+
result.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>"
|
41
|
+
end
|
42
|
+
|
30
43
|
it "should flatten nested arrays properly" do
|
31
44
|
hash = {
|
32
45
|
:foo => [
|
data/spec/lib/hash_spec.rb
CHANGED
@@ -3,8 +3,14 @@ require 'spec_helper'
|
|
3
3
|
describe XmlFu::Hash do
|
4
4
|
|
5
5
|
describe ".to_xml" do
|
6
|
-
it "
|
7
|
-
XmlFu::Hash.to_xml(:
|
6
|
+
it "should return plain xml with simple args" do
|
7
|
+
result = XmlFu::Hash.to_xml({:foo=>"bar"})
|
8
|
+
result.should == "<foo>bar</foo>"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should add XML doctype if :instruct => true" do
|
12
|
+
result = XmlFu::Hash.to_xml({:foo=>"bar"}, :instruct => true)
|
13
|
+
result.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>"
|
8
14
|
end
|
9
15
|
|
10
16
|
it "for a nested Hash" do
|
data/spec/xmlfu_spec.rb
CHANGED
@@ -29,11 +29,11 @@ describe XmlFu do
|
|
29
29
|
it "should return correct value based on nested array of hashes" do
|
30
30
|
hash = {
|
31
31
|
"SecretAgents" => [
|
32
|
-
{"agent/" => {"@
|
33
|
-
{"agent/" => {"@
|
32
|
+
{"agent/" => {"@name"=>"Alec Trevelyan"}},
|
33
|
+
{"agent/" => {"@name"=>"James Bond"}}
|
34
34
|
]
|
35
35
|
}
|
36
|
-
expected = "<SecretAgents><agent name=\"Alec Trevelyan\"
|
36
|
+
expected = "<SecretAgents><agent name=\"Alec Trevelyan\"/><agent name=\"James Bond\"/></SecretAgents>"
|
37
37
|
XmlFu.xml(hash).should == expected
|
38
38
|
end
|
39
39
|
|
@@ -77,13 +77,22 @@ describe XmlFu do
|
|
77
77
|
xf.should respond_to(:infer_simple_value_nodes)
|
78
78
|
end
|
79
79
|
end
|
80
|
+
|
81
|
+
it "should set XmlFu Module variable 'infer_simple_value_nodes'" do
|
82
|
+
XmlFu.infer_simple_value_nodes.should == false
|
83
|
+
XmlFu.infer_simple_value_nodes = true
|
84
|
+
XmlFu.infer_simple_value_nodes.should == true
|
85
|
+
XmlFu.infer_simple_value_nodes = false
|
86
|
+
XmlFu.infer_simple_value_nodes.should == false
|
87
|
+
end
|
80
88
|
end
|
81
89
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
90
|
+
describe ".parse" do
|
91
|
+
it "should always return an array" do
|
92
|
+
::Array === XmlFu.parse()
|
93
|
+
::Array === XmlFu.parse("foo")
|
94
|
+
::Array === XmlFu.parse("foo", {:bar => true})
|
95
|
+
end
|
88
96
|
end
|
97
|
+
|
89
98
|
end
|
data/xml-fu.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.add_dependency "builder", ">= 2.1.2"
|
20
20
|
|
21
21
|
gem.add_development_dependency "rspec", ">= 2.4.0"
|
22
|
+
gem.add_development_dependency "rake"
|
22
23
|
|
23
24
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
25
|
gem.files = `git ls-files`.split("\n")
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 6
|
10
|
+
version: 0.1.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Johnson
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-03-
|
18
|
+
date: 2012-03-21 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: builder
|
@@ -49,6 +49,20 @@ dependencies:
|
|
49
49
|
version: 2.4.0
|
50
50
|
type: :development
|
51
51
|
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: rake
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
52
66
|
description: "\n Inspired by the Gyoku gem for hash to xml conversion, \n XmlFu is designed to require no meta tagging for \n node attributes and content. (i.e. no :attributes! and no :order!)\n "
|
53
67
|
email:
|
54
68
|
- rhino.citguy@gmail.com
|
@@ -60,6 +74,7 @@ extra_rdoc_files: []
|
|
60
74
|
|
61
75
|
files:
|
62
76
|
- .gitignore
|
77
|
+
- .travis-yml
|
63
78
|
- CHANGELOG.md
|
64
79
|
- Gemfile
|
65
80
|
- LICENSE
|