In an attempt to product client definable API returns, Dean Radcliffe, Jake Scruggs and I decided that using the local files would be a good idea. I mentioned this in a previous post. It is a great idea, and the execution is pretty sweet as well.
I am currently fighting with a way to do the sub level arrays such as
Document has many Document tags
So a layout like
<doc>
<tags>
<tag>Tag 1</tag>
<tag>Tag 2 </tag>
<t/ags>
</doc>
where Tags are objects themselves
I got a solution working but it is course and not using Builder XML.
It uses <<--DOCXML DOCXML tags and internally hand hacked xml arguments.
Nasty...
def to_xmls
xml_class_fetch = self.class.to_s.downcase
buff = <<-EOF
<#{xml_class_fetch}>
#{ClientConfig.get("xml_export.#{xml_class_fetch}").map do | xml_field_name, method_call |
case method_call
when Symbol
val = self.send(method_call)
when String
val = self.instance_eval(method_call)
end
if val.is_a?(Array)
val = val.map do |element|
if element.respond_to?(:name)
element_call = element.name
elsif element.respond_to?(:identifier)
element_call = element.identifier
end
"<#{xml_field_name.singularize}>#{element_call}</#{xml_field_name.singularize}>"
end
end
"<#{xml_field_name}>#{val}</#{xml_field_name}>"
end.join("\n")}
</#{xml_class_fetch}>
EOF
end
where the feed looks like
xml_export:
document:
# TODO - may add XML element names etc ...
- ["other-id", :other_id]
- ['created-at', :created_at]
- ['tags', "tags.to_a"]
As one can imagine, I am not happy with this solution at all.
If any one has any better suggestions to return valid XML with nested elements that are part of the model's has_and_belongs_to_many as well as other types of associations, please feel free to shoot me a tweet or comment.
No comments:
Post a Comment