Coder Social home page Coder Social logo

wrap doesn't work for custom getter about happymapper HOT 3 OPEN

mvz avatar mvz commented on July 28, 2024
wrap doesn't work for custom getter

from happymapper.

Comments (3)

burtlo avatar burtlo commented on July 28, 2024

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.

from happymapper.

burtlo avatar burtlo commented on July 28, 2024

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>

from happymapper.

fidalgo avatar fidalgo commented on July 28, 2024

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

from happymapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.