XML representation omits XML declaration
After searching representable and roar, I can find no option to include the XML declaration line in the representation. Is there a way to accomplish this or is it an oversight?
We left that out by intention! However, I agree with you that this might be handy. So you want the top representer to prepend that to the rendered XML doc?
I believe that is what I want. I have not been using roar long enough to completely understand its internals yet, so I would hate to request the wrong thing. This may actually belong in representable.
Seems that when #to_xml is called on a model extended by a representer, the XML declaration line should be there. I myself cannot think of a use case where you may need to turn that off with an option, but there may be some users who desire this?
I would also make use of this feature if added. Happy to investigate adding it myself if you can point me in the right direction, but I won't have too much free time over the next week.
Maybe we should have the following API?
representer.to_xml(declaration: true)
That would go into Representable::XML#to_xml, @ShaneWilton!
That looks easy to use, but how would it handle specifying the version, encoding, and standalone attributes? Of the three, only version is required, so something like this might be more flexible:
representer.to_xml # No XML declaration
representer.to_xml(declaration: nil) # No XML declaration
representer.to_xml(
declaration: {version: "1.0"}
) # <?xml version="1.0" ?>
representer.to_xml(
declaration: {
version: "1.0",
encoding: "UTF-8",
standalone: true
}
) # <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
representer.to_xml(
declaration: {
encoding: "UTF-8",
standalone: true
}
) # ArgumentError: Missing required :version argument
Up to you, @ShaneWilton! I haven't used XML in ages and will probably never use it, again. I'm actually thinking of introducing representable-xml as a separate, community-managed gem.
I haven't used XML in ages and will probably never use it, again.
I'm jealous :stuck_out_tongue:
Cool, I'll see what I can do about building that. It might be a bit because I'm a little swamped right now. I'll let you know if I have questions about the codebase.