Comments (3)
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.
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.
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)
- How to access `wrap` child elements HOT 1
- XML parse issue HOT 1
- Malformed XML while using to_xml HOT 2
- Deprecate positional optional arguments for #to_xml
- Better support for inheritance
- Register Namespace Ordering HOT 2
- Defining namespaces by URI, not just prefix?
- has_many finds deep elements, not just immediate children HOT 2
- Get #inner_html instead of #content HOT 1
- CamelCase elements always nil HOT 2
- Namespace confusion HOT 3
- Parsing nil values in XML causing ArgumentError HOT 2
- Update gemspec HOT 1
- Make happymapper specs run warning-free
- Run specs on Travis CI HOT 1
- Document options and their relations better HOT 1
- Contact Github to turn this fork into its own project. HOT 5
- Parse takes a block, only used if :in_groups_of is passed?
- Warn if multiple elements found unexpectedly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from happymapper.