model_xml 1.0.2 → 1.0.3

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.
@@ -72,7 +72,7 @@ module ModelXML
72
72
 
73
73
  field_list = generate_field_list(options)
74
74
 
75
- xml = Builder::XmlMarkup.new
75
+ xml = options.delete(:builder) || Builder::XmlMarkup.new(:indent => 2)
76
76
  unless options[:skip_instruct]
77
77
  xml.instruct!
78
78
  end
@@ -96,9 +96,9 @@ module ModelXML
96
96
  raise "ModelXML unable to parse #{field.inspect}"
97
97
  end
98
98
 
99
- # if the content implements a to_xml method which returns a non-nil value, insert as raw xml
100
- if content.respond_to?(:to_xml) && inline_content = content.to_xml(:skip_instruct => true, :skip_types => true)
101
- xml << inline_content
99
+ # if the content responds to to_xml, call it passing the current builder
100
+ if content.respond_to?(:to_xml)
101
+ content.to_xml(options.merge(:builder => xml, :skip_instruct => true))
102
102
 
103
103
  # otherwise create the tag normally
104
104
  else
@@ -107,10 +107,7 @@ module ModelXML
107
107
  end
108
108
  end
109
109
 
110
- # builder will screw up the indentation of embedded xml objects, so use nokogiri to format it nicely
111
- output = xml.target!.gsub("\n","").gsub(" ","")
112
- xml = Nokogiri.parse(output)
113
- options[:skip_instruct] ? xml.root.to_s : xml.to_s
110
+ xml.target!
114
111
 
115
112
  end
116
113
  end
data/model_xml.gemspec CHANGED
@@ -1,13 +1,12 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "model_xml"
3
- s.version = '1.0.2'
3
+ s.version = '1.0.3'
4
4
  s.authors = "Rob Anderson"
5
5
  s.email = "rob.anderson@paymentcardsolutions.co.uk"
6
6
  s.summary = "Ruby object to xml converter"
7
7
  s.description = "Simple replacement for ActiveRecord's default to_xml"
8
8
 
9
9
  s.add_dependency 'builder', '>= 2.1.2'
10
- s.add_dependency 'nokogiri', '>= 1.4.2'
11
10
 
12
11
  s.files = `git ls-files`.split("\n")
13
12
  s.require_path = "lib"
@@ -7,6 +7,16 @@ class TestStruct < OpenStruct
7
7
  include ModelXML
8
8
  end
9
9
 
10
+ class Parent < OpenStruct
11
+ include ModelXML
12
+ model_xml :foo, :child
13
+ end
14
+
15
+ class Child < OpenStruct
16
+ include ModelXML
17
+ model_xml :bar
18
+ end
19
+
10
20
  class ModelXMLTest < Test::Unit::TestCase
11
21
 
12
22
  def setup
@@ -78,8 +88,28 @@ class ModelXMLTest < Test::Unit::TestCase
78
88
  res = '<teststruct>
79
89
  <foo>1</foo>
80
90
  <bar>2</bar>
81
- </teststruct>'
91
+ </teststruct>
92
+ '
82
93
  assert_equal res, @t.to_xml(:skip_instruct => true)
83
94
 
84
95
  end
96
+
97
+ def test_embedded_xml
98
+ p = Parent.new(:foo => 1, :child => Child.new(:bar => 2))
99
+
100
+ res = '<?xml version="1.0" encoding="UTF-8"?>
101
+ <parent>
102
+ <foo>1</foo>
103
+ <child>
104
+ <bar>2</bar>
105
+ </child>
106
+ </parent>
107
+ '
108
+ assert_equal res, p.to_xml
109
+ end
110
+
111
+
112
+
113
+
114
+
85
115
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_xml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 2
10
- version: 1.0.2
9
+ - 3
10
+ version: 1.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rob Anderson
@@ -33,22 +33,6 @@ dependencies:
33
33
  version: 2.1.2
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: nokogiri
38
- prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 1
47
- - 4
48
- - 2
49
- version: 1.4.2
50
- type: :runtime
51
- version_requirements: *id002
52
36
  description: Simple replacement for ActiveRecord's default to_xml
53
37
  email: rob.anderson@paymentcardsolutions.co.uk
54
38
  executables: []