MongoMapper and custom type properties
-
27-09-2019 - |
Question
I am trying to create a MongoMapper model which has an array of attributes. This array should contain all kind of information (Integers, Floats, URLs, Enums, Dates, Relations to other documents) depending on the user`s desire.
My problem is how implement the array of attributes, how to implement the custom data type of the attributes. I suppose that the document serialized should look similar to this:
{
...
attributes: {
age: {
name: "Age",
type: "Integer",
value: 12
},
height: {
name: "Height",
type: "Float",
value: 1.86
},
mother: {
name: "Mother",
type: "Relation\Person",
value: "\people\sara"
}
}
}
Any ideas how to approach this problem? My first tought was to create a base class like a factory which has to serialize and deserialize the attributes, but from what I have managed to code, nothing was working properly.
Thanks in advance!
Solution
You could make a custom type, but if you have an array, why not just use an embedded object?
See this: http://speakerdeck.com/u/jnunemaker/p/mongomapper-mapping-ruby-to-and-from-mongo
Slides 47-49.