Last active
October 11, 2019 09:48
-
-
Save reggieb/bf11e47baa15af3dc4d2f8fd5dbab0a7 to your computer and use it in GitHub Desktop.
XBuilder - A simple tool to build complicated XML objects from nested components
This file contains hidden or 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
require "active_support/core_ext" | |
class XBuilder | |
attr_reader :data, :namespace, :root | |
def initialize(data) | |
@namespace = data.delete(:_namespace) | |
@root = data.delete(:_root) | |
@data = data | |
end | |
def to_xml(options={}) | |
hash = data.transform_keys do |key| | |
key = key.to_s.camelize if key.is_a?(Symbol) | |
key.include?(':') ? key : [namespace,key].compact.join(':') | |
end | |
options.merge!(root: root) if root | |
hash.to_xml(options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of usage:
Produces: