model_xml 1.0.5 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,6 +29,8 @@ Then user.to_xml gives:
29
29
  <dob>1970-12-23</dob>
30
30
  </user>
31
31
 
32
+ Just like ActiveRecord's to_xml method, the :only, :except, and :skip_instruct options are supported.
33
+
32
34
  Note that (unlike ActiveRecords's to_xml) the field names can be any method in your object, not just database columns - eg
33
35
 
34
36
  class User < ActiveRecord::Base
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'builder'
3
3
  require 'nokogiri'
4
+ require 'facets/string/snakecase'
4
5
  require 'model_xml/block_parser'
5
6
 
6
7
  module ModelXML
@@ -73,11 +74,14 @@ module ModelXML
73
74
  field_list = generate_field_list(options)
74
75
 
75
76
  xml = options.delete(:builder) || Builder::XmlMarkup.new(:indent => 2)
77
+ exceptions_list = options.delete(:except) || []
78
+ limit_list = options.delete(:only)
79
+
76
80
  unless options[:skip_instruct]
77
81
  xml.instruct!
78
82
  end
79
83
 
80
- xml.tag! object.class.to_s.downcase do
84
+ xml.tag! object.class.to_s.snakecase do
81
85
 
82
86
  field_list.each do |field|
83
87
 
@@ -96,13 +100,19 @@ module ModelXML
96
100
  raise "ModelXML unable to parse #{field.inspect}"
97
101
  end
98
102
 
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, :root => tag.to_s))
102
-
103
- # otherwise create the tag normally
103
+ # ignore the tag if it is on the exclude list, or if a limit list is provided and it is not on it
104
+ if exceptions_list.include?(tag) || (limit_list && !limit_list.include?(tag))
105
+ # do nothing
104
106
  else
105
- xml.tag! tag, content
107
+
108
+ # if the content responds to to_xml, call it passing the current builder
109
+ if content.respond_to?(:to_xml)
110
+ content.to_xml(options.merge(:builder => xml, :skip_instruct => true, :root => tag.to_s))
111
+
112
+ # otherwise create the tag normally
113
+ else
114
+ xml.tag! tag, content
115
+ end
106
116
  end
107
117
  end
108
118
  end
data/model_xml.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "model_xml"
3
- s.version = '1.0.5'
3
+ s.version = '1.0.9'
4
4
  s.authors = "Rob Anderson"
5
5
  s.email = "rob.anderson@paymentcardsolutions.co.uk"
6
6
  s.summary = "Ruby object to xml converter"
@@ -8,6 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.homepage = "https://github.com/rob-anderson/model_xml"
9
9
 
10
10
  s.add_dependency 'builder', '>= 2.1.2'
11
+ s.add_dependency 'facets', '>= 2.9.3'
11
12
 
12
13
  s.files = `git ls-files`.split("\n")
13
14
  s.require_path = "lib"
@@ -32,9 +32,9 @@ class ModelXMLTest < Test::Unit::TestCase
32
32
  assert_equal [[:foo]], TestStruct.model_xml_generator.field_sets
33
33
 
34
34
  res = '<?xml version="1.0" encoding="UTF-8"?>
35
- <teststruct>
35
+ <test_struct>
36
36
  <foo>1</foo>
37
- </teststruct>
37
+ </test_struct>
38
38
  '
39
39
  assert_equal res, @t.to_xml
40
40
  end
@@ -51,10 +51,10 @@ class ModelXMLTest < Test::Unit::TestCase
51
51
  assert_equal [[:foo, :bar]], TestStruct.model_xml_generator.field_sets
52
52
 
53
53
  res = '<?xml version="1.0" encoding="UTF-8"?>
54
- <teststruct>
54
+ <test_struct>
55
55
  <foo>1</foo>
56
56
  <bar>2</bar>
57
- </teststruct>
57
+ </test_struct>
58
58
  '
59
59
  assert_equal res, @t.to_xml
60
60
  end
@@ -70,11 +70,11 @@ class ModelXMLTest < Test::Unit::TestCase
70
70
  end
71
71
 
72
72
  res = '<?xml version="1.0" encoding="UTF-8"?>
73
- <teststruct>
73
+ <test_struct>
74
74
  <foo>1</foo>
75
75
  <bar>2</bar>
76
76
  <foobar>3</foobar>
77
- </teststruct>
77
+ </test_struct>
78
78
  '
79
79
  assert_equal res, @t.to_xml
80
80
  end
@@ -85,10 +85,10 @@ class ModelXMLTest < Test::Unit::TestCase
85
85
  model_xml :foo, :bar
86
86
  end
87
87
 
88
- res = '<teststruct>
88
+ res = '<test_struct>
89
89
  <foo>1</foo>
90
90
  <bar>2</bar>
91
- </teststruct>
91
+ </test_struct>
92
92
  '
93
93
  assert_equal res, @t.to_xml(:skip_instruct => true)
94
94
 
@@ -117,13 +117,28 @@ class ModelXMLTest < Test::Unit::TestCase
117
117
  end
118
118
 
119
119
  res = '<?xml version="1.0" encoding="UTF-8"?>
120
- <teststruct>
120
+ <test_struct>
121
121
  <id>foo</id>
122
- </teststruct>
122
+ </test_struct>
123
123
  '
124
124
  assert_equal res, @t.to_xml
125
125
  end
126
126
 
127
+ def test_except_and_include_options
128
+ TestStruct.instance_eval do
129
+ model_xml_reset!
130
+ model_xml :foo, :bar
131
+ end
132
+
133
+ res = '<?xml version="1.0" encoding="UTF-8"?>
134
+ <test_struct>
135
+ <foo>1</foo>
136
+ </test_struct>
137
+ '
138
+ assert_equal res, @t.to_xml(:except => [:bar])
139
+ assert_equal res, @t.to_xml(:only => [:foo])
140
+
141
+ end
127
142
 
128
143
 
129
144
 
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: 29
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 5
10
- version: 1.0.5
9
+ - 9
10
+ version: 1.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rob Anderson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-11 00:00:00 Z
18
+ date: 2012-04-18 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: builder
@@ -33,6 +33,22 @@ dependencies:
33
33
  version: 2.1.2
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: facets
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 45
45
+ segments:
46
+ - 2
47
+ - 9
48
+ - 3
49
+ version: 2.9.3
50
+ type: :runtime
51
+ version_requirements: *id002
36
52
  description: Simple replacement for ActiveRecord's default to_xml
37
53
  email: rob.anderson@paymentcardsolutions.co.uk
38
54
  executables: []