Skip to content

Instantly share code, notes, and snippets.

@sentientmonkey
Created April 8, 2011 22:38
Show Gist options
  • Save sentientmonkey/910872 to your computer and use it in GitHub Desktop.
Save sentientmonkey/910872 to your computer and use it in GitHub Desktop.
lambda magic...
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