lib/model_xml/generator.rb in model_xml-1.0.5 vs lib/model_xml/generator.rb in model_xml-1.0.9
- old
+ new
@@ -1,8 +1,9 @@
require 'rubygems'
require 'builder'
require 'nokogiri'
+require 'facets/string/snakecase'
require 'model_xml/block_parser'
module ModelXML
class Generator
@@ -71,15 +72,18 @@
def generate_xml! object, options={}
field_list = generate_field_list(options)
xml = options.delete(:builder) || Builder::XmlMarkup.new(:indent => 2)
+ exceptions_list = options.delete(:except) || []
+ limit_list = options.delete(:only)
+
unless options[:skip_instruct]
xml.instruct!
end
- xml.tag! object.class.to_s.downcase do
+ xml.tag! object.class.to_s.snakecase do
field_list.each do |field|
# if the field is a symbol, treat it as the field name and the getter method
if field.is_a?(Symbol)
@@ -94,16 +98,22 @@
# otherwise we have garbage
else
raise "ModelXML unable to parse #{field.inspect}"
end
- # if the content responds to to_xml, call it passing the current builder
- if content.respond_to?(:to_xml)
- content.to_xml(options.merge(:builder => xml, :skip_instruct => true, :root => tag.to_s))
-
- # otherwise create the tag normally
+ # ignore the tag if it is on the exclude list, or if a limit list is provided and it is not on it
+ if exceptions_list.include?(tag) || (limit_list && !limit_list.include?(tag))
+ # do nothing
else
- xml.tag! tag, content
+
+ # if the content responds to to_xml, call it passing the current builder
+ if content.respond_to?(:to_xml)
+ content.to_xml(options.merge(:builder => xml, :skip_instruct => true, :root => tag.to_s))
+
+ # otherwise create the tag normally
+ else
+ xml.tag! tag, content
+ end
end
end
end
xml.target!