Created
April 8, 2011 22:38
-
-
Save sentientmonkey/910872 to your computer and use it in GitHub Desktop.
lambda magic...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_xml(options = {}) | |
options[:indent] ||= 2 | |
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) | |
xml.instruct! unless options[:skip_instruct] | |
xml.deal do | |
xmlify = lambda do |key,value| | |
case value | |
when Array | |
xml.tag!(key.to_sym) do | |
value.map{|v| [key.to_s.singularize, v]}.each(&xmlify) | |
end | |
when Hash | |
xml.tag!(key.to_sym) do | |
value.each(&xmlify) | |
end | |
else | |
xml.tag!(key.to_sym, value) | |
end | |
end | |
public_attributes.each(&xmlify) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment