-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
Define a custom field type:
class MyField
include Mongoid::Fields::Serializable
attr_accessor :my_var
def initialize(var)
@my_var = var
end
def deserialize(object)
self.class.new(object['my_var'])
end
def serialize(object)
{'my_var' => object.my_var} end
end
and then subclass it:
class MySubfield < MyField
end
When used in a model like this:
class MyDoc
include Mongoid::Document
field :field, :type => MyField
field :subfield, :type => MySubfield
end
the subfield field is not deserialized according to the method defined in MyField:
>> x = MyDoc.new(:field => MyField.new("hello"), :subfield => MySubfield.new("world"))
>> x.field
=> #<MyField:0x1037e32c8 @my_var="hello">
>> x.subfield
=>