Skip to content
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

Open
fidalgo opened this issue Mar 6, 2018 · 3 comments
Open

wrap doesn't work for custom getter #90

fidalgo opened this issue Mar 6, 2018 · 3 comments
Assignees
Labels

Comments

@fidalgo
Copy link

fidalgo commented Mar 6, 2018

For this example:

require 'happymapper'

class Root
  include HappyMapper

  tag 'Working'
  element :name, String
  wrap 'mywraptag' do
    element :description, String
    element :number, Integer
  end
  element :code, String

  def description
    'wooo'
  end

  def code
    'ABC123'
  end
end

root = Root.new
root.number = 12_345

puts root.to_xml

I have the following output:

<?xml version="1.0"?>
<Working>
  <mywraptag>
    <number>12345</number>
  </mywraptag>
  <code>ABC123</code>
</Working>

but if I comment the description method, and set the attribute like I do for the number 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.

@burtlo
Copy link
Collaborator

burtlo commented Mar 7, 2018

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 wrap method you define an anonymous class which defines any attributes or elements inside of it.

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 wrap implementation.

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.

@burtlo burtlo added the bug label Mar 7, 2018
@burtlo
Copy link
Collaborator

burtlo commented Mar 7, 2018

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>

@fidalgo
Copy link
Author

fidalgo commented Mar 7, 2018

Thanks @burtlo for the explanation, right now I'm avoiding using wrap, I've simply created another class holding the elements I need.

@mvz mvz self-assigned this Sep 1, 2018
@mvz mvz changed the title wrap don't work for custom getter wrap doesn't work for custom getter Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants