Skip to content

Instantly share code, notes, and snippets.

@barmintor
Created September 7, 2011 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barmintor/1201986 to your computer and use it in GitHub Desktop.
Save barmintor/1201986 to your computer and use it in GitHub Desktop.
Working around the presence of method names on Nokogiri::XML::Builder that may also be in OM templates
def xml_builder_template(extra_opts = {})
extra_attributes = extra_opts.fetch(:attributes, {})
node_options = []
node_child_template = ""
if !self.default_content_path.nil?
node_child_options = ["\':::builder_new_value:::\'"]
node_child_template = " { xml.#{self.default_content_path}( #{OM::XML.delimited_list(node_child_options)} ) }"
else
node_options = ["\':::builder_new_value:::\'"]
end
if !self.attributes.nil?
self.attributes.merge(extra_attributes).each_pair do |k,v|
node_options << "\'#{k}\'=>\'#{v}\'"
end
end
b_ref = "xml"
b_method = self.path
if self.path.include?(":")
ns_prefix = self.path[0..path.index(":")-1]
b_method = self.path[path.index(":")+1..-1]
b_ref = "xml['#{ns_prefix}']"
elsif !self.namespace_prefix.nil? and self.namespace_prefix != 'oxns'
b_ref = "xml['#{self.namespace_prefix}']"
end
if Nokogiri::XML::Builder.method_defined? b_method.to_sym
b_method += '_'
end
template = "#{b_ref}.#{b_method}( #{OM::XML.delimited_list(node_options)} )" + node_child_template
return template.gsub( /:::(.*?):::/ ) { '#{'+$1+'}' }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment