-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wrap doesn't work for custom getter #90
Comments
I remember I merged the wrapper code a long while back without giving it much thought as to how it may cause some confusion in the usage of creating mappings. When you use the Right now, you cannot define a method with the same name as the mapping specified in the wrapper. It essentially defines the method on the current class. But there is this anonymous inner class that has the original method and when it comes time to persist the anonymous inner class uses its method NOT the 'parent' class. So I would definitely call this a bug with the To solve it, the anonymous inner class would probably have to have a reference to the parent class where it was created. Know that it is one of these anonymous wrapper classes, and then make a call up to the parent class to see if there is a method when it comes to persist to xml. |
It's hard for me to try and guess your real implementation. But you may be able to get away with encapsulating this XML representation / output class and the database methods in another class. require 'happymapper'
module Xml
class Root
include HappyMapper
tag 'Working'
element :name, String
wrap 'mywraptag' do
element :description, String
element :number, Integer
end
element :code, String
end
end
class Root
def initialize
@xml_root_object = Xml::Root.new
end
def name
@xml_root_object.name
end
def name=(value)
@xml_root_object.name = value
end
def number
@xml_root_object.number
end
def number=(value)
@xml_root_object.number = value
end
def to_xml
@xml_root_object.description = description
@xml_root_object.code = code
@xml_root_object.to_xml
end
def description
'wooo'
end
def code
'ABC123'
end
end
root = Xml::Root.new
root.number = 12_345
puts root.to_xml
# <?xml version="1.0"?>
# <Working>
# <mywraptag>
# <number>12345</number>
# </mywraptag>
# </Working>
encapsulted_root = Root.new
encapsulted_root.number = 12_345
puts encapsulted_root.to_xml
# <?xml version="1.0"?>
# <Working>
# <mywraptag>
# <description>wooo</description>
# <number>12345</number>
# </mywraptag>
# <code>ABC123</code>
# </Working> |
Thanks @burtlo for the explanation, right now I'm avoiding using wrap, I've simply created another class holding the elements I need. |
For this example:
I have the following output:
but if I comment the
description
method, and set the attribute like I do for thenumber
it will generate the wrap tag as expected.In my case I'm using those defined methods to get some fields from database objects, so I must admit that this could probably be not the most common scenario. Even though as far as I understand if should work.
The text was updated successfully, but these errors were encountered: