rsxml 0.1.3 → 0.1.4

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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/rsxml.rb +25 -8
  3. data/spec/rsxml_spec.rb +27 -1
  4. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/rsxml.rb CHANGED
@@ -4,15 +4,24 @@ require 'builder'
4
4
  module Rsxml
5
5
  module_function
6
6
 
7
+ def check_opts(constraints, opts)
8
+ (opts||{}).each do |k,v|
9
+ raise "opt not permitted: #{k}" if !constraints.has_key?(k)
10
+ constraint = constraints[k]
11
+ end
12
+ end
13
+
7
14
  # convert an Rsxml s-expression representation of an XML document to XML
8
15
  # Rsxml.to_xml(["Foo", {"foofoo"=>"10"}, ["Bar", "barbar"] ["Baz"]])
9
16
  # => '<Foo foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>'
10
- def to_xml(rsxml)
17
+ def to_xml(rsxml, &transformer)
11
18
  xml = Builder::XmlMarkup.new
12
- Sexp.write_xml(xml, rsxml)
19
+ Sexp.write_xml(xml, rsxml, &transformer)
13
20
  xml.target!
14
21
  end
15
22
 
23
+ TO_RSXML_OPTS = {:ns=>nil}
24
+
16
25
  # convert an XML string to an Rsxml s-expression representation
17
26
  # Rsxml.to_rsxml('<Foo foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>')
18
27
  # => ["Foo", {"foofoo"=>"10"}, ["Bar", "barbar"], ["Baz"]]
@@ -23,8 +32,9 @@ module Rsxml
23
32
  # fragment = '<foo:Foo foo:foofoo="10"><Bar>barbar</Bar><Baz></Baz></Foo>'
24
33
  # Rsxml.to_rsxml(fragment, {"foo"=>"http://foo.com/foo", ""=>"http://baz.com/baz"})
25
34
  # => ["foo:Foo", {"foo:foofoo"=>"10", "xmlns:foo"=>"http://foo.com/foo", "xmlns"=>"http://baz.com/baz"}, ["Bar", "barbar"], ["Baz"]]
26
- def to_rsxml(doc, ns_prefixes=nil)
27
- doc = Xml.wrap_fragment(doc, ns_prefixes)
35
+ def to_rsxml(doc, opts={})
36
+ check_opts(TO_RSXML_OPTS, opts)
37
+ doc = Xml.wrap_fragment(doc, opts[:ns])
28
38
  root = Xml.unwrap_fragment(Nokogiri::XML(doc).children.first)
29
39
  Xml.read_xml(root, [])
30
40
  end
@@ -40,13 +50,20 @@ module Rsxml
40
50
  module Sexp
41
51
  module_function
42
52
 
43
- def write_xml(xml, sexp)
53
+ def write_xml(xml, sexp, path="", &transformer)
44
54
  tag, attrs, children = decompose_sexp(sexp)
45
55
 
46
- xml.__send__(tag, attrs) do
47
- children.each do |child|
56
+ if transformer
57
+ txtag, txattrs = transformer.call(tag, attrs, path)
58
+ else
59
+ txtag, txattrs = [tag, attrs]
60
+ end
61
+
62
+ cp = [path, tag].join("/")
63
+ xml.__send__(txtag, txattrs) do
64
+ children.each_with_index do |child, i|
48
65
  if child.is_a?(Array)
49
- write_xml(xml, child)
66
+ write_xml(xml, child, "#{cp}[#{i}]", &transformer)
50
67
  else
51
68
  xml << child
52
69
  end
data/spec/rsxml_spec.rb CHANGED
@@ -67,6 +67,32 @@ describe Rsxml do
67
67
  r.attributes["baz"].namespace.href.should == "http://foo.com/foo"
68
68
  r.attributes["baz"].namespace.prefix.should == "foo"
69
69
  end
70
+
71
+ it "should transform a single tag if a transformer is supplied" do
72
+ xml = Rsxml.to_xml([:foo]) do |tag,attrs,path|
73
+ path.should == ""
74
+ [tag.upcase, attrs]
75
+ end.should ==
76
+ "<FOO></FOO>"
77
+ end
78
+
79
+ it "should transform nested tags if a transformer is supplied" do
80
+ txs = {"/foo"=>"Blub", "/foo[0]/bar"=>"Wub"}
81
+ xml = Rsxml.to_xml([:foo, [:bar]]) do |tag,attrs,path|
82
+ $stderr << [tag, attrs, path].inspect
83
+ [txs[[path, tag].join("/")], attrs]
84
+ end.should ==
85
+ "<Blub><Wub></Wub></Blub>"
86
+ end
87
+
88
+ it "should transform a tag with attributes if a transformer is supplied" do
89
+ xml = Rsxml.to_xml([:foo, {:bar=>"bar"}]) do |tag,attrs,path|
90
+ path.should == ""
91
+ [tag.upcase, Hash[*attrs.map{|k,v| [k.to_s.upcase,v]}.flatten]]
92
+ end.should ==
93
+ '<FOO BAR="bar"></FOO>'
94
+
95
+ end
70
96
  end
71
97
 
72
98
  describe "to_rsxml" do
@@ -105,7 +131,7 @@ describe Rsxml do
105
131
  xml = Rsxml.to_xml(["foo:foofoo", {"foo:bar"=>"1", "foo:baz"=>"baz"}])
106
132
 
107
133
  org_with_ns = ["foo:foofoo", {"foo:bar"=>"1", "foo:baz"=>"baz", "xmlns"=>"http://baz.com/baz", "xmlns:foo"=>"http://foo.com/foo"}]
108
- rsxml = Rsxml.to_rsxml(xml, {:foo=>"http://foo.com/foo", ""=>"http://baz.com/baz"})
134
+ rsxml = Rsxml.to_rsxml(xml, :ns=>{:foo=>"http://foo.com/foo", ""=>"http://baz.com/baz"})
109
135
 
110
136
  rsxml.should == org_with_ns
111
137
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsxml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Trampoline Systems Ltd
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-04 00:00:00 +01:00
18
+ date: 2011-05-05 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency