I've used Builder for several projects over the last few years and every time I make the same API mistake: I use #to_s to finalize the built document.
xml = Builder::XmlMarkup.new
xml.foo do
xml.bar
end
return xml.to_s
Only to find that I've injected an extra, unwanted <to_s/> element. A naive approach might try this:
class Builder::XmlMarkup
def to_s
@target.to_s
end
end
But I know you just require the :<< method, thus allowing direct IO, which can't convert to a String. Perhaps something like this:
class Builder::XmlMarkup
def to_s
raise ArgumentError, "Cannot coerce #{@target.class.name} to String" unless @target.is_a?(String)
@target
end
end
WDYT?
I've used Builder for several projects over the last few years and every time I make the same API mistake: I use #to_s to finalize the built document.
Only to find that I've injected an extra, unwanted
<to_s/>element. A naive approach might try this:But I know you just require the :<< method, thus allowing direct IO, which can't convert to a String. Perhaps something like this:
WDYT?