Closed Tab10id closed 2 years ago
Thanks for pointing that out.
That's an oversight on my side. Both should work the same and the second version's output is intended.
When you use xml
block it should override default mapping created by attribute
methods.
I'll fix that in the next version and also update the docs to be clear about this behavior.
Thanks. Do you have plans to implement regular way to fix original mapping without fully override? In my case I have a bunch of attributes and I just want to rename a root element. Without this feature I need to double lines of this class.
Full override is intended, because in most cases when you define your own mapping you don't need the default one.
But you always have access to the mapping object and can modify it however you want, so in your case something like this would work:
class Person < Shale::Mapper
attribute :name, Shale::Type::String
end
Person.xml_mapping.root('foo')
Person.new(name: 'John Doe').to_xml(:pretty)
# =>
#
# <foo>
# <name>John Doe</name>
# </foo>
Ok. I like it. Little fix:
class Person < Shale::Mapper
attribute :name, Shale::Type::String
xml_mapping.root('foo')
end
Person.new(name: 'John Doe').to_xml(:pretty)
# =>
#
# <foo>
# <name>John Doe</name>
# </foo>
There two mappers that by my mind should work identically:
After little research I recognized that xml method fully override config setted by
attribute
method calls. It's not critical but I think that it should be documented as minimum.