Ticket #87 (new enhancement)
Store attributes as instances; defer to_s calls until serialization
| Reported by: | Phrogz | Owned by: | ser |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.2.0 |
| Component: | DOM | Version: | 3.1.5 |
| Severity: | minor | Keywords: | tostring delayed deferred delay |
| Cc: | gavin.kistner@… | Ruby version: | 1.8.4 |
| Operating system: | All |
Description
I want to be able to assign object instances as attribute values of an Element and have the to_s method of the instance called and used each time the Element is serialized, NOT when the value is assigned.
Theoretically, this would make document construction faster, but serialization a little slower. I *think* memory usage would go down. In any case, the benefits of this feature are not speed or memory changes, but rather the ability to use complex, mutable objects as attributes.
I would be fine if this was an option included at document creation, instead of the default behavior.
Following is a test case showcasing how I'd like it to behave (assuming default behavior instead of a per-document or per-element setting):
require 'test/unit' require 'rexml/document'
class Foo
attr_accessor :bar def to_s; bar.to_s; end
end
class MyTest? < Test::Unit::TestCase?
def test_deferred_tostring
d = REXML::Document.new d << REXML::Element.new( 'root' ) f = Foo.new f.bar = 'old' d.root.attributes[ 'foo' ] = f f.bar = 'new' output = "" d.write( output ) assert_equal( output, "<root foo='new'/>" )
end
end
